Go Down

Topic: sos light using while statements (Read 1 time) previous topic - next topic

Z3R0_C001

hi new member and im just starting to learn about the arduino's capability's.

I was looking at the blinking light tutorial and thought I ought to make something like it but a little more complex.

Code: [Select]
/*
By: Z3R0 C001
Date: Sunday, Dec, 13, 2009
Discription: A morse code SOS program using while statements.
*/

int ledpin = 13; //LED CONNECTED TO PIN 13
int count = 0; // first interger to count slow blinks
int selo = 0; //second int to count fast blinks

void setup()  {

 //makes the pin13 the output
 pinMode(ledpin, OUTPUT);
}

void loop()
{
label:
 while (count < 1) /* this while loop makes the pause between the last quick flash to the first long flash the same length as the rest of the pauses between the other long flashes */

 {    
   delay(300);
   digitalWrite(ledpin, HIGH); //turns led pin on
   delay(900);
   digitalWrite(ledpin, LOW);
   delay(500);
   ++count;
 }
 while (count >= 1 && count < 3) // second and third long flash
 {
   ++count;
   digitalWrite(ledpin, HIGH); //turns led pin on
   delay(900);
   digitalWrite(ledpin, LOW);
   delay(500);
 }
 while (count = 3 && selo < 3) // short flashes
 {
   ++selo;
   digitalWrite(ledpin, HIGH); //turns led pin on
   delay(200);
   digitalWrite(ledpin, LOW);
   delay(200);
 }
 
 count = 0; //resetting the variables
 selo = 0;
 goto label; //starts the while loops over
}


now i was trying to make this simpler and much smaller but i gave up after a bit   :D

any suggestions?

PaulS

Well, for one thing, loop is called in an infinite loop, so the label and the goto statement are useless.

Second, you did not describe what the problem is. What is working? What is not working?

PaulS

A suggestion. Create two functions, dot and dash.

Make dot flash the led quickly, and wait for the between signals time.

Make dash flash the led for longer, and wait for the between signals time.

Then, create functions for letters. S should call dot and/or dash as appropriate, in the correct order. O should do likewise.

Then, loop can look like this:

Code: [Select]
void loop()
{
  S();
  O();
  S();
  delay(1000); // Wait for help
}

Z3R0_C001

Well unfortunately the loop code did not work for some reason that is why i used the goto because i tried to run it on my arduino but it did not work.

I like your suggestion but i haven't gotten as far as that yet
but it sounds like a good thing to to next.

thanks!

Z3R0_C001

to your question about what is and is not working the program is now working but i was wondering if there was a cleaner shorter way.

PaulS

Quote
i was wondering if there was a cleaner shorter way.


The use of subroutines/functions to group related commands/behavior is the cleanest way to write code. It is not necessarily the shortest, but short code tends to be obscure code, and obscure code tends to be hard to maintain. Lots of head-scratching, wondering "why did I (they) do that?".

Z3R0_C001

well thank you for your help im looking into your method now

Go Up