Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: Having Issues with code

  1. #1
    Member
    Join Date
    Dec 2013
    Location
    Magalia, CA
    Posts
    23

    Having Issues with code

    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.

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,919
    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);
      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);
      }
    }
    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 by defragster; 04-24-2015 at 06:20 AM.

  3. #3
    Member
    Join Date
    Dec 2013
    Location
    Magalia, CA
    Posts
    23
    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

  4. #4
    Senior Member
    Join Date
    Dec 2014
    Posts
    306
    As Defragster mentioned above, the Bounce library can deal with your debouncing for you.
    https://www.pjrc.com/teensy/td_libs_Bounce.html

  5. #5
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    Quote Originally Posted by ReonBalisty View Post
    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.

  6. #6
    Member
    Join Date
    Dec 2013
    Location
    Magalia, CA
    Posts
    23
    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.

  7. #7
    Member
    Join Date
    Dec 2013
    Location
    Magalia, CA
    Posts
    23
    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);
      }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •