Very basic programming question

Hi all I am very new to the arduino and have been working throug a couple of tutorial.

I am only at the stage of programming a morse code flasher - after viewing a couple of tutorial I tried one that looked at setting up a function called "flash" in order to create the S O S signal.

In the tutorial we created a function called "flash" that had to be called a certain number of time depending on which letter was desired (IE S O or S).

I tried to extend the idea of creating functions and tried to create a seperate function for each of the 2 letters and then just call the function for each letter.

But it doesn't work.

Here is the code.

int ledPin = 12; // LED connected to digital pin 12 void setup() { pinMode(ledPin, OUTPUT); }

void flash(int duration) { digitalWrite(ledPin, HIGH); delay(duration); digitalWrite(ledPin, LOW); delay(duration); }

void smorse() { flash(200); flash(200); flash(200); }

void omorse() { flash(200); flash(200); flash(200); }

void loop() { smorse; delay(300); omorse; smorse; delay(1000); }

I cant figure out why? Any help please - I don't necessarily want the complete answer, but a pointer as to where I am going wrong would be great!

TIA

John

"smorse;" is not a function call. Use "smorse();"

To call a function, you have to have the parenthesis on the end of it. Without those, it's just a pointer to the function, which by itself on a line is meaningless.

void loop()
{
smorse();
delay(300);
omorse();
smorse();
delay(1000);
}

Now see if you can make it so that you have one function called flashLetter that takes a char as an argument which is the letter you want to flash. So you could call:

void loop()
{
flashLetter('s');
delay(300);
flashLetter('o');
flashLetter('s');
delay(1000);
}

Welcome to the Forum. Most Arduino boards use pin 13, not 12, for the LED. Also, please read Nick Gammon's two posts at the top of this Forum for guidelines on how to properly make a post, especially when posting code. It will help us help you.

Edit: Also, a dash should be 3 times as long as a dot (600). Finally, 2/10 of a second may not be long enough to see the LED change state. Try longer delays to see if that changes things, too.

econkack - sorry about not posting the code in the correct format, I will do so in future.

I am aware that the Arduino uses pin 13 to flash the onboard led (the tutorials all told me to use this). I specifically didnt want the onboard LED to flash, only an externally connected one which is why I chose pin 12. Is there a reason why I shouldn't have done this?

Cheers

John

EDIT - just seen your edit.

I can get the flashing working just fine with the delays used (they are from the original tutorial I used) I just wasn't getting anything at all. Adding the () resolved that side of things. I know that I need to change timings etc. (that's what comes from trying to adapt someone elses code I guess!)

Thanks for your help.

Cheers

John

fredfish:
I am aware that the Arduino uses pin 13 to flash the onboard led (the tutorials all told me to use this). I specifically didnt want the onboard LED to flash, only an externally connected one which is why I chose pin 12. Is there a reason why I should have done this?

There is absolutely no reason whatsoever that you shouldn’t do that. You can use any pin on the board to flash an LED. I’d stay away from 0 and 1 though since that is your serial comms.

I didn't know you had wired a different LED onto the board. Did you use a resistor in the circuit? Also, did you change the delays?

Not sure what this means:

I can get the flashing working just fine with the delays used (they are from the original tutorial I used) I just wasn't getting anything at all.

Is the LED flashing or not?

Yes am I am using a resistor (it is on a breadboard).

What I meant is that after implementing the other suggestions of adding () which I had neglected to do after the function calls the flashing is working fine. I was just trying to explain that the delay values I am using give me distinct flashing patterns. I still need to play around with the different delays to get a proper S O S signal.

The original tutorial that I was trying to change was from the book "30 Arduino Projects for the Evil Genius". I was just trying to point out that the delays used from that tutorial were giving me distinct flash patterns.

I have played around with different values and have found that anything down to about 50 miliseconds gives me a flashing effect.

Cheers

John

There are lots of Morse code programs out there that implement the entire alphabet. Still, see if you can follow this using your algorithm:

//int LEDPIN = 12;
// LED connected to digital pin 12

#define LEDPIN 12
#define DIT  200                          // Now, changing DIT automatically does balanced changes in all
#define DASH  (DIT * 3)
#define LETTERSPACE (DIT * 2)
#define WORDSPACE (DIT * 7)


void setup()
{
  pinMode(LEDPIN, OUTPUT);
  digitalWrite(LEDPIN, LOW);
}

void flash(int duration)
{
  digitalWrite(LEDPIN, HIGH);
  delay(duration);
  digitalWrite(LEDPIN, LOW);
  delay(duration);
}

void Pause(int howLong)
{
  delay(howLong);
}

void loop()
{
  flash(DIT); flash(DIT); flash(DIT);      // Send an 'S'
  Pause(LETTERSPACE);
  flash(DASH); flash(DASH); flash(DASH);   // Send an 'O'
  Pause(LETTERSPACE);
  flash(DIT); flash(DIT); flash(DIT);      // Send an 'S'
  Pause(WORDSPACE);
}

Thanks econjack - I will certainly have a go at the code example that you give.

I am at the stage that any "development and expansion" of my learning is a good thing. I wasn't hugely committed to producing a fully functioning Morse code simulator - except that "getting it right" will teach me a great deal more than getting it wrong!

I know I didn't specify this but the reason behind my initial question was that i had seen a glimpse of how to set up a function but couldn't get it to work. The answers that I received (including yours - thank you) helped me to do that.

The next thing on my radar is to get my head around using arrays as I suspect that his would be a good way to include all of the different code patterns for each letter of the alphabet. But I am not quite there yet - I want to fully understand this stuff first.

Many, many thanks for your help. I feel sure that I will be posting many more questions!!

Cheers

John

Google is your friend: I searched "Arduino arrays in C" and got almost a million hits. The first one is:

http://arduino.cc/en/reference/array

which isn't a bad place to start. There are a bazillion free tutorials on C on the web and just about as many books on C. My totally biased choice is Beginning C for Arduino, for which you can see reviews on Amazon.com. Hang tough...it's worth learning C.