Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is most likely no translated for the post!





Assistance with do-...
 
Share:
Notifications
Clear all

Assistance with do-while loop


(@rdiizz)
Active Member
Joined: 4 months ago
Posts: 9
Topic starter  

I have a trouble, basically i have a code and i have to rewrite it with ''do'' and ''while''.

can somebody help me ? or atleast give some hints :D

It's basically about 3 leds and press button, for my school arduino project.

int buttonState = 0;

void setup()
{
pinMode(2, INPUT);
pinMode(8, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
}

void loop()
{
buttonState = digitalRead(2);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
if (buttonState == HIGH) {
digitalWrite(8,LOW);

for(int i=0; i<3;i=i+1){
digitalWrite(10,HIGH);
delay(3000);
digitalWrite(10,LOW);
delay(3000);}

digitalWrite(9, HIGH);
delay(1000);
}// else {
// digitalWrite(8, HIGH);
// digitalWrite(9, LOW);
// digitalWrite(10, LOW);
// }
}

ReplyQuote
Topic Tags
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1767
 

Can you explain what the code is supposed to be doing?

Note: it would be good to use constants/defines instead of absolute numbers. This way you can right away see what pin is used for what, and if that needs to be changed, then you can do that in just one spot. (example code blow - as you can see: it also improves readability)

#define BUTTON 2
#define LED1 8
#define LED2 9
#define LED3 10

void setup() { pinMode(BUTTON, INPUT); pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT); pinMode(LED3, OUTPUT); } void loop() { buttonState = digitalRead(BUTTON); digitalWrite(LED1, HIGH); digitalWrite(LED2, LOW); if (buttonState == HIGH) { digitalWrite(LED1,LOW); for(int i=0; i<3;i=i+1){ digitalWrite(LED3,HIGH); delay(3000); digitalWrite(LED3,LOW); delay(3000);
} digitalWrite(LED2, HIGH); delay(1000); }// else { // digitalWrite(LED1, HIGH); // digitalWrite(LED2, LOW); // digitalWrite(LED3, LOW); //
} }

 From what I'm reading in the code:

- LED1 on, LED2 off (LED3???)
- if BUTTON pressed then LED1 off, 2x LED3 on/off (3 seconds each).
- LED2 on
- Wait a second
- repeat

I'm not seeing where the do-while loop should be?

 Tip: I wrote a little intro to Arduino programming. There is a Do-While section here that explains the use of a do-while loop.


ReplyQuote
(@rdiizz)
Active Member
Joined: 4 months ago
Posts: 9
Topic starter  

Hello, ill take look  intho that for sure, tnx :)


ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: