I just got my arduino (counterfeit..more on that in a new thread...) today!
i've got a few projects in mind, but i dont have any of the parts yet
so i wanted to play around, to see how much my knowledge of Java and C++ would help me here.
Also, my projects will all involve serial communications with a PC, so knowing how to do this is going to be very important!
So lets get to it, shall we?
I wanted to go above and beyond the hello world (pin 13 LED). So i wrote this.
I call it BlinkSoS. Or blink sauce.
Basically, it listens for new serial packets then if they are any of the standard 26 letters it will blink led 13 as if it were a morse code signaler.
there isint much to it (you dont even have to build a circuit!)
The code is attached below. But for now, can i get some feedback / commentary / criticisms on my code / coding style.. etc?
i want to become a more efficient programmer, so if you guys see ways i can trim the fat, please let me know!
/*
BlinkSos. Goes great on waffles...Mmmm...
A stimple test of serial and LED. since i dont have *any* other componenets to play with right now :/
*/
// SETUP
int ledPin = 13; // The defaults
int Long = 700; // 70% of a second for a —
int Short = 200; // 30% of a second for a · (dot)
int incomingByte = 0;
// Mmm. constructors :)
void setup(){
pinMode(ledPin, OUTPUT); // Duh.
Serial.begin(9600); // Yeah, i'd say this is important.
Serial.flush();
}
void blink(int L){
digitalWrite(ledPin, LOW); // set the LED off incase it wasnt.
delay(100);
digitalWrite(ledPin, HIGH); // set the LED on
delay(L); // wait for l/100 of a second
digitalWrite(ledPin, LOW); // set the LED off
}
void charBlink(int c){
switch (c) {
case 65:
blink(Short);
blink(Long);
return;
case 66:
blink(Long);
blink(Short);
blink(Short);
blink(Short);
return;
case 67:
blink(Long);
blink(Short);
blink(Long);
blink(Short);
return;
case 68:
blink(Long);
blink(Short);
blink(Short);
return;
case 69:
blink(Short);
case 70:
blink(Short);
blink(Short);
blink(Long);
blink(Short);
return;
case 71:
blink(Long);
blink(Long);
blink(Short);
return;
case 72:
blink(Short);
blink(Short);
blink(Short);
blink(Short);
return;
case 73:
blink(Short);
blink(Short);
return;
case 74:
blink(Short);
blink(Long);
blink(Long);
blink(Long);
return;
case 75:
blink(Long);
blink(Short);
blink(Long);
return;
case 76:
blink(Short);
blink(Long);
blink(Short);
blink(Short);
return;
case 77:
blink(Long);
blink(Long);
return;
case 78:
blink(Long);
blink(Short);
return;
case 79:
blink(Long);
blink(Long);
blink(Long);
return;
case 80:
blink(Short);
blink(Long);
blink(Long);
blink(Short);
return;
case 81:
blink(Long);
blink(Long);
blink(Short);
blink(Long);
return;
case 82:
blink(Short);
blink(Long);
blink(Short);
return;
case 83:
blink(Short);
blink(Short);
blink(Short);
return;
case 84:
blink(Long);
return;
case 85:
blink(Short);
blink(Short);
blink(Long);
return;
case 86:
blink(Short);
blink(Short);
blink(Short);
blink(Long);
return;
case 87:
blink(Short);
blink(Long);
blink(Long);
return;
case 88:
blink(Long);
blink(Short);
blink(Short);
blink(Long);
return;
case 89:
blink(Long);
blink(Short);
blink(Long);
blink(Long);
return;
case 90:
blink(Long);
blink(Long);
blink(Short);
blink(Short);
return;
default:
Serial.print("Sorry, i didn't recognize that character ");
Serial.println(c);
}
}
void header(){
Serial.println("Welcome to BlinkSos.");
// more will go here!
}
void loop()
{
// header();
// Serial.println("What character would you like me to put in morese code for you?");
// Serial.println("Please enter a character: ");
// we must make sure we have data available!
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
charBlink(incomingByte);
Serial.println("DONE BLINKING THAT CHARACTER");
delay(400); // wait a second before going on to our next character.
}
}