[EDIT - SOLVED but not understood] - In the tutorial (not from here) I was watching, they were using an UNO on PWM pins 6 and 9, and the HIGH and LOW statements worked for them. As I understand it pins 2-13 on the MEGA are PWM, but when I checked the Arduino reference, they said to use...
analogWrite(yellowPin, 255); //for HIGH
analogWrite(yellowPin, 0); //for LOW
I still dont understand why it worked when I had more than 4 LED flashes. I discovered the problem when I tested the voltage and found the output of the HIGH was much lower than the 4.72V my board puts out from the 5V terminal.
---------- original post below------------
I'm going crazy.
Please help.
I have four sections of code that turn two LEDs on and off. This was made because I was trying to learn "For Loops", but when they didnt work I did some tests to see what was going on.
This code works...
int redPin = 9;
int yellowPin = 6;
int redTime = 500;
int yellowTime = 500;
void setup() {
// put your setup code here, to run once:
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
analogWrite(yellowPin, HIGH);
delay(yellowTime);
analogWrite(yellowPin, LOW);
delay(yellowTime);
analogWrite(yellowPin, HIGH);
delay(yellowTime);
analogWrite(yellowPin, LOW);
delay(yellowTime);
analogWrite(redPin, HIGH);
delay(redTime);
analogWrite(redPin, LOW);
delay(redTime);
analogWrite(redPin, HIGH);
delay(redTime);
analogWrite(redPin, LOW);
delay(redTime);
}
But if I delete any one of the sections that look like this, no LEDs light...
analogWrite(yellowPin, HIGH);
delay(yellowTime);
analogWrite(yellowPin, LOW);
delay(yellowTime);
Both the full version, and any version with one of the analogWrite blocks removed compile with no errors (I scrolled up to check).
It doesn't matter which section I remove, it only works with four.
I tried it with four yellows, and it works. With three yellows it doesn't work.
It works with more than four.
I rebooted my system, and the mega 2560.
I started a new project, and retyped it.
I changed pins.
LEDs are connected to PWM pins via a 330 ohm resistor.
I made an LED flash as my "Hello world" first project.
Nothing has worked.
My skills include being able to move slowly forward in time, and if I really concentrate, I can sometimes tell what I'm thinking.
Thanks for the reply.
I'm even more surprised that it ONLY worked when I had more lines of code!
It's been a challenging night.
My skills include being able to move slowly forward in time, and if I really concentrate, I can sometimes tell what I'm thinking.
Don't declare pin numbers with"int", as "int" can change.
Use either "#define" or "const int", because these can't change!
OK, Thanks.
I thought it was strange because people were calling "int" integer, which I presume is correct.
I figured it must have been short for "initialise" or something.
I was wondering how int 6; or int 9; could refer to a pin number given how specific everything else is.
Thanks again.
My skills include being able to move slowly forward in time, and if I really concentrate, I can sometimes tell what I'm thinking.
This is from a purely logical stand point ...
I suspect that it will work with any even number of 'sections' because the pin state is always in the same state next time we loop is iterated.
But if I delete any one of the sections that look like this, no LEDs light...
I was unable to reproduce this statement. I commented out one section of code and it still worked but it should be noted that the LEDs are very dim either way you run the code.
They were dim.
I can't yet read resistors (I write on tem when I buy them), and figured it was just me putting in something with a higher resistance, because when you are making endless blinking LED projects, you can get sick of the brightness. Especially the new ones I bought. Orange and blue hut my old eyes 🙂
Thanks for looking into it.
This learning caper is excellent, but I constantly need to fight against skipping ahead, and not getting a grip on the basics.
I wonder why it was working for the guy on youtube, and all the other people in the comments section. Perhaps it's because my board isn't genuine Arduino Mega
My skills include being able to move slowly forward in time, and if I really concentrate, I can sometimes tell what I'm thinking.