Hi!
im trying to do a automatic gate, and i can't put my reedswitch2 working (start sensor)
I want my project to look like an automatic gate, so if i press the button "▼" i want the gate to open automaticaly, and when i press the button "▲" i want the gate to close. The objective to have 2 reedswitches is to when is detects one of them the arduino knows that the gate is closed and when it detecs the other, arduino knows that the gate is open.
In this code im not able to make it work because it is giving an error in the if statement.
I would apreciate some help.
This is the code i used:
//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 1 // Reed switch pin 1
#define switchReed2 0 // 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(140,140);go_ahead(20);JogFlag = true;JogTimeCnt = 3;JogTime=millis();break;//if GO_ADVANCE code is detected, then go advance
case GO_BACK:
set_motorspeed(140,140);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(IN2, LOW);
} else {
digitalWrite(LED_R, HIGH); //LED vermelho
digitalWrite(LED_G, LOW);
}
{
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);
}
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);