implementing toggle switch in arduino

Discussion in 'The Projects Forum' started by justtrying, Apr 14, 2012.

  1. justtrying

    Thread Starter Active Member

    Mar 9, 2011
    329
    352
    I need to program a toggle switch in arduino to be able to toggle between two modes. As a start I played around toggling LEDs and it worked out fine but I am having issues using it to switch between different sections of my program. Within the program a user should have a choice to go into either learn mode or trigger mode when using the device.

    Here is what I have as a basic program:

    Code ( (Unknown Language)):
    1. byte switchPin = 2;        //pin to which switch is connected
    2. byte ledPin = 13;
    3.  
    4. volatile boolean trigger = LOW; //interrupt attached to the pin
    5.  
    6. void setup()
    7. {
    8.   attachInterrupt(0, interrupttrigger, LOW); //digital pin 2 is interrupt 0
    9.   pinMode(ledPin,OUTPUT);
    10. }
    11.  
    12. void loop()
    13. {
    14.   delay(20); //wait to debounce the switch
    15.  
    16.   if (trigger = !trigger) //check if interrupt was triggered
    17.     {
    18.        LED(); //go execute the function
    19.        //use this to execute one function in one state
    20.     }
    21.   else
    22.     {
    23.         analogWrite(ledPin, 0); //turn LED off
    24.         //execute another function in another state
    25.     }
    26. }
    27.  
    28. void LED()
    29.  
    30. {
    31.   delay(200);
    32.   while(trigger == LOW)
    33.     {
    34.       digitalWrite(ledPin, HIGH);
    35.     }
    36. }  
    37.  
    38. void interrupttrigger()
    39. {
    40.   if(trigger == LOW)  //reset switch
    41.     {
    42.       trigger = HIGH;
    43.     }
    44.   else
    45.     {
    46.       trigger = LOW;
    47.     }
    48. }
    I thought that I should be able to replace LEDs with my respective functions, but it does not seem to be working. I've tried a few other things, but they only end in one mode stopping and restarting. I would appreciate any suggestions (except replacing this with a push button :))
     
  2. calypso_rae

    New Member

    Apr 15, 2012
    9
    0
    Hi, have you looked at the example sketch for toggling between two states. In my Arduino IDE, it's at File -> examples -> digital -> debounce

    I'm only new to this game, but in all of the working sketches that I've seen so far, there is nothing after the end of loop() ...

    Hope this helps,
    Rob
     
  3. chrisw1990

    Active Member

    Oct 22, 2011
    543
    41
    you dont have returns?
    do you need them for ardys?
     
  4. calypso_rae

    New Member

    Apr 15, 2012
    9
    0
  5. chrisw1990

    Active Member

    Oct 22, 2011
    543
    41
    im not arguing lol, i just wondered, iv always just put them at the end of functions.. if anything to just say "done" but each to their own, as long as the code works!:D
     
  6. justtrying

    Thread Starter Active Member

    Mar 9, 2011
    329
    352
    thanks, I got it to work. I think ideally I would want to use interrupts, but not enough time to sort it out.

    About return, arduino is interesting that way, almost "sloppy" as it contains no requirement to have a return at the end of a function.
     
  7. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    You would need a return statement in a function that returns a value rather than void.

    simple example:

    Code ( (Unknown Language)):
    1.  
    2. unsigned int get_product(unsigned char a, unsigned char b){
    3.  
    4.        return(a*b);
    5.  
    6. }
    7.  
    hgmjr
     
    chrisw1990 and justtrying like this.
  8. justtrying

    Thread Starter Active Member

    Mar 9, 2011
    329
    352
    we were hoping to get to that point, but will not make it ;)
     
  9. the_file

    New Member

    Apr 15, 2012
    6
    0
    That looks like a great project.
     
  10. calypso_rae

    New Member

    Apr 15, 2012
    9
    0
    Any chance of posting your final sketch here, plus a few words about how it works?

    Some of the official examples do indeed have functions after the end of loop(), so that's something new I've learned about Arduinos :)
     
  11. justtrying

    Thread Starter Active Member

    Mar 9, 2011
    329
    352
    I will, stay posted...
     
    ngwpower likes this.
Loading...