Go Down

Topic: 2 Reed switch (start and end) (Read 173 times) previous topic - next topic

Mouraa

May 26, 2019, 02:46 pm Last Edit: May 26, 2019, 03:30 pm by Mouraa
This code have some errors and i want help to fix this.

The code working, but after i tryed add the second reedswitch this stoped work.

This is the part: (bold part)

Code: [Select]

}
void loop()
{ if (digitalRead(switchReed1) == LOW) { //Se o circuito estiver fechado
digitalWrite(LED_R, LOW); //LED verde
digitalWrite(LED_G, HIGH);
digitalWrite(IN2, LOW);
} else {
digitalWrite(LED_R, HIGH); //LED vermelho
digitalWrite(LED_G, LOW);
}
{
[b]  if (digitalRead(switchReed2) == LOW) { //Se o circuito estiver fechado
digitalWrite(LED_R, LOW); //Sem LED
digitalWrite(LED_G, LOW);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);[/b]

}
 
 
 
  do_IR_Tick();
  do_Drive_Tick();
}
}

J-M-L

#1
May 26, 2019, 03:20 pm Last Edit: May 26, 2019, 03:20 pm by J-M-L
Please correct your post above and add code tags around your code:
[code] // your code is here [/code].

It should look like this:
Code: [Select]
// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

You need to also describe how things are wired, powered, what arduino you use etc and what you mean when you say it stoped working?
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Mouraa

Code: [Select]

//Defenir pins e variaveis
#include <IRremote.h>
#define IN1  2   //K1、K2 motor direction
#define IN2  4     //K1、K2 motor direction
#define ENA  5    // Needs to be a PWM pin to be able to control motor speed ENA
#define ENB  6    // Needs to be a PWM pin to be able to control motor speed ENA
#define IRPIN 3 //IR receiver Signal pin connect to Arduino pin 4
#define switchReed1 // Reed switch pin 4
#define switchReed2 // Reed switch pin 0
#define IR_ADVANCE       0x00FF18E7       //code from IR controller "▲" button
#define IR_BACK          0x00FF4AB5       //code from IR controller "▼" button
#define LED_G 10
#define LED_R 9
enum DN
{
  GO_ADVANCE, //go ahead
  GO_BACK,//go back
  DEF
}Drive_Num=DEF;
IRrecv IR(IRPIN);  //   IRrecv object  IR get code from IR remoter
decode_results IRresults;   
bool stopFlag = true;//set stop flag
bool JogFlag = false;
uint16_t JogTimeCnt = 0;
uint32_t JogTime=0;





/*motor control*/
void go_back(int t)  //motor rotate clockwise -->robot go ahead
{
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  delay(t);
}
void go_ahead(int t) //motor rotate counterclockwise -->robot go back
{
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  delay(t);
}
void go_stop() //motor brake -->robot stop
{
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
}





/*set motor speed */
void set_motorspeed(int lspeed,int rspeed) //change motor speed
{
  analogWrite(ENA,lspeed);//lspeed:0-255
  analogWrite(ENB,rspeed);//rspeed:0-255   
}






/**************detect IR code***************/
void do_IR_Tick()
{
  if(IR.decode(&IRresults))
  {
    if(IRresults.value==IR_ADVANCE)
    {
      Drive_Num=GO_ADVANCE;
    }
    else if(IRresults.value==IR_BACK)
    {
        Drive_Num=GO_BACK;
    }
    IRresults.value = 0;
    IR.resume();
  }
}




/**************car control**************/
void do_Drive_Tick()
{
    switch (Drive_Num)
    {
      case GO_ADVANCE:
            set_motorspeed(100,100);go_ahead(20);JogFlag = true;JogTimeCnt = 3;JogTime=millis();break;//if GO_ADVANCE code is detected, then go advance
      case GO_BACK:
            set_motorspeed(100,100);go_back(20);JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//if GO_BACK code is detected, then backward
      default:break;
    }
    Drive_Num=DEF;
   

}
void loop()
{
if (digitalRead(switchReed1) == LOW) { //Se o circuito estiver fechado
  digitalWrite(LED_R, LOW);//LED verde
  digitalWrite(LED_G, HIGH);
  digitalWrite(switchReed2, LOW);
  digitalWrite(IN2, LOW);
} else {
 
  digitalWrite(LED_R, HIGH); //LED vermelho
  digitalWrite(LED_G, LOW);
}
{
  if (digitalRead(switchReed2) == LOW) {
  digitalWrite(switchReed1, LOW)
  digitalWrite(LED_R, LOW);//SEM LED
  digitalWrite(LED_G, LOW);
  digitalWrite(IN1, LOW);
} else {
  digitalWrite(switchReed1, LOW);
  digitalWrite(LED_R, LOW);
  digitalWrite(LED_G, LOW);
 
 
 
  do_IR_Tick();
  do_Drive_Tick();
}
}





void setup() { /******L298N******/
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ENA, OUTPUT); 
  pinMode(ENB, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(LED_G, OUTPUT);
  pinMode(switchReed1, INPUT);
    pinMode(switchReed2, INPUT);
  pinMode(IRPIN, INPUT);

 
  digitalWrite(IRPIN, HIGH); 
  IR.enableIRIn();
}

J-M-L

#3
May 26, 2019, 03:53 pm Last Edit: May 26, 2019, 03:55 pm by J-M-L
Really?
Code: [Select]
  digitalWrite(switchReed1, LOW);
...
  digitalWrite(switchReed2, LOW)


again, You need to also describe how things are wired, powered, what arduino you use etc and what you mean when you say it stoped working?
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Mouraa

im using a arduino uno, 2 reed switches (start and end), 1 led rgb, 1 l298N to control the DC motor


IN1 - pin2   
IN2 - pin4     
ENA - pin 5   
ENB - pin 6 
IRPIN - pin 3
switchReed1 - pin 1
switchReed2 - pin 0
IR_ADVANCE       0x00FF18E7       //code from IR controller "▲" button
IR_BACK          0x00FF4AB5       //code from IR controller "▼" button
LED_G - pin 10
LED_R - pin 9

J-M-L

#5
May 26, 2019, 04:24 pm Last Edit: May 26, 2019, 04:26 pm by J-M-L
When the Reed Switch is exposed to a magnetic field, the two ferrous materials inside the switch joins and the switch closes.

When the magnetic field is removed, the reeds separate and the switch opens. This makes for a great non-contact switch, as long as you have a magnet to trigger it.

One would usually connect one side of the Switch to a digital Pin, set up as INPUT_PULLUP and the other side to GND.

When reading the digital pin, if you see HIGH it means the Switch has not been triggered (it's open) and if you see LOW it means it's closed.


--> How are your Reed Switch connected ? why would you pull their pin LOW ? (which removed the pullup)
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

groundFungus

Also posted here.   Posting the same question in different places is cross posting.  Cross posting wastes time.   Do not cross post.  

Mouraa

The 1 reedswitch (end) is working but the start reedswitch i cant put him working.
I have a magnet in the door,
I  put them close because when the door move and the reedswitch detect the magnet, circuit stop, and i cant do it in start switch

J-M-L

You still did not describe your wiring. I'm tired of asking.

Quote
I  put them close because when the door move and the reedswitch detect the magnet, circuit stop, and i cant do it in start switch
Don't mess with the pins by writing to them - they are meant to be in INPUT_PULLUP mode, so just be read - to indicate the presence or not of the magnet.
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Mouraa

Thanks!

What i exactly need to change in code?

J-M-L

I let you figure it out. as mentioned above, you don't answer questions. if you do not bother read and understand  what I'm typing, I'll go play on other stuff. Good luck.
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Go Up