Results 1 to 6 of 6
  1. #1
    New Member
    Join Date
    Mar 2016
    Location
    Spain
    Posts
    3
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Exclamation Multiple outs error

    Hi!

    I'm new to the forum, but no so new to arduino.
    I had a setup with some leds, buttons, switches and even a 4 digit 7 segment screen which worked perfect with X-plane, but now that I'm trying to make it work with FS it gives me headaches. What happens is that each time I press the button I have (for brakes), it serial prints "C04" from 2 to 4 times, making it work sometimes but most of the time it returns to be without brakes. I don't know what's happening, because with X-plane it worked so I think it's not hardware but software (I've rewritten all the code).

    The code I'm using is this:

    Code:
      frenosState = digitalRead(botonFrenos); // so brakesState gets the value of the switch
      if (frenosState != frenosSave){ //to prevent sending the command multiple times if the button is hold
    
       if (frenosState == HIGH) {
        Serial.println("C04"); //serial.print only when it changes to HIGH
        frenosSave = frenosState; //save the last position of the button
       }else{
        frenosSave = frenosState;
       }
      }
    Obviously, at the start of the code I have

    Code:
    int frenosSave = 0;
    int frenosState = 0;
    int botonFrenos = 6;
    And in setup I have

    Code:
    pinMode(botonFrenos, INPUT);
    So I don't know why it's sending the command several times instead of just once.

  2. #2
    150+ Forum Groupie


    BushPilotWannabe's Avatar
    Join Date
    Jan 2014
    Location
    Alberta, Canada
    Posts
    176
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Multiple outs error

    Do you have a noisy pushbutton?
    ---CYXD ----- TWR --- GND ------ Closed
    ILS-- NDB -- 119.1 -- 121.9 ---- 11/2013

  3. #3
    New Member
    Join Date
    Mar 2016
    Location
    Spain
    Posts
    3
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Multiple outs error

    I didn't think so, but I added a delay(200); after sending the println and it worked perfect, so it must be that problem, thanks!

  4. #4
    150+ Forum Groupie


    BushPilotWannabe's Avatar
    Join Date
    Jan 2014
    Location
    Alberta, Canada
    Posts
    176
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Multiple outs error

    That is nice to hear.

    Hugh
    Attached Images Attached Images
    ---CYXD ----- TWR --- GND ------ Closed
    ILS-- NDB -- 119.1 -- 121.9 ---- 11/2013

  5. #5
    150+ Forum Groupie
    Join Date
    Nov 2013
    Location
    Evansville, Indiana
    Posts
    243
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Multiple outs error

    Try this instead of delay(200)

    In your variable declarations:
    Code:
    unsigned long time;
    unsigned long last_time = 0;  
    int debounce_time = 200;  // <--- 200ms  ** change this number to increase or decrease debounce delay **
    In your void loop
    Code:
    void loop() 
    { 
         time = millis(); 
         // and your other stuff
    }
    To debounce your button...
    Code:
    frenosState = digitalRead(botonFrenos); // so brakesState gets the value of the switch
    if (frenosState != frenosSave){ //to prevent sending the command multiple times if the button is hold
    
    
         if (frenosState == HIGH) 
         {
               if (time - last_time > debounce_time)
               {
                    Serial.println ("C04");    
                    last_time = time;
               }
         }
         else
         {
               frenosSave = frenosState;  
         }
    }

    ~Fess
    I will only ever be, half the Geek that I wished I was.
    TheGeekForge.Com

  6. #6
    New Member
    Join Date
    Mar 2016
    Location
    Spain
    Posts
    3
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Multiple outs error

    Quote Originally Posted by Fess_ter View Post
    Try this instead of delay(200)

    In your variable declarations:
    Code:
    unsigned long time;
    unsigned long last_time = 0;  
    int debounce_time = 200;  // <--- 200ms  ** change this number to increase or decrease debounce delay **
    In your void loop
    Code:
    void loop() 
    { 
         time = millis(); 
         // and your other stuff
    }
    To debounce your button...
    Code:
    frenosState = digitalRead(botonFrenos); // so brakesState gets the value of the switch
    if (frenosState != frenosSave){ //to prevent sending the command multiple times if the button is hold
    
    
         if (frenosState == HIGH) 
         {
               if (time - last_time > debounce_time)
               {
                    Serial.println ("C04");    
                    last_time = time;
               }
         }
         else
         {
               frenosSave = frenosState;  
         }
    }

    ~Fess
    Oh! I see what you did

    I used delay instead of a millis check because I was a bit lazy, thanks for the best solution!

    Oh, and you missed the frenosSave = frenosState in the HIGH, or my fault, that I could have wrote that only once this way:

    Code:
    frenosState = digitalRead(botonFrenos); // so brakesState gets the value of the switch
    if (frenosState != frenosSave){ //to prevent sending the command multiple times if the button is hold
    
    
         if (frenosState == HIGH) 
         {
               if (time - last_time > debounce_time)
               {
                    Serial.println ("C04");    
                    last_time = time;
               }
         }
         frenosSave = frenosState;  
    }