Pages: [1]   Go Down
Author Topic: sos light using while statements  (Read 1169 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
 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   smiley-grin

any suggestions?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void loop()
{
   S();
   O();
   S();
   delay(1000); // Wait for help
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?".
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: