Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: Turning switches off

  1. #11
    Aviation Enthusiast
    Join Date
    Sep 2013
    Location
    Cornwall, UK
    Posts
    42

    Re: Turning switches off

    Hi!

    Sorry to keep asking so many questions - I'm an arduino noob

    I tried what you suggested but the serial monitor still shows that it is sending repeated C02/C01 lines.

    int val = 0;int inputPin = 2;
    int valOld = 0;


    void setup()
    {
    pinMode(inputPin, INPUT);
    Serial.begin(9600);
    digitalWrite(inputPin, HIGH);
    }


    void loop() {
    val = digitalRead(inputPin);
    if (val != valOld); {
    if (val == HIGH) { Serial.println("C02"); } else { Serial.println("C01"); };
    valOld = val;
    }
    }


  2. #12
    150+ Forum Groupie
    Join Date
    Apr 2011
    Location
    United States of America
    Posts
    233

    Re: Turning switches off

    That's ok. I encourage it because we all had to start somewhere.

    Usually when I have difficulty, I serialPrint the variables for troubleshooting; similar to how you are using C01/C02.
    Serial.println("val");
    Serial.println("valOld")
    delay(750);

  3. #13
    Aviation Enthusiast
    Join Date
    Sep 2013
    Location
    Cornwall, UK
    Posts
    42

    Re: Turning switches off

    I did it!

    For some reason it wouldn't work as just an 'if' statement so I changed it to an 'if{}else' statement and it worked.

    For anybody that may be experiencing similar problems:
    int val = 0;int inputPin = 2;
    int valOld = 0;


    void setup()
    {
    pinMode(inputPin, INPUT);
    Serial.begin(9600);
    digitalWrite(inputPin, HIGH);
    }


    void loop() {
    val = digitalRead(inputPin);

    if (val == valOld) {
    } else {
    if (val == HIGH) { Serial.println("C02"); } else { Serial.println("C01"); };
    valOld = val;
    }
    delay(500);
    }

    Edit: I added a delay as without it any rapid switch changes caused problems.
    Last edited by samfranklin; 10-20-2013 at 10:52 AM. Reason: Edit in coding

  4. #14
    150+ Forum Groupie
    Join Date
    Apr 2011
    Location
    United States of America
    Posts
    233

    Re: Turning switches off

    Awesome. It is strange you had to use an else statement to make it work.
    Code:
    int val = 0;int inputPin = 2;
    int valOld = 0;
    
    
    void setup()
    {
    pinMode(inputPin, INPUT);
    Serial.begin(9600);
    digitalWrite(inputPin, HIGH);
    }
    
    void loop() {
    val = digitalRead(inputPin);
    
    if (val != valOld) {
    if (val == HIGH) { Serial.println("C02"); } else { Serial.println("C01"); };
    
    }
    valOld = val;  //moved outside of if/then statement
    delay(500);
    }
    As for the rapid switch changes causing problems: Are you using a 10K pull-down resistor when the switch is in the off/open position? I also assume you are using a ~10K pull-up for the on/closed position of the switch.

  5. #15
    New Member
    Join Date
    Mar 2018
    Location
    Chandigarh India
    Posts
    2

    Re: Turning switches off

    Hi,
    I am new to arduino and Link2Fs, The above code is for a single switch, Could anyone please help me with the code if i wanted to have multiple switches.
    Thanks,

  6. #16
    150+ Forum Groupie BushPilotWannabe's Avatar
    Join Date
    Jan 2014
    Location
    Alberta, Canada
    Posts
    203

    Re: Turning switches off

    Welcome. Happy to see some beginner Arduino, Link2FS activity.

    EDIT

    My apologies to karen_maverick. My original post has been expanded and moved into a new thead. http://www.mycockpit.org/forums/show...105#post171105
    ---CYXD ----- TWR --- GND ------ Closed
    ILS-- NDB -- 119.1 -- 121.9 ---- 11/2013

  7. #17
    New Member
    Join Date
    Mar 2018
    Location
    Chandigarh India
    Posts
    2

    Re: Turning switches off

    Thanks for the quick reply.

Page 2 of 2 FirstFirst 12