Having Issues with code

Status
Not open for further replies.
Arduino: 1.6.3 (Windows 7), TD: 1.22, Board: "Teensy 3.1, Serial + Keyboard + Mouse + Joystick, 96 MHz optimized (overclock), US English"

pedals_ino.ino: In function 'void loop()':
pedals_ino.ino:28:3: error: 'else' without a previous 'if'
pedals_ino.ino:28:9: error: expected primary-expression before ')' token
pedals_ino.ino:38:3: error: 'else' without a previous 'if'
pedals_ino.ino:38:9: error: expected primary-expression before ')' token
expected primary-expression before ')' token

Code:
const int pinZero = 0;
const int pinOne = 1;


int ledPin = 13;
boolean ledOn = true;


int buttonState = 0;

void setup () {
  pinMode (pinZero, INPUT_PULLUP);
  pinMode (pinOne, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(9600);
  Keyboard.begin();
}

void loop () {

  buttonState = digitalRead(pinZero);
  if (digitalRead(pinZero) == LOW) ; {
    Keyboard.write(32);
    digitalWrite(ledPin, HIGH);
    delay(300);
  }
  else () ; {
    digitalWrite(pinZero, HIGH);
    digitalWrite(ledPin, LOW);
  }
  buttonState = digitalRead(pinOne);
  if (digitalRead(pinOne) == LOW); {
    Keyboard.write('v');
    digitalWrite(ledPin, HIGH);
    delay(300);
  }
  else () ; {
    digitalWrite(pinOne, HIGH);
    digitalWrite(ledPin, LOW);
  }
}

Been working on this for a foot pedal build for Guildwars 2, as in to Jump (space) and Dodge (v), Finding I am having trouble with the building of this code. Looking for guidance please. Plus a Debounce addition to the code as well if that helps, with the update to 1.6.* my code no longer works to be verified and uploaded.
 
You have spare ';' on your conditional lines.

After if only one statement will be executed, your ';' is that statement. If you remove the ';' the statement to be executed is defined by the enclosing braces {} that follow.

In the provided sample the "if (digitalRead(pinZero) == LOW) ; {" ends with the ';', that makes the following {} code an unconditional statement, followed by an unpaired "else ();" - also with a spare ';' as well as a spurious set of "()".

Below for loop() code will be closer to compiling as intended - the 4 BOLD lines are edited::

Code:
void loop () {

  buttonState = digitalRead(pinZero);
[B]  if (digitalRead(pinZero) == LOW) {[/B]
    Keyboard.write(32);
    digitalWrite(ledPin, HIGH);
    delay(300);
  }
[B]  else {[/B]
    digitalWrite(pinZero, HIGH);
    digitalWrite(ledPin, LOW);
  }
  buttonState = digitalRead(pinOne);
[B]  if (digitalRead(pinOne) == LOW) {[/B]
    Keyboard.write('v');
    digitalWrite(ledPin, HIGH);
    delay(300);
  }
[B]  else {[/B]
    digitalWrite(pinOne, HIGH);
    digitalWrite(ledPin, LOW);
  }
}

Also - there is a library "#include <Bounce.h>" that will better detect key events, I haven't used it or looked at it - but I assume you can find an example in the library.
 
Last edited:
Thanks, it is working again, now just to get assistance on the debouncing codes, some times the Foot pedals I built ghost click, but no connection is made physically
 
Thanks, it is working again, now just to get assistance on the debouncing codes, some times the Foot pedals I built ghost click, but no connection is made physically
You do need debouncing if you don't now have it. But when you say "no connection is made physically" this suggests there's a second problem - that being the signal voltage level conditioning. If "no connection.." means the pedal is unplugged, then noise can get on the wires and trigger the firmware. Debouncing might also reject noise like this. But do you have strong pull-ups or pull-downs on the I/O bits? With some filters? And so on.
 
That Is what I was saying, debouncing code, looking for some help on this issues as I stated: I press the pedal, it dodges, then moments later (while not even remotely near the pedal) it dodges on its own. I figured debouncing would fix this issue, but am not well versed on that aspect.
 
Hopefully this looks like more of a final product.

Code:
const int pinZero = 0;
const int pinOne = 1;


int ledPin = 13;
boolean ledOn = true;

int counter = 0;
int reading;
int current_state = LOW;

long time = 0;
int debounce_count = 10;

int buttonState = 0;

void setup () {
  pinMode (pinZero, INPUT_PULLUP);
  pinMode (pinOne, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  digitalWrite(ledPin, current_state);
  Serial.begin(9600);
  Keyboard.begin();
}

void loop () {

  buttonState = digitalRead(pinZero);
  if(millis() != time)
  {
    reading = digitalRead(pinZero);

    if(reading == current_state && counter > 0)
    {
      counter--;
    }
    if(reading != current_state)
    {
       counter++; 
    }
    // If the Input has shown the same value for long enough let's switch it
    if(counter >= debounce_count)
    {
      counter = 0;
      current_state = reading;
      digitalWrite(ledPin, current_state);
    }
    time = millis();
  }
  if (digitalRead(pinZero) == LOW) {
    Keyboard.write(32);
    digitalWrite(ledPin, HIGH);
    delay(300);
  }
  else {
    digitalWrite(pinZero, HIGH);
    digitalWrite(ledPin, LOW);
  }
  buttonState = digitalRead(pinOne);
  if(millis() != time)
  {
    reading = digitalRead(pinOne);

    if(reading == current_state && counter > 0)
    {
      counter--;
    }
    if(reading != current_state)
    {
       counter++; 
    }
   if(counter >= debounce_count)
    {
      counter = 0;
      current_state = reading;
      digitalWrite(ledPin, current_state);
    }
    time = millis();
  }
  if (digitalRead(pinOne) == LOW) {
    Keyboard.write('v');
    digitalWrite(ledPin, HIGH);
    delay(300);
  }
  else {
    digitalWrite(pinOne, HIGH);
    digitalWrite(ledPin, LOW);
  }
}
 
Status
Not open for further replies.
Back
Top