Morse Code lights problem with delay and organization

wrote this to have two lights apologize to each other in Morse code. (As of right now just using LED’s soon to move to relay switches wired in to 120VAC. ) (I know electricity, not electronics/programming and safety is part of my routine)

r1 starts off by saying, " I’m Sorry"

r2 interjects around the end of r1’s Sorry and says “No, I’m Sorry.”

The sequence then loops indefinitely

I’m having trouble with the lights holding on a little too long on the HIGH and LOW. I understand this is due to using delay () but I’m not sure how to remedy this.
Also, I know there has to be an easier way to write this all out, at least a more condensed form.

I’m brand new to all of this so I’m sorry if I don’t understand what it is youre trying to say. Really dumb it down for me if you can.

Thanks, this arduino is awesome and I’m excited to be learning more.

int r1=8; //led pin 8
int r2=4; //led pin 4

 
void setup()
{
pinMode (r1,OUTPUT);
pinMode (r2, OUTPUT);
}
void loop()
 { 

  
  //digitalWrite(r1, HIGH);
  //delay (200);   dit for r1
  
  //digitalWrite(r1, HIGH);
  //delay(800); dah for r1
  
  //digitalWrite(r1, LOW);
  //delay (200);  space for r1
  
  ////digitalWrite(r2, LOW);
  //delay (400);  space between letter for r2
  
  
  
  //digitalWrite(r2, HIGH);
  //delay (200);   dit for r2
  
  //digitalWrite(r2, HIGH);
  //delay(800); dah for r2
  
  //digitalWrite(r2, LOW);
  //delay (200);  space for r2
  
  ////digitalWrite(r2, LOW);
  //delay (400);  space between letter for r2
  
  //Space between words is 800 delay
  
  
  //I
    digitalWrite(r1, HIGH);
delay (200);
  
  digitalWrite(r1, LOW);
  delay (200);
  
   digitalWrite(r1, HIGH);
  delay (200);
  
  
  //end I
  
  digitalWrite(r1, LOW);
  delay (400);
  
  //M
  digitalWrite(r1, HIGH);
  delay(800);
  digitalWrite(r1, LOW);
  delay (200);
  digitalWrite(r1, HIGH);
  delay(800);
  
  //end M
  
  digitalWrite(r1, LOW);
  delay (800);
  
  //S
  
  digitalWrite(r1, HIGH);
  delay (200);
  
  digitalWrite(r1, LOW);
  delay (200);
  
   digitalWrite(r1, HIGH);
  delay (200);
  
  digitalWrite(r1, LOW);
  delay (200);
  
   digitalWrite(r1, HIGH);
  delay (200);
  
  //End S
  
  digitalWrite(r1, LOW);
  delay (400);
  
  //O
   digitalWrite(r1, HIGH);
  delay(800);
  digitalWrite(r1, LOW);
  delay (200);
  digitalWrite(r1, HIGH);
  delay(800);
   digitalWrite(r1, LOW);
  delay (200);
  digitalWrite(r1, HIGH);
  delay(800);
  //End O
  
  
  digitalWrite(r1, LOW);
  delay (400);
  
  
  //R
  digitalWrite(r1, HIGH);
  delay (200);
  digitalWrite(r1, LOW);
  delay (200);
   digitalWrite(r1, HIGH);              
  delay(800);  
  digitalWrite(r1, LOW);
  delay(200);
  digitalWrite(r1, HIGH);  
  delay(200);
  
 //End R
 
 digitalWrite(r1, LOW);
  delay (400);
  
    //R
  digitalWrite(r1, HIGH);
  delay (200);
  digitalWrite(r1, LOW);
  delay (200);
   digitalWrite(r1, HIGH);
  delay(800);
  digitalWrite(r1, LOW);
  delay (200);
  digitalWrite(r1, HIGH);
  delay (200);
   digitalWrite(r1, LOW);
   //End R                              
                                     //N
   
                            digitalWrite(r2, HIGH);              
                                delay(800);
                            digitalWrite(r2, LOW);
                                delay (200);
                            digitalWrite(r2, HIGH);
                                delay (200);
                            
                  
                              
 {                               //end N
  
  digitalWrite(r2, LOW);
  digitalWrite(r1, LOW);
  delay (400);
 }
                              //O
                            digitalWrite(r2, HIGH);
                                delay (800);
                             digitalWrite(r2, LOW);
                               delay (200);
                             digitalWrite(r2, HIGH);
                               delay(800);
                             digitalWrite(r2, LOW);
                               delay(200);
  //Y
  {{
  if (r1, HIGH)
  {digitalWrite (r2, HIGH);
  
  }
  digitalWrite (r1, HIGH);
  delay(800); //dah
  digitalWrite (r2, LOW);
  digitalWrite(r1, LOW);
  delay (200);
  }
 }
  digitalWrite(r1,HIGH);
  delay (200);  //dit
  
  
  digitalWrite(r1, LOW);    
  delay (200); 
  {
if (r1, LOW)
{digitalWrite(r2, HIGH);
delay (200);}

  digitalWrite(r1, HIGH);
  delay(800);  //dah    
  }


delay(200);  //dah
  digitalWrite(r1, LOW);
  delay (200); 
  digitalWrite(r1, HIGH);
  delay(800);             //dah
 digitalWrite (r1, LOW);
 //End Y-1
 
 
 
 
 
 
 
 
 
 
                                     //2-I
    digitalWrite(r2, HIGH);
delay (200);
  
  digitalWrite(r2, LOW);
  delay (200);
  
   digitalWrite(r2, HIGH);
  delay (200);
  
  
  //end 2-I
  
  digitalWrite(r2, LOW);
  delay (400);
  
  //2-M
  digitalWrite(r2, HIGH);
  delay(800);
  digitalWrite(r2, LOW);
  delay (200);
  digitalWrite(r2, HIGH);
  delay(800);
  
  //end 2-M
  
  digitalWrite(r2, LOW);
  delay (800);
  
  //2-S
  
  digitalWrite(r2, HIGH);
  delay (200);
  
  digitalWrite(r2, LOW);
  delay (200);
  
   digitalWrite(r2, HIGH);
  delay (200);
  
  digitalWrite(r2, LOW);
  delay (200);
  
   digitalWrite(r2, HIGH);
  delay (200);
  
  //End 2-S
  
  digitalWrite(r2, LOW);
  delay (400);
  
  //2-O
   digitalWrite(r2, HIGH);
  delay(800);
  digitalWrite(r2, LOW);
  delay (200);
  digitalWrite(r2, HIGH);
  delay(800);
   digitalWrite(r2, LOW);
  delay (200);
  digitalWrite(r2, HIGH);
  delay(800);
  //End 2- O
  
  
  digitalWrite(r2, LOW);
  delay (400);
  
  
  //2-Ra
  digitalWrite(r2, HIGH);
  delay (200);
  digitalWrite(r2, LOW);
  delay (200);
   digitalWrite(r2, HIGH);              
  delay(800);  
  digitalWrite(r2, LOW);
  delay(200);
  digitalWrite(r2, HIGH);  
  delay(200);
  
 //End Ra
 
 digitalWrite(r2, LOW);
  delay (400);
  
    //R-2b
  digitalWrite(r2, HIGH);
  delay (200);
  digitalWrite(r2, LOW);
  delay (200);
   digitalWrite(r2, HIGH);
  delay(800);
  digitalWrite(r2, LOW);
  delay (200);
  digitalWrite(r2, HIGH);
  delay (200);
 
   //End R-2b        
   digitalWrite(r2, LOW);
   delay(400);
   
   
  //Y-2
  digitalWrite (r2, HIGH);
  delay(800); //dah
  digitalWrite (r2, LOW);
  delay (200);
  digitalWrite(r2,HIGH);
  delay (200);  //dit
  digitalWrite(r2, LOW);    
  delay (200); 
  digitalWrite(r2, HIGH);
  delay(800);  //dah    
  digitalWrite(r2, LOW);
  delay (200); 
  digitalWrite(r2, HIGH);
  delay(800);    //dah
 digitalWrite (r2, LOW);
  
  //end apology
  delay (1500);
}

morse.mp4 (2.75 MB)

if (r1, HIGH)

What do you think this is doing? It isn't, but fixing it depends on what you think it is doing.

Where are the code tags you were supposed to use?

There is no suppose to use. I'm kind of figuring things out along the way. From what I understand I though that was saying that when r1 is high it was making r2 high as well. I included a video with my results so far. I really am completely new at this, so I am happy I've gotten what I have, but I know there are other and or better ways to do what I am doing.

there are many Morse examples here and they almost always boil themselves down to function calls for the dash and dot...

you should try to search the forum for morse and see how many great examples there are

You are supposed to use code tags. Read How to post code properly and fix up your first post otherwise I, for one, won't help.

Pete

There is no suppose to use.

What?

From what I understand I though that was saying that when r1 is high it was making r2 high as well.

If you mean that you thought is was comparing the value of r1 to HIGH, it is not. if(r1 == HIGH) would. But, since r1 is a pin number, and 8 is not 1, that test would never be true. It looks like you are trying to read the state of r1 and compare that to HIGH, but there is no function call to read the state of the pin...

I'm having trouble with the lights holding on a little too long on the HIGH and LOW

It is OK to be new and asking simple questions. I have to answer by asking a question though - what do you think the number inside the delay() is doing, and what happens if you change it?

Also, I know there has to be an easier way to write this all out, at least a more condensed form

Oh yes there is. It is called writing your own function. Your comments nicely say "dah" and "dit" - so that is what we want: A function that does a "dah" and one for "dit".

void dahr1() { // dah for r1 
digitalWrite(r1, HIGH);
  delay (400);   
}

Then you do all 8 combinations on & off combinations... and your main code (we'll skip the other error(s), as PausS already has pointed that one out) simple becomes ditr1() ; spcr1(); ditr1() ; spcr1(); dahr1() ; wrdr1() ;and so on.

I'd optimize that as there must be some space after every dit or dah, and include that in the dit and dah functions. As the word-space always will follow a characterspace, we can just add the etra as the word space.

Lastly, there is no need to write one set of functions for r1 and on set of function for r2. After all, there is only one digitalWrite() and it is good for all pins - you include which pin is written to. The same with your dit and dah function. The syntax for inclduiog this in your own functions is

void dit(byte pin) {
  digitalWrite(pin,HIGH) ;
  delay(200) ;  // Dit
  digitalWrite(pin,LOW);
  delay(200);  // small space
}

And now you only have a dit, dah and wrd - only 3 functions. Your main code simplifies to dit(r1); dah(r1); wrd(r1);and so on. You can write a whole morse character on one line.

Msquare:

It is OK to be new and asking simple questions. I have to answer by asking a question though - what do you think the number inside the delay() is doing, and what happens if you change it?

My understanding is that the delay is holding that command for a certain amount of time in milliseconds. Like delay (1000) would mean the command holds for 1 second.

Oh yes there is. It is called writing your own function. Your comments nicely say "dah" and "dit" - so that is what we want: A function that does a "dah" and one for "dit".

void dahr1() { // dah for r1 
digitalWrite(r1, HIGH);
  delay (400);   
}

Then you do all 8 combinations on & off combinations... and your main code (we'll skip the other error(s), as PausS already has pointed that one out) simple becomes ditr1() ; spcr1(); ditr1() ; spcr1(); dahr1() ; wrdr1() ;and so on.

I'd optimize that as there must be some space after every dit or dah, and include that in the dit and dah functions. As the word-space always will follow a characterspace, we can just add the etra as the word space.

Lastly, there is no need to write one set of functions for r1 and one set of function for r2. After all, there is only one digitalWrite() and it is good for all pins - you include which pin is written to. The same with your dit and dah function. The syntax for including this in your own functions is

void dit(byte pin) {
  digitalWrite(pin,HIGH) ;
  delay(200) ;  // Dit
  digitalWrite(pin,LOW);
  delay(200);  // small space
}

And now you only have a dit, dah and wrd - only 3 functions. Your main code simplifies to dit(r1); dah(r1); wrd(r1);and so on. You can write a whole morse character on one line.

is byte pin the name of the pin in this case?

Thank you. I'm just trying to figure things out. This is some neat stuff; I'm just having a hard time understanding the vernacular/ jargon.

My understanding is that the delay is holding that command for a certain amount of time in milliseconds.

Yes. I was asking, because you wrote "holding on a little too long " - so one simply adjusts the delay. As you know that, I am now wondering f you asked something else and I misunderstood the question.

is byte pin the name of the pin in this case?

Yes. specifically when you write a function the first word (void in this case) says what the function returns (like digitalRead() returns something, but delay() does not). Void means we are not returning anything. Then comes the name of the function, we used "dit" as the example. Inside the brackets you can zero, one or many arguments. Here we have one. For each argument you need two words, the first is the type ( byte, char, long ...) the second word is the name you want to use.

For all the other neat ways to use functions in C or C++ read some tutorials, books or google.

And yes, I can think of plenty of ways to improve and reorganize the code, but you can do this one thing at a time, gradually as your knowledge gets better, and still have a working device. at all intermediare stages. One note of "warning"(?) - the use of delay() limits this device, it will need a different approach if you want to read buttons or otherways make it interactive. Keep up the good hobby !