2

This is my first program on Arduino Uno (the starter kit), although I do have prior experience with programming (JavaScript, coding class last year 7th grade). It blinks the internal led (pin 13) in a Morse Code message:

.... . .-.. .-.. --- .-- --- .-. .-.. -..

. = dit(); 
- = dah(); 
(interval between parts of letter) = wip(); 
(interval between letters/words) = wah();

Here's my code:

/*
 * Roman's first program
 * Decipher the message!
*/
int ledPin = 13;
int index = 0;
void setup() {
 //initialize pins as outputs
 pinMode(ledPin, OUTPUT);
 Serial.begin(9600);
}
void loop() {
 int sequence[] = {1,0,1,0,1,0,1,3,1,3,1,0,2,0,1,0,1,3,1,0,2,0,1,0,1,3,2,0,2,0,2,3,3,3,1,0,2,0,2,3,2,0,2,0,2,3,1,0,2,0,1,3,1,0,2,0,1,0,1,3,2,0,1,0,1};
 int arrLength = sizeof(sequence)/sizeof(int); //however THIS works?!
 if (index < arrLength-1) { //0-based array
 index++;
 } else {
 index = 0; //restart the sequence once finished
 }
 if (sequence[index] == 0) {
 wip();
 } else if (sequence[index] == 1) {
 dit();
 } else if (sequence[index] == 2) { 
 dah();
 } else if (sequence[index] == 3) { //because sometimes I the values are screwed past arrLength...
 wah();
 }
 //Serial.println(String(sequence[index])+" , "+String(index));
}
void dit () {
 digitalWrite(pinMode, HIGH); 
 delay(300); 
 digitalWrite(pinMode, LOW);
}
void dah () {
 digitalWrite(pinMode, HIGH); 
 delay(900); 
 digitalWrite(pinMode, LOW);
}
void wip () {
 delay(900);
}
void wah () {
 delay(2100);
}

Yet nothing is happening, except for the four/five startup flashes on the led when I restart it (red button). And yes, I saved and compiled.

asked Aug 28, 2016 at 16:07

1 Answer 1

6

Well, mainly digitalWrite(pinMode, HIGH); looks suspicious. I though you called LED pin as ledPin

If you fix this, it should be working. As this one is working too, it's just simplified a little:

int ledPin = 13;
int index = 0;
const int8_t sequence[] = {1,0,1,0,1,0,1,3,1,3,1,0,2,0,1,0,1,3,1,0,2,0,1,0,1,3,2,0,2,0,2,3,3,3,1,0,2,0,2,3,2,0,2,0,2,3,1,0,2,0,1,3,1,0,2,0,1,0,1,3,2,0,1,0,1};
int arrLength = sizeof(sequence);
void setup() {
 pinMode(ledPin, OUTPUT);
 Serial.begin(57600);
}
void loop() {
 Serial.println(String(sequence[index])+" , "+String(index));
 switch (sequence[index++]) {
 case 0: wip(); break;
 case 1: dit(); break;
 case 2: dah(); break;
 case 3: wah(); break;
 }
 if (index == arrLength) {
 index = 0;
 }
}
void dit () {
 digitalWrite(ledPin, HIGH); 
 delay(300); 
 digitalWrite(ledPin, LOW);
}
void dah () {
 digitalWrite(ledPin, HIGH); 
 delay(900); 
 digitalWrite(ledPin, LOW);
}
void wip () {
 delay(900);
}
void wah () {
 delay(2100);
}
answered Aug 28, 2016 at 16:18
3
  • Thank you! But, I'm pretty new to C code. Could you explain const int8_t or should I learn later? Also, you changed the baud...? Commented Aug 28, 2016 at 17:54
  • @Roman int is likely to be 16 or 32 bits (can't recall off the top of my head). IIRC the size of int may vary depending on the compiler you're using. Using int8_t guarantees an array of 8 bit ints, saving quite a bit of space. You're likely to have plenty of space at first, but eventually you'll write a large program and need to squeeze out every wasted byte you can just to fit it on the mcu. Commented Aug 29, 2016 at 1:13
  • Specifying 'const' informs the compiler that these variables are not meant to be changed (once initialized) and enables the compiler to check for and enforce that. Commented Aug 29, 2016 at 12:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.