hello guys,
i have an else statement in my codes that is not working !
what should happen is this : i have 3 potentiometers, but the one will be needed in this post are 2, one for throttle and one for brakes, and a push button to reverse, so when the push button state is LOW the reverse is off and when the push button state is HIGH the reverse is on but the else statement is not working for that to happen so any idea why ?
i tried else and else if and if alone but nothing nevertheless the debugging is giving a good state for the button and it indicates that it works (the statement i am talking about is indicated with ////)
here are the codes :
//Transmitter
#include <VirtualWire.h>
char Array[10];
int potPin1 = A0; // Throttle
int potPin2 = A1; // Brakes
int potPin3 = A2; //Streering
//these values will be sent by the transmitter
int valThrottle = 0;
int valBrakes = 0;
int valSteering = 0;
int transmitterPin = 10; //transmitter pin
int downShift = 2; //Left paddle shift pin 2
int upShift = 3; //Right paddle shift pin 3
int burnout = 5; //Burnout Button pin 5
int neutral = 6; //Neutral Green Button pin 6
int reverse = 7; //Reverse Button pin 7
int limiter = 8; //Pit Limiter Button pin 8
//int cam = 9; //Cam grey Button pin 9
//pin 4 is the white button
//downShift
boolean lastButton1 = LOW;
boolean currentButton1 = LOW;
//upShift
boolean lastButton2 = LOW;
boolean currentButton2 = LOW;
//burnout
boolean lastButton4 = LOW;
boolean currentButton4 = LOW;
//neutral
boolean lastButton5 = LOW;
boolean currentButton5 = LOW;
//reverse
boolean lastButton6 = LOW;
boolean currentButton6 = LOW;
boolean reverseState = LOW;
//limiter
boolean lastButton7 = LOW;
boolean currentButton7 = LOW;
//cam
boolean lastButton8 = LOW;
boolean currentButton8 = LOW;
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("Sending"); // Debugging only
// Initialise the IO and ISR
//vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_set_tx_pin(transmitterPin); //RF sending pin Arduino Mega
for(int i = 2; i<10; i++){
pinMode(i, INPUT);
}
}
//debounce function
boolean debounce(boolean last, int button)
{
boolean current = digitalRead(button);
if(last != current)
{
delay(5);
current = digitalRead(button);
}
return current;
}
void loop(){
//for the downShift
currentButton1 = debounce(lastButton1, downShift);
if(lastButton1 == LOW && currentButton1 == HIGH)
{
Serial.write('-');
}
lastButton1 = currentButton1;
//for the upShift
currentButton2 = debounce(lastButton2, upShift);
if(lastButton2 == LOW && currentButton2 == HIGH)
{
Serial.write('+');
}
lastButton2 = currentButton2;
//for the burnout
currentButton4 = debounce(lastButton4, burnout);
if(lastButton4 == LOW && currentButton4 == HIGH)
{
Serial.write('b');
}
lastButton4 = currentButton4;
//for the neutral
currentButton5 = debounce(lastButton5, neutral);
if(lastButton5 == LOW && currentButton5 == HIGH)
{
Serial.write('n');
}
lastButton5 = currentButton5;
//for the reverse
currentButton6 = debounce(lastButton6, reverse);
if(lastButton6 == LOW && currentButton6 == HIGH)
{
////////////////////////////////////////////////////////////
Serial.write('r');
reverseState = !reverseState;
Serial.println(reverseState);
delay(1000);
//////////////////////////////////////////////////////////////
}
lastButton6 = currentButton6;
//for the limiter
currentButton7 = debounce(lastButton7, limiter);
if(lastButton7 == LOW && currentButton7 == HIGH)
{
Serial.write('l');
}
lastButton7 = currentButton7;
//for the cam
/*currentButton8 = debounce(lastButton8, cam);
if(lastButton8 == LOW && currentButton8 == HIGH)
{
Serial.write('c');
}
lastButton8 = currentButton8;*/
//transmitter codes
valThrottle = analogRead(potPin1); // read the value from the sensor
valThrottle = map(valThrottle, 0, 55, 0, 255);
valThrottle = constrain(valThrottle, 0, 255);
valBrakes = analogRead(potPin2); // read the value from the sensor
////////////////////////////////////////////////////////////this is the statement/////
if(reverseState == HIGH){
valBrakes = map(valBrakes, 1023, 0, 0, 255); // this pot is broken
valBrakes = constrain(valBrakes, 0, 255);
}else{
valBrakes = map(valBrakes, 1023, 0, 0, 10); // this pot is broken
valBrakes = constrain(valBrakes, 0, 10);
}
///////////////////////////////////////////////////////////////////////////////////////////////
valSteering = analogRead(potPin3); // 0 to 1023
valSteering = map(valSteering, 0, 1023, 255, 0);
Serial.println(valSteering);
sprintf(Array, "%d,%d,%d.", valBrakes,valThrottle,valSteering);
vw_send((uint8_t*)Array, strlen(Array));
vw_wait_tx();
Serial.println(Array);
}