Go Down

Topic: Morse Code With for loop help needed (Read 1 time) previous topic - next topic

Daniel1407

int GreenLED = 12;   
int RedLED = 11;

void setup()                     
{
 pinMode(GreenLED, OUTPUT);
 pinMode(RedLED, OUTPUT);
}


void loop(int times)
{
for(int i=0; itimes; i++)
{
  dash(1); dot(2); blank(1); //D
  dot(1); dash(1); blank(1); //A
  dash(1); dot(1); blank(1); //N
  dot(2); blank(1); //I
  dot(1); blank(1); //E
  dot(1); dash(1); dot(2); blank(1); //L
}
}
 

int dash(int times) {
    digitalWrite(GreenLED, HIGH);
    delay(750); 
    digitalWrite(GreenLED, LOW);   
    delay(750);                       
}

int dot(int times){
    digitalWrite(RedLED, HIGH);   
    delay(250);                   
    digitalWrite(RedLED, LOW);   
    delay(250);                   
}

int blank(int times) {
    digitalWrite(GreenLED, LOW);
    digitalWrite(RedLED, LOW);
      delay(1000);
}


Grumpy_Mike

#1
Oct 19, 2017, 01:28 am Last Edit: Oct 19, 2017, 01:30 am by Grumpy_Mike
Why do you pass a number to the dash, dot and blank functions and never use it?
Why do you declare these functions as returning an int and never return anything?

When you say it doesn't run, do the lights flash at all? If not then your wiring is wrong. If it flashes noncense then that is because it is doing exactly what your code says, which is wrong.

Daniel1407

The program does not even verify and i recieve message like this:

arduino-builder/arduino-builder -compile -core-api-version 10611 -build-path /tmp/219822155/build -hardware arduino-builder/hardware -hardware arduino-builder/packages/cores -tools arduino-builder/tools -tools arduino-builder/packages/tools -built-in-libraries arduino-builder/latest -libraries /tmp/219822155/pinned -libraries /tmp/219822155/custom -fqbn arduino:avr:uno -build-cache /tmp -logger humantags -verbose=false -logger humantags /tmp/219822155/Morse_Code_Challenge
/tmp/ccO5MQPZ.ltrans0.ltrans.o: In function `main':
/home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/main.cpp:46: undefined reference to `loop'
collect2: error: ld returned 1 exit status
exit status 1

Daniel1407

Code: [Select]
int GreenLED = 12;   
int RedLED = 11;

void setup()                     
{
 pinMode(GreenLED, OUTPUT);
 pinMode(RedLED, OUTPUT);
}


void loop()
{
int times;
for(int i=0; i<times; ++i)
{
  dash(1); dot(2); blank(1); //D
  dot(1); dash(1); blank(1); //A
  dash(1); dot(1); blank(1); //N
  dot(2); blank(1); //I
  dot(1); blank(1); //E
  dot(1); dash(1); dot(2); blank(1); //L
return times;
}
}
 

int dash(int times) {
    digitalWrite(GreenLED, HIGH);
    delay(750); 
    digitalWrite(GreenLED, LOW);   
    delay(750);
    return(times);
}

int dot(int times){
    digitalWrite(RedLED, HIGH);   
    delay(250);                   
    digitalWrite(RedLED, LOW);   
    delay(250);
    return(times);
}

int blank(int times) {
    digitalWrite(GreenLED, LOW);
    digitalWrite(RedLED, LOW);
    delay(1000);
    return(times);
}




I don't know how to use the numbers in the brackets after specific commands.
The program verifies but the specified LED's don't even light up.
If somebody could help me it would be a great help?

Grumpy_Mike

That is because you are defining a variable to be passed to the loop function and the part of the code calling it ( this is hidden from you ) is not sending one.

No code you have seen in any of the examples in the IDE does that so why have you?

Daniel1407


Grumpy_Mike

How can i fix the program??

Correct the things I have told you about.

slipstick

If you don't know how to use "numbers in the brackets" why have you written them in? What do you think they are doing?

BTW if you're going to define a variable like "times" to control a for loop it's a good idea to initialise to some known value. And when something is defined as void don't try to return a value from it.

Steve

Grumpy_Mike

#8
Oct 20, 2017, 09:26 am Last Edit: Oct 20, 2017, 09:30 am by Grumpy_Mike
The code in reply#3 will do nothing because the default value for the variable Times is zero hence you do things zero times in each loop.

Now come clean, did you write this code? You seem to be using concepts that you have no idea about.
There is no need to put the code in a for loop because the loop function keeps being called. That is a fundamental piece of elementary knowledge that you don't seem to know.

Quote
I don't know how to use the numbers in the brackets
Then why did you put them in your code?

We are used to all sorts of beginners here but you come across as not genuine, a bit of a troll so to speak. A bit like some one pretending not to know stuff, but not knowing much about how beginners think and act.

Daniel1407

Yea i wrote the code but i don't gethow to assign a value to to brackets. This is for a school project.

Grumpy_Mike

The code dot(2 ) calls the function dot and passes to it the value of 2.
In the definition of the dot(2 ) function their is a variable defined:-
int dot(int times){
It is that variable called times which is an integer that is set to the value of 2 from that call. However you don't actually use that variable so it is a bit useless in your code. I suspect you want to use it to set a number of repeats of the light flashing in that function. To do this you will have to use a for loop to repeat the light flashing.
Like

Code: [Select]

void dot(int times){
  for(int i=0; i<times) {
    digitalWrite(RedLED, HIGH);   
    delay(250);                   
    digitalWrite(RedLED, LOW);   
    delay(250);
  }
}

Note define the function as void, that means it does not return a value, and remove the return line because you are not doing anything with the returned value you have.

Make those changes in all your functions, and remove the for loop from the loop function, just leave its contents.


Go Up