I am new to the Arduino and programming in general and there is a lot I don understand.
The program I am working on I need to use an if statement and either a for, while or do... while statement. I am trying to use a button to turn a circuit on while turning the running circuits of. the code as I have it so far is attached any help would be greatly appreciated. Right no the button operates the lights in the third circuit but does not turn off the other two circuits like I want it to. thank you
int srs = 13; // name series circuit
int prll = 12; // name parallel circuit
int btn = 10; // name the button
int btnst = 0; // button state (still not sure what this does)
int cnt = 0; // initailize count
int bk = 1000; // delay 1 second
void setup() {
// put your setup code here, to run once:
pinMode (srs, OUTPUT); //set series to output
pinMode (prll, OUTPUT); // set parallel to output
pinMode (btn, INPUT); // set button to input
digitalWrite (btn, LOW); // start button in off position
Serial.begin (9600);
}
void loop() {
btnst = digitalRead (btn);
while (btn == HIGH){ //while the button is pressed turun
digitalWrite (srs, LOW); //the other circuits off
digitalWrite (prll, LOW);
}
do {
if (cnt < 3){
Serial.println ("a");
digitalWrite (srs, HIGH); //blink alternativley both circuits
digitalWrite (prll, LOW); //3 three time for 1 second
delay (bk);
digitalWrite (srs, LOW);
digitalWrite (prll, HIGH);
delay (bk);
cnt++;
}else if (cnt < 6){
Serial.println ("b");
digitalWrite (srs, HIGH); // blink alternativley both circuits
digitalWrite (prll, LOW); // 3 times for 1/2 a second
delay (bk/2);
digitalWrite (srs, LOW);
digitalWrite (prll, HIGH);
delay (bk/2);
cnt++;
}else if (cnt < 9){
Serial.println ("c");
digitalWrite (srs, HIGH); // blink alternativley both circuits
digitalWrite (prll, LOW); // 3 times for 1/4 a second
delay (bk/4);
digitalWrite (srs, LOW);
digitalWrite (prll, HIGH);
delay (bk/4);
cnt++;
}else if (cnt < 15){
Serial.println ("d");
digitalWrite (srs, HIGH); // blink alternativley both circuits
digitalWrite (prll, LOW); // 6 times for 1/8 a second
delay (bk/8);
digitalWrite (srs, LOW);
digitalWrite (prll, HIGH);
delay (bk/8);
cnt++;
}else cnt=0;
Serial.println("e");
}while (btn == LOW); // while button is off
}
I am not sure what it is you're asking for. Are you wanting to know if the code does what it's supposed to or whether it is a valid programming flow? To test we would need a schematic.
You might want to simplify the code some as well but that's up to you.
void toggleSrsPrll(int modeSrs, int modePrll, int delayTime) {
digitalWrite (srs, modeSrs);
digitalWrite (prll, modePrll);
delay(delayTime);
}
void loop() {
btnst = digitalRead (btn);
while (btn == HIGH) { //while the button is pressed turun
digitalWrite (srs, LOW); //the other circuits off
digitalWrite (prll, LOW);
}
do {
if (cnt >= 15)
cnt = 0;
else {
if (cnt < 3) {
Serial.println ("a");
toggleSrsPrll(HIGH, LOW, bk);
toggleSrsPrll(LOW, HIGH, bk);
cnt++;
}
else if (cnt < 6) {
Serial.println ("b");
toggleSrsPrll(HIGH, LOW, bk / 2);
toggleSrsPrll(LOW, HIGH, bk / 2);
cnt++;
}
else if (cnt < 9) {
Serial.println ("c");
toggleSrsPrll(HIGH, LOW, bk / 4);
toggleSrsPrll(LOW, HIGH, bk / 4);
cnt++;
}
else if (cnt < 15) {
Serial.println ("d");
toggleSrsPrll(HIGH, LOW, bk / 8);
toggleSrsPrll(LOW, HIGH, bk / 8);
cnt++;
}
}
Serial.println("e");
} while (btn == LOW); // while button is off
}
Thank you caolandix, I am new to all of this and trying to learn as I go. The issue I am having is I want the button to turn off the series and parallel circuits while turning on the third circuit and returning to the loop when the button is released. By schematic do you mean you want a drawing of the whole set up?
rcor78:
Thank you caolandix, I am new to all of this and trying to learn as I go. The issue I am having is I want the button to turn off the series and parallel circuits while turning on the third circuit and returning to the loop when the button is released. By schematic do you mean you want a drawing of the whole set up?
What you have is code that runs in under a millisecond interspersed with full-second delays where nothing is sensed or acted upon, these delays eat over 99% of the time the code runs.
The difference between
code that waits for something by allowing nothing else to run while waiting
and
code that waits for something by checking on the time every so often, allowing everything else to run
is code that can do one thing at a time and code that can do 100's of things at a time.
You can learn the tricks/tools/techniques here, it's like the forum specialty, and then you can make code that flies.
rcor78:
I am trying to use a button to turn a circuit on while turning the running circuits of.
I don't understand that.
If you mean that you want to be able to press a button to interrupt something that is already running then you should get rid of all the delay()s in your code because they block the Arduino from doing other things. You should also avoid using FOR or WHILE loops unless they complete very quickly - in a few microseconds. Use IF and allow loop() to do the repetition.
Have a look at the demo Several Things at a Time to see how it detects button presses while other things are happening. Note how each function runs very briefly and returns to loop() so the next one can be called. Long running processes are achieved a tiny piece at a time. And there may be dozens of calls to some functions before it is actually time for anything to happen.