I’m relatively new to arduino coding, and had some problems with coding.
I’m trying to change some existing code to power a small relay instead of an LED.
Instead of outputting 1 second of power to an LED, I want case -1 to output power to pin 3 until case -2 is triggered, regardless of other input. If possible, I would also like case -3 to output power to pin 5 until case -3 is triggered again. The entire thing should run on a loop so that each trigger can be repeated multiple times.
The output to the pins is not directly powering a relay, it will be running through a transistor first to trigger the relay on with another power source.
The code below is the sections I am referring to.
#define LED1 3
#define LED2 4
#define LED3 5
(skips some lines for space)
if((tA <= tB) && tA <= tC)
return -3 ;// FORWRD
if((tB <= tC) && tB <= tA)
return -2 ;// LEFT
if((tC <= tB) && tC <= tA)
return -1 ;//RIGHT
void DoCommand(int command)
case 0: Serial.println(“Stopped”);delay(500);break;
case -1: Serial.println("Turn RIGHT ");digitalWrite(LED1,HIGH); delay(1000);break;
case -2: Serial.println("Turn LEFT ");digitalWrite(LED2,HIGH); delay(1000);break;
case -3: Serial.println("GO FORWARD “);digitalWrite(LED3,HIGH); delay(1000);break;
case -4: Serial.println(” GO BACK "); delay(1000);break;
default : Serial.println(“NO Command detected”);break;
for(int i = 3;i<=5;i++)
tA = 0;//Reset time A
tB = 0;//Reset time B
tC = 0;//Reset time C