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

Thread Starter

jimmyoooooo

Joined Oct 24, 2015
3
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




C:
  int pinA = 2;
  int pinB = 3;
  int pinC = 4;
  int pinD = 5;
  int pinE = 6;
  int pinF = 7;
  int pinG = 8;

  // the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(pinA, OUTPUT);
  pinMode(pinB, OUTPUT);
  pinMode(pinC, OUTPUT);
  pinMode(pinD, OUTPUT);
  pinMode(pinE, OUTPUT);
  pinMode(pinF, OUTPUT);
  pinMode(pinG, OUTPUT);
  pinMode(1, OUTPUT);

}

// the loop function runs over and over again forever
void loop() {



  //0
  digitalWrite(pinA,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinB,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinC,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinD,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinE,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinF,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinG,LOW);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second




  //1
  digitalWrite(pinA,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinB,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinC,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinD,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinE,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinF,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinG,LOW);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second

  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second

  //2
  digitalWrite(pinA,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinB,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinC,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinD,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinE,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinF,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinG,HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second


  //3
  digitalWrite(pinA,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinB,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinC,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinD,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinE,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinF,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinG,HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second

  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second


  //4
  digitalWrite(pinA,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinB,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinC,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinD,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinE,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinF,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinG,HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second

          


  //5
  digitalWrite(pinA,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinB,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinC,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinD,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinE,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinF,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinG,HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second

  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second


  //6
  digitalWrite(pinA,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinB,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinC,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinD,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinE,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinF,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinG,HIGH);    // wait for a second
  delay(1000);




  //7
  digitalWrite(pinA,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinB,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinC,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinD,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinE,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinF,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinG,LOW);    // wait for a second
  delay(1000);

  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second




  //8
  digitalWrite(pinA,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinB,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinC,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinD,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinE,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinF,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinG,HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second



  //9
  digitalWrite(pinA,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinB,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinC,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinD,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinE,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinF,HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(pinG,HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second

  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

   //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

   //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

   //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

   //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

    //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second
  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

   //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

   //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

   //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

   //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

   //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

    //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second
  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

  //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second

   //buzzer
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(2000);              // wait for a second
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(250);              // wait for a second
}
Moderators note: used code=c tags
 
Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
3,949
Write your own delay routine that samples the switch and returns a value based on that switch.

C:
int myDelay(int milliseconds)
{
  int i;
  for (i=0, i<milliseconds;i++){
   delay(1); // delay one millisecond
   if (switch_in_is_active) // if switch is active, stop and return 1 to indicate that
      return 1;
  }// for loop
  return 0; // delay completed without the switch being hit
}// myDelay
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:

Thread Starter

jimmyoooooo

Joined Oct 24, 2015
3
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
 

JohnInTX

Joined Jun 26, 2012
3,949
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
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.
 

be80be

Joined Jul 5, 2008
1,990
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
 
Top