Go Down

### Topic: Praticing using "while loops" (Read 663 times)previous topic - next topic

#### Mikey42787

##### Oct 13, 2013, 12:40 am
I am making a binary clock and have tried to exercise different loops. Here is my logic...

I have 4 stored integers

a ( for units seconds) = 0
b ( for tens of seconds) = 0
c ( for units minuets) = 0
d ( for tens of minuets) = 0

The flow chart I have devised goes as so.

INT A-----> [ START COUNTING 1-9]
1
2
3
4
5
6
7
8
9
[ HAS A REACHED 9?]
l
l
Y  E S                     NO-----------------------Return to top of loop and retest.
l
ADD ONE TO INT B AND RETEST (A) CONDITION----------------------------
l
INT B-----> [ START COUNTING 1-6]
1
2
3
4
5
6
[ HAS B REACHED 6]
l
l
Y  E S                      NO-----------------------Return to top of loop and retest.
l
l
ADD ONE TO C AND RE TEST LOOP

and so on etc etc

I have A counting 1-9 and B going to 1 once A is reached 9 . Im now confused on how to get from A to B while recounting A and having it remember B in a loop that will let me display up to 59 with both columns. I have to do it with WHILE LOOPS. Here is my code.

Code: [Select]
`// Binary clockint a = 0;int b = 0;int c = 0;int d = 0;void setup (){  pinMode(0, OUTPUT);  pinMode(1, OUTPUT);  pinMode(2, OUTPUT);  pinMode(3, OUTPUT);  pinMode(4, OUTPUT);  pinMode(5, OUTPUT);  pinMode(6, OUTPUT);  pinMode(7, OUTPUT);  pinMode(8, OUTPUT);  pinMode(10, OUTPUT);  pinMode(11, OUTPUT);  pinMode(12, OUTPUT);  pinMode(13, OUTPUT);  digitalWrite(0, HIGH);  digitalWrite(1, HIGH);  digitalWrite(2, HIGH);  digitalWrite(3, HIGH);  digitalWrite(4, HIGH);  digitalWrite(5, HIGH);  digitalWrite(6, HIGH);  digitalWrite(7, HIGH);  digitalWrite(8, HIGH);  digitalWrite(9, HIGH);  digitalWrite(10, HIGH);  digitalWrite(11, HIGH);  digitalWrite(12, HIGH);  digitalWrite(13, HIGH);}void loop (){  start:  while (a < 10)  {    // 1 bit    digitalWrite(0, LOW);    digitalWrite(1, HIGH);    digitalWrite(2, HIGH);    digitalWrite(3, HIGH);    delay(1000);    a++;    // 2 bit     digitalWrite(0, HIGH);    digitalWrite(1, LOW);    digitalWrite(2, HIGH);    digitalWrite(3, HIGH);    delay(1000);    a++;    //3 bit    digitalWrite(0, LOW);    digitalWrite(1, LOW);    digitalWrite(2, HIGH);    digitalWrite(3, HIGH);    delay(1000);    a++;    // 4 bit    digitalWrite(0, HIGH);    digitalWrite(1, HIGH);    digitalWrite(2, LOW);    digitalWrite(3, HIGH);    delay(1000);    a++;    // 5 bit    digitalWrite(0, LOW);    digitalWrite(1, HIGH);    digitalWrite(2, LOW);    digitalWrite(3, HIGH);    delay(1000);    a++;    // 6 bit    digitalWrite(0, HIGH);    digitalWrite(1, LOW);    digitalWrite(2, LOW);    digitalWrite(3, HIGH);    delay(1000);    // 7 bit    digitalWrite(0,LOW);    digitalWrite(1,LOW);    digitalWrite(2,LOW);    digitalWrite(3,HIGH);    delay(1000);    a++;    // 8 bit    digitalWrite(0, HIGH);    digitalWrite(1, HIGH);    digitalWrite(2, HIGH);    digitalWrite(3, LOW);    delay(1000);    a++;    // 9 bit    digitalWrite(0, LOW);    digitalWrite(1, HIGH);    digitalWrite(2, HIGH);    digitalWrite(3, LOW);    delay(1000);    a=0;    if    (a == 9)    {     b++;    }    break;  }     while (b < 6)    {      digitalWrite(7, LOW);      delay(1000);      digitalWrite(8, L      {        goto start;      }    }}`

#### PaulS

#1
##### Oct 13, 2013, 12:45 am
Code: [Select]
`  start:`
This is a mistake. It's almost a sure indication that you plan to use the completely unnecessary goto statement.

Code: [Select]
`      digitalWrite(8, L`
What's this supposed to be?

Code: [Select]
`      {        goto start;      }`
Yep. Here is the useless goto in useless curly braces.

You don't seem to understand that loop() is called in an endless loop, and thar trying to circumvent that is a bad idea.

#### Mikey42787

#2
##### Oct 13, 2013, 12:48 am
Yes, I put a goto in the bottom to jump to the top immediately. I am new to this as you can see.
The digitalWrite 7, L is just me not finishing the line. I understand the loop is a never ending my flow. My boss has assigned I  do this while using the WHILE loop. Advise on how to tie it together.

#### Nick Gammon

#3
##### Oct 13, 2013, 12:51 am
Code: [Select]
`   while (b < 6)    {      digitalWrite(7, LOW);      delay(1000);      digitalWrite(8, L      {        goto start;      }    }`

Huh? This doesn't make any sense at all. While b is less than 6 (and b is not changed inside that loop) immediately leave the loop?

Rewrite without using goto.

Quote

My boss has assigned I  do this while using the WHILE loop.

I hope he also said not to use goto.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

#### Nick Gammon

#4
##### Oct 13, 2013, 12:54 am
Code: [Select]
`  pinMode(0, OUTPUT);  pinMode(1, OUTPUT);  pinMode(2, OUTPUT);  pinMode(3, OUTPUT);  pinMode(4, OUTPUT);  pinMode(5, OUTPUT);  pinMode(6, OUTPUT);  pinMode(7, OUTPUT);  pinMode(8, OUTPUT);  pinMode(10, OUTPUT);  pinMode(11, OUTPUT);  pinMode(12, OUTPUT);  pinMode(13, OUTPUT);`

First tip. Replace that with:

Code: [Select]
`  for (int i = 0; i <= 13; i++)    pinMode (i, OUTPUT);`

Now tackle those digitalWrite function calls.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

#### CrossRoads

#5
##### Oct 13, 2013, 01:05 am
Why didn't you set the other unused IO pins, D14 to D19, to High outputs as well?

Code: [Select]
`void loop(){  currentMillis = millis();  while ( (currentMillis - nextMillis)>=1000){    // the time test condition has passed    nextMillis = nextMillis + 1000; //set up for the next one    // increment & rollover the "digits"    a=a+1;    if (a==10){      a=0;      b=b+1;      if (b==6){        b=0;        c=c+1;        if (c==10){          c=0;          d=d+1;          if (d==6){            d==0;          }        }      }    }    // write your outputs    switch (a){    case 0:      // 1 bit      digitalWrite(0, LOW);      digitalWrite(1, HIGH);      digitalWrite(2, HIGH);      digitalWrite(3, HIGH);      break;    case 1:      // etc.    } // end A    switch (b){    case 0:      // 1 bit      digitalWrite(0, LOW);      digitalWrite(1, HIGH);      digitalWrite(2, HIGH);      digitalWrite(3, HIGH);      break;    case 1:      // etc.    } // end B    switch (c){    case 0:      // 1 bit      digitalWrite(0, LOW);      digitalWrite(1, HIGH);      digitalWrite(2, HIGH);      digitalWrite(3, HIGH);      break;    case 1:      // etc.    } // end C    switch (d){    case 0:      // 1 bit      digitalWrite(0, LOW);      digitalWrite(1, HIGH);      digitalWrite(2, HIGH);      digitalWrite(3, HIGH);      break;    case 1:      // etc.    } // end D  } // end while  // do other stuff outside of the while} // end loop`
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### Mikey42787

#6
##### Oct 13, 2013, 01:09 am
@ Cross roads thank you that was very informative, @ Gammon, I did not know I could shorten the outputs like that. Ive lost to GOTO function now.

#### Jiggy-Ninja

#7
##### Oct 13, 2013, 01:56 am

My boss has assigned I  do this while using the WHILE loop.

Why?

That's beyond micromanaging; a dictate like that should be classified as nanomanaging.

I could see a point in forcing it to be implemented a certain way for a school assignment, but not for a work product. It makes no sense.

#### Nick Gammon

#8
##### Oct 13, 2013, 05:25 am
He might have spotted the goto.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up

Please enter a valid email to subscribe

### Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy