Hello,
I am using an arduino to drive an electric scooter and everything works fine but after the throttle is done being used I want the scooter to time out and open a relay. I am using the following code:
const int sLED = 5;
const int iLED = 6;
const int dLED = 7;
const int rOut = 8;
const int fetOut = 9;
const int pIn = A1;
boolean driveFlag;
void setup(){
pinMode(sLED, OUTPUT);
pinMode(iLED, OUTPUT);
pinMode(dLED, OUTPUT);
pinMode(rOut, OUTPUT);
pinMode(fetOut, OUTPUT);
pinMode(pIn, INPUT);
digitalWrite(sLED, HIGH);
digitalWrite(iLED, HIGH);
digitalWrite(dLED, HIGH);
digitalWrite(rOut, LOW);
digitalWrite(fetOut, LOW);
delay(1000);
startUp();
}
void loop(){
idle();
drive();
timeout();
if(driveFlag = true){
drive();
}else{
analogWrite(fetOut, 0);
digitalWrite(rOut, LOW);
digitalWrite(dLED, HIGH);
delay(100);
}
}
void startUp(){
int tempPot = 500;
digitalWrite(sLED, LOW);
do{
tempPot = analogRead(pIn);
delay(10);
}while(tempPot >= 450);
digitalWrite(sLED, HIGH);
delay(100);
}
void idle(){
int tempPot = 0;
digitalWrite(iLED, LOW);
do{
tempPot = analogRead(pIn);
delay(10);
}while(tempPot < 450);
digitalWrite(iLED, HIGH);
delay(100);
}
void drive(){
int potRead = 450;
int mapVal = 0;
digitalWrite(dLED, LOW);
digitalWrite(rOut, HIGH);
delay(80);
do{
potRead = analogRead(pIn);
if(potRead < 450){
potRead = 450;
}else if(potRead > 700){
potRead = 700;
}
mapVal = map(potRead, 450, 700, 0, 255);
analogWrite(fetOut, mapVal);
delay(10);
}while(potRead >= 450);
}
void timeout(){
int tempRead = 0;
int delayTime = 100;
for(int i = 0; i < delayTime; i++){
tempRead = analogRead(pIn);
if(tempRead > 450){
driveFlag = true;
break;
}else{
driveFlag = false;
}
}
}
What I am observing is the drive LED never shuts off so that means the code never gets past the drive loop in the main loop.
Code notes:
I am sourcing the LEDs so I am pulling them low to turn them on.
The PWM and everything else works fine but the relay refuses to open with a low command to it. I have tried to relay just using the arduino and it is capable of opening and closing it. The arduino controls a NPN transistor to switch on the relay with no problem.
So any code suggestions would be appreciated.