Notifications
Clear all

Strange behaviour when I delete one of four identical sections of code  

  RSS

BullwinkleII
(@bullwinkleii)
Active Member
Joined: 9 months ago
Posts: 12
2020-02-26 4:23 pm  

[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.

This topic was modified 8 months ago 2 times by BullwinkleII

My skills include being able to move slowly forward in time, and if I really concentrate, I can sometimes tell what I'm thinking.


Quote
Topic Tags
Pugwash
(@pugwash)
Prominent Member
Joined: 1 year ago
Posts: 997
2020-02-26 6:19 pm  

@bullwinkleii

I am surprised that your code worked at all!

You should not use HIGH or LOW with analogWrite(), only with digitalWrite().

This is how it should have been coded:

#define redPin 9
#define 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, 255);
  delay(yellowTime);
  analogWrite(yellowPin, 0);
  delay(yellowTime);
  
  analogWrite(yellowPin, 255);
  delay(yellowTime);
  analogWrite(yellowPin, 0);
  delay(yellowTime);
  
  analogWrite(redPin, 255);
  delay(redTime);
  analogWrite(redPin, 0);
  delay(redTime);
  
  analogWrite(redPin, 255);
  delay(redTime);
  analogWrite(redPin, 0);
  delay(redTime);

}

Or alternatively use this:

#define redPin 9
#define 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:

  digitalWrite(yellowPin, HIGH);
  delay(yellowTime);
  digitalWrite(yellowPin, LOW);
  delay(yellowTime);
  
  digitalWrite(yellowPin, HIGH);
  delay(yellowTime);
  digitalWrite(yellowPin, LOW);
  delay(yellowTime);
  
  digitalWrite(redPin, HIGH);
  delay(redTime);
  digitalWrite(redPin, LOW);
  delay(redTime);
  
  digitalWrite(redPin, HIGH);
  delay(redTime);
  digitalWrite(redPin, LOW);
  delay(redTime);

}

 


ReplyQuote
BullwinkleII
(@bullwinkleii)
Active Member
Joined: 9 months ago
Posts: 12
2020-02-26 6:25 pm  

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.


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 1 year ago
Posts: 997
2020-02-26 6:29 pm  

@bullwinkleii

Don't declare pin numbers with"int", as "int" can change.

Use either "#define" or "const int", because these can't change!


ReplyQuote
BullwinkleII
(@bullwinkleii)
Active Member
Joined: 9 months ago
Posts: 12
2020-02-27 12:39 am  

@pugwash

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.


ReplyQuote
MarkPritchett
(@markpritchett)
New Member
Joined: 8 months ago
Posts: 2
2020-02-27 4:22 am  

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.


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 1 year ago
Posts: 997
2020-02-27 9:41 am  

@bullwinkleii

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.

 


ReplyQuote
BullwinkleII
(@bullwinkleii)
Active Member
Joined: 9 months ago
Posts: 12
2020-02-28 9:34 am  

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 

 

This post was modified 8 months ago by BullwinkleII

My skills include being able to move slowly forward in time, and if I really concentrate, I can sometimes tell what I'm thinking.


ReplyQuote