I'm still very new to this but I've put together some code that when a button is pressed, my servo runs continuously, a second button rotates it the other way and a third cancels everything.
A red or green comes on depending on the rotation and this can be one using an IR receiver also.
Issue I'm having is that randomly, 1/5 of the time, the red or green LED does not turn off. I can manually kill it using the third cancel button or just press it a couple times until it turns off as it should on the next push of the button.
Cant work out where im going wrong.
I also find my IR remote works maybe 60% of the time. Any help would be greatly appreciated.
#include <Servo.h>
Servo servo1;
int servoPin = 9;
#include "IRremote.h"
/----- Variables, Pins -----/
int receiver = 12; // Signal Pin of IR receiver to Arduino Digital Pin 6
int RledPin = 5;
int GledPin = 3;
int buttonApin = 7;
int buttonBpin = 8;
int buttonCpin = 4;
unsigned long elapsedTime;
unsigned long onTime;
/-----( Declare objects )-----/
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
void setup()
{
irrecv.enableIRIn(); // Start the receiver
servo1.attach(servoPin);
servo1.write(90);
pinMode(RledPin, OUTPUT);
pinMode(GledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
pinMode(buttonCpin, INPUT_PULLUP);
}
void loop(){
if (digitalRead(buttonApin) == LOW) // Switch is closed to start LED timer
{
digitalWrite(RledPin, HIGH); // Red LED comes On
digitalWrite(GledPin, LOW); // Green LED Goes Off
servo1.attach(servoPin); //Servo is activated
servo1.write(0); //Servo turns on clockwise
onTime = millis(); //time set
}
if(onTime > 0 && millis() - onTime > 1500) //time limit target
{
servo1.write(90); //Servo stops
servo1.detach(); //servo deactivated
delay(500); //used to sync the servo stop with the LED off
digitalWrite(RledPin, LOW); // LED goes off
onTime = 0; //time reset
}
if (digitalRead(buttonCpin) == LOW) // Switch is closed to start LED timer
{
digitalWrite(GledPin, HIGH); // Green LED comes On
digitalWrite(RledPin, LOW); // RED LED Goes Off
servo1.attach(servoPin); //Servo is activated
servo1.write(180); //Servo turns on anti-clockwise
onTime = millis(); //time set
}
if(onTime > 0 && millis() - onTime > 1500) //time limit target
{
servo1.write(90); //Servo stops
servo1.detach(); //servo deactivated
delay(500);
digitalWrite(GledPin, LOW);
onTime = 0; //time reset
}
if (digitalRead(buttonBpin) == LOW) //Deactivation Sequence KILL SWITCH
{
digitalWrite(RledPin, LOW);
digitalWrite(GledPin, LOW);
servo1.write(90);
servo1.detach();
onTime = 0; //time reset
}
if (irrecv.decode(&results)) // have we received an IR signal?
{
switch(results.value)
{
case 0xFFE01F: // DOWN button pressed
{
digitalWrite(RledPin, HIGH); // Red LED comes On
digitalWrite(GledPin, LOW); // Green LED Goes Off
servo1.attach(servoPin); //Servo is activated
servo1.write(0); //Servo turns on clockwise
onTime = millis(); //time set
}
if(onTime > 0 && millis() - onTime > 2000) //time limit target
{
servo1.write(90); //Servo stops
servo1.detach(); //servo deactivated
digitalWrite(RledPin, LOW); // LED goes off
onTime = 0; //time reset
}
break;
case 0xFF906F: // UP button pressed
{
digitalWrite(GledPin, HIGH); // Green LED comes On
digitalWrite(RledPin, LOW); // RED LED Goes Off
servo1.attach(servoPin); //Servo is activated
servo1.write(180); //Servo turns on anticlockwise
onTime = millis(); //time set
}
if(onTime > 0 && millis() - onTime > 2000) //time limit target
{
servo1.write(90); //Servo stops
servo1.detach(); //servo deactivated
digitalWrite(GledPin, LOW); // LED goes off
onTime = 0; //time reset
}
break;
case 0xFF02FD: // PAUSE button pressed
servo1.write(90);
servo1.detach();
digitalWrite(GledPin, LOW);
digitalWrite(GledPin, LOW);
break;
}
irrecv.resume(); // receive the next value
}
}/* --end main loop -- */
doRed = true;......case 0xFFE01F:does the same thing .... neither of these do anything else, just set a flag and continue on ..... further down inloop(), act on the state of the flagif (doRed) {– jsotola May 16 '21 at 22:32if button is pressed, then turn on LED.... separatedif button is pressed, then set a flag....if flag is set, then turn on LED.... multiple events can set the flag ... only one function checks the flag and activates the LED – jsotola May 17 '21 at 18:34