please help i need to add a toggle switch to my arduino curcuit to break the loop (stop start)

Discussion in 'Embedded Systems and Microcontrollers' started by jimmyoooooo, Oct 24, 2015.

  1. jimmyoooooo

    Thread Starter New Member

    Oct 24, 2015
    3
    0
    i am making a timing device for interval training i have done the timer 7 seg led and the buzzer but just need to add an on off switch basically any help is appreciated im fairly new to this




    Code (C):
    1.  
    2.   int pinA = 2;
    3.   int pinB = 3;
    4.   int pinC = 4;
    5.   int pinD = 5;
    6.   int pinE = 6;
    7.   int pinF = 7;
    8.   int pinG = 8;
    9.  
    10.   // the setup function runs once when you press reset or power the board
    11. void setup() {
    12.   // initialize digital pin 13 as an output.
    13.   pinMode(pinA, OUTPUT);
    14.   pinMode(pinB, OUTPUT);
    15.   pinMode(pinC, OUTPUT);
    16.   pinMode(pinD, OUTPUT);
    17.   pinMode(pinE, OUTPUT);
    18.   pinMode(pinF, OUTPUT);
    19.   pinMode(pinG, OUTPUT);
    20.   pinMode(1, OUTPUT);
    21.  
    22. }
    23.  
    24. // the loop function runs over and over again forever
    25. void loop() {
    26.  
    27.  
    28.  
    29.   //0
    30.   digitalWrite(pinA,HIGH);   // turn the LED on (HIGH is the voltage level)
    31.   digitalWrite(pinB,HIGH);   // turn the LED on (HIGH is the voltage level)
    32.   digitalWrite(pinC,HIGH);   // turn the LED on (HIGH is the voltage level)
    33.   digitalWrite(pinD,HIGH);   // turn the LED on (HIGH is the voltage level)
    34.   digitalWrite(pinE,HIGH);   // turn the LED on (HIGH is the voltage level)
    35.   digitalWrite(pinF,HIGH);   // turn the LED on (HIGH is the voltage level)
    36.   digitalWrite(pinG,LOW);   // turn the LED on (HIGH is the voltage level)
    37.   delay(1000);              // wait for a second
    38.  
    39.  
    40.  
    41.  
    42.   //1
    43.   digitalWrite(pinA,LOW);   // turn the LED on (HIGH is the voltage level)
    44.   digitalWrite(pinB,HIGH);   // turn the LED on (HIGH is the voltage level)
    45.   digitalWrite(pinC,HIGH);   // turn the LED on (HIGH is the voltage level)
    46.   digitalWrite(pinD,LOW);   // turn the LED on (HIGH is the voltage level)
    47.   digitalWrite(pinE,LOW);   // turn the LED on (HIGH is the voltage level)
    48.   digitalWrite(pinF,LOW);   // turn the LED on (HIGH is the voltage level)
    49.   digitalWrite(pinG,LOW);   // turn the LED on (HIGH is the voltage level)
    50.   delay(1000);              // wait for a second
    51.  
    52.   //buzzer
    53.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    54.   delay(1000);              // wait for a second
    55.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    56.   delay(1000);              // wait for a second
    57.  
    58.   //2
    59.   digitalWrite(pinA,HIGH);   // turn the LED on (HIGH is the voltage level)
    60.   digitalWrite(pinB,HIGH);   // turn the LED on (HIGH is the voltage level)
    61.   digitalWrite(pinC,LOW);   // turn the LED on (HIGH is the voltage level)
    62.   digitalWrite(pinD,HIGH);   // turn the LED on (HIGH is the voltage level)
    63.   digitalWrite(pinE,HIGH);   // turn the LED on (HIGH is the voltage level)
    64.   digitalWrite(pinF,LOW);   // turn the LED on (HIGH is the voltage level)
    65.   digitalWrite(pinG,HIGH);   // turn the LED on (HIGH is the voltage level)
    66.   delay(1000);              // wait for a second
    67.  
    68.  
    69.   //3
    70.   digitalWrite(pinA,HIGH);   // turn the LED on (HIGH is the voltage level)
    71.   digitalWrite(pinB,HIGH);   // turn the LED on (HIGH is the voltage level)
    72.   digitalWrite(pinC,HIGH);   // turn the LED on (HIGH is the voltage level)
    73.   digitalWrite(pinD,HIGH);   // turn the LED on (HIGH is the voltage level)
    74.   digitalWrite(pinE,LOW);   // turn the LED on (HIGH is the voltage level)
    75.   digitalWrite(pinF,LOW);   // turn the LED on (HIGH is the voltage level)
    76.   digitalWrite(pinG,HIGH);   // turn the LED on (HIGH is the voltage level)
    77.   delay(1000);              // wait for a second
    78.  
    79.   //buzzer
    80.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    81.   delay(1000);              // wait for a second
    82.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    83.   delay(1000);              // wait for a second
    84.  
    85.  
    86.   //4
    87.   digitalWrite(pinA,LOW);   // turn the LED on (HIGH is the voltage level)
    88.   digitalWrite(pinB,HIGH);   // turn the LED on (HIGH is the voltage level)
    89.   digitalWrite(pinC,HIGH);   // turn the LED on (HIGH is the voltage level)
    90.   digitalWrite(pinD,LOW);   // turn the LED on (HIGH is the voltage level)
    91.   digitalWrite(pinE,LOW);   // turn the LED on (HIGH is the voltage level)
    92.   digitalWrite(pinF,HIGH);   // turn the LED on (HIGH is the voltage level)
    93.   digitalWrite(pinG,HIGH);   // turn the LED on (HIGH is the voltage level)
    94.   delay(1000);              // wait for a second
    95.  
    96.          
    97.  
    98.  
    99.   //5
    100.   digitalWrite(pinA,HIGH);   // turn the LED on (HIGH is the voltage level)
    101.   digitalWrite(pinB,LOW);   // turn the LED on (HIGH is the voltage level)
    102.   digitalWrite(pinC,HIGH);   // turn the LED on (HIGH is the voltage level)
    103.   digitalWrite(pinD,HIGH);   // turn the LED on (HIGH is the voltage level)
    104.   digitalWrite(pinE,LOW);   // turn the LED on (HIGH is the voltage level)
    105.   digitalWrite(pinF,HIGH);   // turn the LED on (HIGH is the voltage level)
    106.   digitalWrite(pinG,HIGH);   // turn the LED on (HIGH is the voltage level)
    107.   delay(1000);              // wait for a second
    108.  
    109.   //buzzer
    110.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    111.   delay(1000);              // wait for a second
    112.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    113.   delay(1000);              // wait for a second
    114.  
    115.  
    116.   //6
    117.   digitalWrite(pinA,HIGH);   // turn the LED on (HIGH is the voltage level)
    118.   digitalWrite(pinB,LOW);   // turn the LED on (HIGH is the voltage level)
    119.   digitalWrite(pinC,HIGH);   // turn the LED on (HIGH is the voltage level)
    120.   digitalWrite(pinD,HIGH);   // turn the LED on (HIGH is the voltage level)
    121.   digitalWrite(pinE,HIGH);   // turn the LED on (HIGH is the voltage level)
    122.   digitalWrite(pinF,HIGH);   // turn the LED on (HIGH is the voltage level)
    123.   digitalWrite(pinG,HIGH);    // wait for a second
    124.   delay(1000);
    125.  
    126.  
    127.  
    128.  
    129.   //7
    130.   digitalWrite(pinA,HIGH);   // turn the LED on (HIGH is the voltage level)
    131.   digitalWrite(pinB,HIGH);   // turn the LED on (HIGH is the voltage level)
    132.   digitalWrite(pinC,HIGH);   // turn the LED on (HIGH is the voltage level)
    133.   digitalWrite(pinD,LOW);   // turn the LED on (HIGH is the voltage level)
    134.   digitalWrite(pinE,LOW);   // turn the LED on (HIGH is the voltage level)
    135.   digitalWrite(pinF,LOW);   // turn the LED on (HIGH is the voltage level)
    136.   digitalWrite(pinG,LOW);    // wait for a second
    137.   delay(1000);
    138.  
    139.   //buzzer
    140.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    141.   delay(1000);              // wait for a second
    142.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    143.   delay(1000);              // wait for a second
    144.  
    145.  
    146.  
    147.  
    148.   //8
    149.   digitalWrite(pinA,HIGH);   // turn the LED on (HIGH is the voltage level)
    150.   digitalWrite(pinB,HIGH);   // turn the LED on (HIGH is the voltage level)
    151.   digitalWrite(pinC,HIGH);   // turn the LED on (HIGH is the voltage level)
    152.   digitalWrite(pinD,HIGH);   // turn the LED on (HIGH is the voltage level)
    153.   digitalWrite(pinE,HIGH);   // turn the LED on (HIGH is the voltage level)
    154.   digitalWrite(pinF,HIGH);   // turn the LED on (HIGH is the voltage level)
    155.   digitalWrite(pinG,HIGH);   // turn the LED on (HIGH is the voltage level)
    156.   delay(1000);              // wait for a second
    157.  
    158.  
    159.  
    160.   //9
    161.   digitalWrite(pinA,HIGH);   // turn the LED on (HIGH is the voltage level)
    162.   digitalWrite(pinB,HIGH);   // turn the LED on (HIGH is the voltage level)
    163.   digitalWrite(pinC,HIGH);   // turn the LED on (HIGH is the voltage level)
    164.   digitalWrite(pinD,LOW);   // turn the LED on (HIGH is the voltage level)
    165.   digitalWrite(pinE,LOW);   // turn the LED on (HIGH is the voltage level)
    166.   digitalWrite(pinF,HIGH);   // turn the LED on (HIGH is the voltage level)
    167.   digitalWrite(pinG,HIGH);   // turn the LED on (HIGH is the voltage level)
    168.   delay(1000);              // wait for a second
    169.  
    170.   //buzzer
    171.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    172.   delay(250);              // wait for a second
    173.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    174.   delay(250);              // wait for a second
    175.  
    176.    //buzzer
    177.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    178.   delay(250);              // wait for a second
    179.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    180.   delay(250);              // wait for a second
    181.  
    182.   //buzzer
    183.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    184.   delay(250);              // wait for a second
    185.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    186.   delay(250);              // wait for a second
    187.  
    188.    //buzzer
    189.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    190.   delay(250);              // wait for a second
    191.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    192.   delay(250);              // wait for a second
    193.  
    194.   //buzzer
    195.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    196.   delay(250);              // wait for a second
    197.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    198.   delay(250);              // wait for a second
    199.  
    200.    //buzzer
    201.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    202.   delay(250);              // wait for a second
    203.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    204.   delay(250);              // wait for a second
    205.  
    206.   //buzzer
    207.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    208.   delay(250);              // wait for a second
    209.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    210.   delay(250);              // wait for a second
    211.  
    212.    //buzzer
    213.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    214.   delay(250);              // wait for a second
    215.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    216.   delay(250);              // wait for a second
    217.  
    218.   //buzzer
    219.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    220.   delay(250);              // wait for a second
    221.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    222.   delay(250);              // wait for a second
    223.  
    224.     //buzzer
    225.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    226.   delay(250);              // wait for a second
    227.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    228.   delay(250);              // wait for a second
    229.  
    230.   //buzzer
    231.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    232.   delay(250);              // wait for a second
    233.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    234.   delay(250);              // wait for a second
    235.   //buzzer
    236.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    237.   delay(250);              // wait for a second
    238.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    239.   delay(250);              // wait for a second
    240.  
    241.   //buzzer
    242.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    243.   delay(250);              // wait for a second
    244.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    245.   delay(250);              // wait for a second
    246.  
    247.    //buzzer
    248.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    249.   delay(250);              // wait for a second
    250.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    251.   delay(250);              // wait for a second
    252.  
    253.   //buzzer
    254.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    255.   delay(250);              // wait for a second
    256.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    257.   delay(250);              // wait for a second
    258.  
    259.    //buzzer
    260.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    261.   delay(250);              // wait for a second
    262.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    263.   delay(250);              // wait for a second
    264.  
    265.   //buzzer
    266.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    267.   delay(250);              // wait for a second
    268.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    269.   delay(250);              // wait for a second
    270.  
    271.    //buzzer
    272.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    273.   delay(250);              // wait for a second
    274.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    275.   delay(250);              // wait for a second
    276.  
    277.   //buzzer
    278.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    279.   delay(250);              // wait for a second
    280.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    281.   delay(250);              // wait for a second
    282.  
    283.    //buzzer
    284.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    285.   delay(250);              // wait for a second
    286.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    287.   delay(250);              // wait for a second
    288.  
    289.   //buzzer
    290.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    291.   delay(250);              // wait for a second
    292.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    293.   delay(250);              // wait for a second
    294.  
    295.    //buzzer
    296.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    297.   delay(250);              // wait for a second
    298.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    299.   delay(250);              // wait for a second
    300.  
    301.   //buzzer
    302.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    303.   delay(250);              // wait for a second
    304.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    305.   delay(250);              // wait for a second
    306.  
    307.     //buzzer
    308.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    309.   delay(250);              // wait for a second
    310.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    311.   delay(250);              // wait for a second
    312.  
    313.   //buzzer
    314.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    315.   delay(250);              // wait for a second
    316.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    317.   delay(250);              // wait for a second
    318.   //buzzer
    319.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    320.   delay(250);              // wait for a second
    321.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    322.   delay(250);              // wait for a second
    323.  
    324.   //buzzer
    325.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    326.   delay(250);              // wait for a second
    327.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    328.   delay(250);              // wait for a second
    329.  
    330.    //buzzer
    331.   digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    332.   delay(2000);              // wait for a second
    333.   digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    334.   delay(250);              // wait for a second
    335. }
    336.  
    Moderators note: used code=c tags
     
    Last edited by a moderator: Oct 24, 2015
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,024
    Write your own delay routine that samples the switch and returns a value based on that switch.

    Code (C):
    1. int myDelay(int milliseconds)
    2. {
    3.   int i;
    4.   for (i=0, i<milliseconds;i++){
    5.    delay(1); // delay one millisecond
    6.    if (switch_in_is_active) // if switch is active, stop and return 1 to indicate that
    7.       return 1;
    8.   }// for loop
    9.   return 0; // delay completed without the switch being hit
    10. }// myDelay
    11.  
    Then call myDelay like this in your while loop:
    if(myDelay(1000) == 1) break; // wait for 1 sec, if switch was hit, break out of the while loop

    Note that sampling the switch will impact the accuracy of the delay time. You might want to make the delay have a basic time of 10msec or more then calculate how many times to to call it for the total time.

    You have discovered an inherent problem with dumb delay loops. By consuming ALL of the CPU power to do timing, you can't do anything else (like sample a switch). My proposed solution has its own flaws but is a solution given the inline nature of your code.

    Note that I left the switch sampling code to you. Keep in mind that it will affect your timing. You may find you'll have to tune your delay loop to compensate for the switch input - another bad thing about dumb delays...

    Finally, I don't do Arduino so the syntax may not be correct - I also cheated by using returns directly - but hopefully you get the idea.

    BTW: You should FIX your comments. Obviously block-copied, they do not reflect what the code is doing. That is worse than no comments at all.

    (How's that Joey? :) )
     
    Last edited: Oct 24, 2015
  3. jimmyoooooo

    Thread Starter New Member

    Oct 24, 2015
    3
    0
    sorry for sounding like an idiot but this is one of the first arduino codes i have written would you be able to explain it a bit more or amybe think of a simpler solution sorry and thankyou for your help
     
  4. ScottWang

    Moderator

    Aug 23, 2012
    4,853
    767
    Could you attach the hardware?
     
  5. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,024
    Don't worry about sounding like anything.. You've run into a typical newbie problem.

    First, your problem is using lots of Delay() in a big loop. Those delay times add up to many seconds so even if you put your switch sampling at the end of the loop, you would possibly have to wait many seconds before you got down to sampling the switch.
    You COULD sample the switch after each delay then do a 'break' to exit the while loop if the switch was pushed. 'break' will exit the loop.
    By replacing the standard delay with the one I suggested, you get the delay function AND the switch detect. The function has a return value that indicates whether the delay completed OR the switch was pushed during the delay. Then you know what to do.

    A more involved but better solution would be to separate each step in your sequence into its own function. Call the function to set up the IO and invoke the delay/switch routine. The function does the IO (buzzer etc) then delays while watching the switch and returns a value indicating what happened.

    Chew on that awhile. The original solution proposed is probably the simplest.

    Good luck.
     
  6. jimmyoooooo

    Thread Starter New Member

    Oct 24, 2015
    3
    0
    image1.JPG the switch i need to include is the one in the picture to act as an on off switch then i can neaten up the circuit
     
  7. ScottWang

    Moderator

    Aug 23, 2012
    4,853
    767
    You should attach the schematic circuit, not just the breadboard, the members get used to see the schematic circuit.
     
  8. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    Use mllis It's none blocking. And could you post what you are really trying to do.
    Timer how long?
    Sound alarm how long?
    Turn on when and off when?
    You could write the whole thing in maybe in a 1/3 of what you have there
     
Loading...