I'm trying to cycle Red-Green-Blue on RGB light but there is a split second where my colors mesh(Ex. when I switch from blue to red in makes magenta for a split second). Is there any way I can get rid of that?
Screenshot - 1c19fee397d7db174b3ed7bc97aa8eb2 - Gyazo this my board and code.
jim-p
October 3, 2024, 2:22pm
2
In the IDE, click on Edit then Copy for Forum, then paste your code here.
I don't see the copy for forum thing on tinkercad. Can I just copy and paste the code manually?
jim-p
October 3, 2024, 2:35pm
4
Yes but you must use <CODE > tags.
Paste where it says
type or paste code here
//Lab 5 Level 3
//October 3rd 2024
int button1Pin = 2; //The SW1 button is connect to pin 2 of the Arduino.
int button2Pin=3;
int timer =500;
int RGBpins[]= {11, 9, 10};
int PinCount = 3;
int count = 0;
void setup() { //The Setup function runs once
pinMode(button1Pin, INPUT); //Setup button1 pin as an input pin.
pinMode(button2Pin,INPUT);
for (int thisPin = 0; thisPin < PinCount; thisPin++){
pinMode(RGBpins[thisPin], OUTPUT);
//setup 2 interupts
attachInterrupt(digitalPinToInterrupt(button1Pin), blink, RISING);
attachInterrupt(digitalPinToInterrupt(button2Pin), stopblink, RISING);
}
}
void loop() { //The loop function runs forever.
if (count==1){
for (int thisPin = 0; thisPin < PinCount; thisPin++){
digitalWrite(RGBpins[thisPin], HIGH);
delay(timer);
digitalWrite(RGBpins[thisPin], LOW);
}
}
}
//make your interupt functions
void blink(){
count=1;
}
void stopblink(){
count=0;
}
Did I do that right? Sorry I'm new to all this
jim-p
October 3, 2024, 2:45pm
6
Yes.
Now we can examine your code but remember, what you see in a tinkercad simulation may not happen in a real circuit and vice versa
You also need to post you tinkercad diagram.
kolaha
October 3, 2024, 2:46pm
7
kelougin:
for (int thisPin = 0; thisPin < PinCount; thisPin++){
pinMode(RGBpins[thisPin], OUTPUT);
//setup 2 interupts
attachInterrupt(digitalPinToInterrupt(button1Pin), blink, RISING);
attachInterrupt(digitalPinToInterrupt(button2Pin), stopblink, RISING);
}
}
do you really want to set interrupts triple?
kmin
October 3, 2024, 2:53pm
9
Add a small delay after digitalwrite LOW.
And move interrupts out from the for loop
In order to get full marks I need to include an interrupt in one of my labs.
kolaha
October 3, 2024, 2:55pm
11
this is not good, connect cathode to GND and use own resistors for each color
You seem to be experiencing "visual persistence" because you are switching colors TOO FAST! Turn one color off and wait for a small time before you turn the other color on.
kolaha
October 3, 2024, 2:57pm
13
you get not more if you set interrupt 6 times. you need actually only 1.
jim-p
October 3, 2024, 3:03pm
15
Try putting a delay after
digitalWrite(RGBpins[thisPin], LOW);
The board is setup the way my teacher set it up though. Also, we just learned interrupts this week so I'm not to comfortable using them. Do I only need to set the interrupt for button 2 so it turns off?
Thank you adding a delay does get rid of the color meshing.
jim-p
October 3, 2024, 3:12pm
18
Try this but don't forget what I said about simulations and real circuits.
//Lab 5 Level 3
//October 3rd 2024
int button1Pin = 2; //The SW1 button is connect to pin 2 of the Arduino.
int button2Pin = 3;
int timer = 750;
int RGBpins[] = {11, 9, 10};
int PinCount = 3;
int count = 0;
void setup() { //The Setup function runs once
pinMode(button1Pin, INPUT); //Setup button1 pin as an input pin.
pinMode(button2Pin, INPUT);
for (int thisPin = 0; thisPin < PinCount; thisPin++) {
pinMode(RGBpins[thisPin], OUTPUT);
}
//setup 2 interupts
attachInterrupt(digitalPinToInterrupt(button1Pin), blink, RISING);
attachInterrupt(digitalPinToInterrupt(button2Pin), stopblink, RISING);
}
void loop() { //The loop function runs forever.
if (count == 1) {
for (int thisPin = 0; thisPin < PinCount; thisPin++) {
digitalWrite(RGBpins[thisPin], HIGH);
delay(timer);
digitalWrite(RGBpins[thisPin], LOW);
delay(timer);
}
}
}
//make your interupt functions
void blink() {
count = 1;
}
void stopblink() {
count = 0;
}
jim-p
October 3, 2024, 3:15pm
19
OK but look at post #18 where I moved the attachinterrupt out of the for loop.
Otherwise, glad it worked.
Have fun!
I do have one more thing I'm trying to figure it. Its the same goal as this but not using an array. Can I ask here or should I make a new post? My cycle is starting at green and I can't figure out why.