Morse project with function, parameters and switch
SOS code with 3 functions
const byte ledPin = 13;
void setup(){
// Use ledPin as output
pinMode(ledPin, OUTPUT);}
//Create a function for short blink
void shortBlink(){
// Make a single short blink
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);}
void longBlink(){
// Make a single long blink
digitalWrite(ledPin, HIGH);
delay(600);
digitalWrite(ledPin, LOW);
delay(200);
}
void morseBlink(char character) {
/*Morse blink is a function that only depends on one argument or parameters named character ( I choose that name ). This parameter is a variable of char type, char type is a number corresponding to ASCII characters */
// Translate character to Morse code
switch(character){
/* switch is a common function in programing language and is an alternative to the "if, else" statements or instructions equivalent to if character =="s" */
case ‘s‘:
shortBlink();
shortBlink();
shortBlink();
break;
//Break means stop the code, and end of the code.
case ‘o‘:
longBlink();
longBlink();
longBlink();
break;
}
}
void loop() {
// Start blinking SOS
morseBlink(‘s‘);
morseBlink(‘o‘);
morseBlink(‘s’);
}