Frustrated, Frustrated with , " while() "

Hi Guys, I have to admit to myself I will never be a programmer, too complicated for me .
I only wanted the Arduino Board for one project and one project only ( to do with music and keyboard )
I have followed everyones advice and searched past posts for information on how to include " while() " in my code.I have checked with Arduino NoteBook v1-1 but its interpretation and implementation is a little
vague and not enough detail.I have always been a faily logical person, but I feel working with Arduino requires something more than logic, maybe I am not thinking outside the square. Anyhow guys, perhaps you can help me by giveing me ( precisely ) a few lines of of code to keep me with my project. As mentioned in my past posts, I have a simple array of LEDs on, off, etc,at some stage in the code I want to be able to "wait" or "hold" the run , then restart with a pushbutton "input" ( James has suggested I use " while() loop ).This I have Tried with no luck. Here is a part of my code with the bit in the middle missing. Could someone PLEASE fill in the gap.I would be very grateful. Cheers, Chez7.
// Example 01 : Blinking LED
//
// Copy and paste this example into an empty Arduino sketch

#define LED 13 // LED connected to
// digital pin 13

void setup()
{
pinMode(LED, OUTPUT); // sets the digital
// pin as output
}

void loop()
{
digitalWrite(LED, HIGH); // turns the LED on
delay(2000); // waits for a second
digitalWrite(LED, LOW); // turns the LED off
delay(2000); // waits for a second
}
while() loop ; // from here I want to delay the programme until I pushbutton an " input".

Pushbutton input; // After this input, programme cotinues on its run.

Digital Write ( LED, HIGH);
delay (2000);
Etc
Etc

You're looking for a do while.

http://arduino.cc/en/Reference/DoWhile

do {

input = digitalRead(3); //using pin 3 as input

} while (input == 0); //if your button wiring makes it go from 1 to 0 then you will have to switch the 0 to a 1

If you want to learn more about programming look at the reference page: Arduino - Home
The more tools you have in your toolbox the more problems you can solve, and the more projects you will think of.

Thanks " Big Oil", will try your suggestion.

Thanks "KE7GKP" As you say LED on for 2000ms then off for 2000ms then at this stage I want the programme to "Stop" or "Wait" (at my leisure) until I press the input button to carry on the programme.
e,g. LED ON
Pause 2seconds
LED OFF
WAIT
Button input ( to carry on the programme )
LED ON
Pause 2 seconds
Etc
Etc

hi KE&GKP, I understand you cant programme "Etc, Etc" , I was useing it as an abbreviation for the rest of the code. As for pushbutton see below my reply to Big Oil.

Hi Big Oil, Tried your suggestion but got error message "expected unqualified-id before ,do,

Heres my code;

// Example 01 : Blinking LED
//
// Copy and paste this example into an empty Arduino sketch

#define LED 13 // LED connected to
// digital pin 13

void setup()
{
pinMode(LED, OUTPUT); // sets the digital
// pin as output
}

void loop()
{
digitalWrite(LED, HIGH); // turns the LED on
delay(2000); // waits for a second
digitalWrite(LED, LOW); // turns the LED off
delay(2000); // waits for a second
}

do{

input = digitalRead(7); //using pin 7 as input

{while (input == 0); //if your button wiring makes it go from 1 to 0 then you will have to switch the 0 to a 1
{
digitalWrite(LED, HIGH); // turns the LED on
delay(2000); // waits for a second
digitalWrite(LED, LOW); // turns the LED off
delay(2000); // waits for a second
}

Code has to be in functions. I suppose this isn't made totally clear in some of the documentation.

Unfortunately C doesn't have the "function" keyword like some other languages (Eg. VB, Lua).

But this is a function:

void setup()
{
  pinMode(LED, OUTPUT);    // sets the digital
                           // pin as output
}

It is called setup. It starts at the { and ends at the }.

You can't have code outside functions. You need to start a new function.

In Lua it is slightly more self-explanatory:

function setup()
  pinMode(LED, OUTPUT);    -- sets the digital pin as output
end

So your first step is to move the extra stuff inside a function (eg. inside setup or loop).

You also need to declare variables, eg. input is a variable in your case. So you need something like:

int input;

Now the variable input exists.

So this code compiles, notice how I have moved things around so everything is in a function.

#define LED 13   // LED connected to: digital pin 13

void setup()
{
  pinMode(LED, OUTPUT);    // sets the digitat pin as output
}  // end of setup

void loop()
{
  digitalWrite(LED, HIGH);   // turns the LED on
  delay(2000);               // waits for a second
  digitalWrite(LED, LOW);    // turns the LED off
  delay(2000);               // waits for a second

  int input;  // variable to read into

  do
    {
    input = digitalRead(7); //using pin 7 as input
    } 
  while (input == 0);   // end of do loop

}  // end of loop

You need to also pay attention to detail. You had put the squiggly bracket the wrong way around. It's no good "almost" typing in what people suggest.

You could use the "test at the start" method anyway to eliminate the need for a variable, and make it shorter:

#define LED 13   // LED connected to: digital pin 13

void setup()
{
  pinMode(LED, OUTPUT);    // sets the digitat pin as output
}  // end of setup

void loop()
{
  digitalWrite(LED, HIGH);   // turns the LED on
  delay(2000);               // waits for a second
  digitalWrite(LED, LOW);    // turns the LED off
  delay(2000);               // waits for a second

  // loop until pin 7 goes high

  while (digitalRead (7) == LOW)
    { }    // do nothing (except loop)

}  // end of loop

One thing that may not have been made clear is the difference between a "while" loop and a "do..while" loop is that a "while" loop's body may not execute at all, whereas the body of a "do..while" will always execute at least once

Quite right. And this will affect things if you do stuff inside the loop. But the do ... while can be written as:

 do
    {  }   // nothing except loop
  while (digitalRead(7) == LOW);   // end of do loop

So in this particular case, either way will work the same. But I agree that the difference is very relevant in cases where you may not want to do at all, depending on some condition.

When will it be possible to program the arduino in Lua (presumably via a cross compiler)?

Lua itself is interpreted from "bytecodes" which are processor-neutral, so in theory it should be possible. Lua also compiles under any ANSI C compiler (including gcc of course) and in minimal form could be compiled to only use ints or longs (its normal number type is double).

I think you could certainly fit the runtime engine on the Arduino, probably without too much effort. The bigger problem would probably be running out of RAM. With only 2 Kb of SRAM to play with, and with Lua using garbage-collection, you would soon run out unless you collected garbage fairly aggressively, and also kept strings etc. to a minimum.

Still, I believe someone has reprogrammed the Lego Mindstorm NXT "brick" to have Lua firmware instead of the usual NXT Basic, so if it can be done to that it probably can be done on the Arduino.

I found some specs on the NXT brick:

It seems it uses:

Main processor: Atmel® 32-bit ARM® processor, AT91SAM7S256

  • 256 KB FLASH
  • 64 KB RAM
  • 48 MHz

That has somewhat more flash, RAM and speed than the Uno etc. and I remember the author of the Lua firmware saying that "space was tight" to get Lua onto it.

So, it may be a bit impractical. Perhaps for small sketches.

Hi Nic, Thought I was getting on top of it (for about 1 minute) Tried your suggestion and it certainly worked to a degree, however, when I press button it just goes back to the beggining and repeats itself. what I need is for the code to carry on to next section, ie LED 12 to go high, etc, etc. Have tried a couple of options but getting deeper in the mire. Am still thinking too logically. Cheers Chez7.

#define LED 13 // LED connected to: digital pin 13
#define LED 12

void setup()
{
pinMode(LED, OUTPUT); // sets the digitat pin as output
} // end of setup

void loop()
{
digitalWrite(LED13, HIGH); // turns the LED on
delay(2000); // waits for a second
digitalWrite(LED13, LOW); // turns the LED off
delay(2000); // waits for a second

int input; // variable to read into

do
{
input = digitalRead(7); //using pin 7 as input
}
while (input == 0); // end of do loop

} // end of loop
{
digitalWrite(LED12, HIGH); // turns the LED on
delay(2000); // waits for a second
digitalWrite(LED12, LOW); // turns the LED off
delay(2000); // waits for a second

Chez7:
Hi Nic, Thought I was getting on top of it (for about 1 minute) Tried your suggestion and it certainly worked to a degree, however, when I press button it just goes back to the beggining and repeats itself. what I need is for the code to carry on to next section, ie LED 12 to go high, etc, etc. Have tried a couple of options but getting deeper in the mire. Am still thinking too logically. Cheers Chez7.

void loop()

{
 digitalWrite(LED13, HIGH);   // turns the LED on
 delay(2000);               // waits for a second
 digitalWrite(LED13, LOW);    // turns the LED off
 delay(2000);               // waits for a second

int input;  // variable to read into

do
   {
   input = digitalRead(7); //using pin 7 as input
   }
 while (input == 0);   // end of do loop

}  // end of loop



<mark>{</mark>
<mark> digitalWrite(LED12, HIGH); // turns the LED on</mark>
<mark> delay(2000); // waits for a second</mark>
<mark> digitalWrite(LED12, LOW); // turns the LED off</mark>
<mark> delay(2000); // waits for a second</mark>

You have put stuff outside a function again. The "loop" function ends at the squiggly bracket where I put "end of loop".

You have to do your next thing before that squiggly thing.

Also this doesn't make sense:

#define LED 13   // LED connected to: digital pin 13
#define LED 12

You have defined LED as 12 and then changed it to 13. You want something like:

#define FIRST_LED 13   // LED connected to: digital pin 13
#define SECOND_LED 12

void setup()
{
  pinMode(FIRST_LED, OUTPUT);    // sets the digital pin as output
  pinMode(SECOND_LED, OUTPUT);    // and for the second LED
}  // end of setup

void loop()
{
  digitalWrite(FIRST_LED, HIGH);   // turns the LED on
  delay(2000);               // waits for a second
  digitalWrite(FIRST_LED, LOW);    // turns the LED off
  delay(2000);               // waits for a second

  int input;  // variable to read into

  do
    {
    input = digitalRead(7); //using pin 7 as input
    }
  while (input == 0);   // end of do loop


  digitalWrite(SECOND_LED, HIGH);   // turns the LED on
  delay(2000);               // waits for a second
  digitalWrite(SECOND_LED, LOW);    // turns the LED off
  delay(2000);               // waits for a second

}  // end of loop

Hi Guys, Everything works perfectly. Thank you all for your help, advice and patience, especially Nick Gammon. I have learned so much during this post from all your code examples,etc. I have even learned to put the squiggly brackets in the right places and the right way around. Once again, thank you all for all your help, now I can get on to the music. Chez7.