I'm trying to do my second lab for an assignment and I essentially have to modify my first lab so it will change what happens to the color order. Ex. in my first lab I have it so the red light comes on first, i click a button and then green light, i click a button then blue in a rgb and prints Red: Green: Blue: in the serial monitor. In my lab 2 I need it so the user determines how the light works so if they input "GBR" it would start with green light and then go red then blue. Can someone point in the right direction?
This is my code for my first lab:
// C++ code
//
int RGBRedPin = 9;
int RGBGreenPin = 10;
int RGBBluePin = 11;
int fadeDelay = 5;
int rotationPin = A0;
int data = 0;
int button = 2;
int state = 0;
int value1 = 0;
int value2 = 0;
int value3 = 0;
void setup()
{
pinMode(RGBRedPin, OUTPUT);
pinMode(RGBGreenPin, OUTPUT);
pinMode(RGBBluePin, OUTPUT);
pinMode(rotationPin, INPUT);
pinMode(button, INPUT);
Serial.begin(9600);
}
void loop()
{
data = analogRead(rotationPin);
data = map(data,0,1023,0,255);
Serial.print("Red: ");
Serial.print(value1);
Serial.print(" Green: ");
Serial.print(value2);
Serial.print(" Blue: ");
Serial.println(value3);
lockbrightness();
setbrightness();
}
void setbrightness(){
if(state == 0){
value1 = data;
analogWrite(RGBRedPin, data);
} if(state == 1){
value2 = data;
analogWrite(RGBGreenPin, data);
} if(state == 2){
value3 = data;
analogWrite(RGBBluePin, data);
}
delay(500);
}
void lockbrightness(){
if(digitalRead(button)==HIGH){
state++;
}
if(state>2){
state=0;
}
}
I'm not asking someone to write my code but I have been trying to figure this out for the last 3 days so I was hoping to just get some direction. I have tried do something like this
// C++ code
//
int RGBRedPin = 9;
int RGBGreenPin = 10;
int RGBBluePin = 11;
int fadeDelay = 5;
int rotationPin = A0;
int data = 0;
int button = 2;
int state = 0;
int value1 = 0;
int value2 = 0;
int value3 = 0;
String incomingbyte = " ";
void setup()
{
pinMode(RGBRedPin, OUTPUT);
pinMode(RGBGreenPin, OUTPUT);
pinMode(RGBBluePin, OUTPUT);
pinMode(rotationPin, INPUT);
pinMode(button, INPUT);
Serial.begin(9600);
Serial.println("Please input here");
}
void loop()
{
data = analogRead(rotationPin);
data = map(data,0,1023,0,255);
if(Serial.available() > 0) {
incomingbyte = Serial.readString();
if(incomingbyte == "R" && "G" && "B")
setbrightness();
delay(50);
Serial.print("Red: ");
Serial.print(value1);
Serial.print(" Green: ");
Serial.print(value2);
Serial.print(" Blue: ");
Serial.println(value3);
}
}
void setbrightness(){
if(state == 0){
value1 = data;
analogWrite(RGBRedPin, data);
} if(state == 1){
value2 = data;
analogWrite(RGBGreenPin, data);
} if(state == 2){
value3 = data;
analogWrite(RGBBluePin, data);
}
delay(500);
}
void lockbrightness(){
if(digitalRead(button)==HIGH){
state++;
}
if(state>2){
state=0;
}
}
But this breaks my light and the print only happens once in the serial monitor. Anyway, I'm just going to take a break for now before I do something stupid and I'll look what DaveX posted after. Thanks for the help.
The code you posted really isn't a very good point of departure for this next lab project.
Who designed the course, as far as the labs and the sequence you are learning things?
This, for example
if (incomingbyte == "R" && "G" && "B") {
is perfectly valid code, and utter nonsense at the same time. I saw on your other thread that you were using interrupts, that makes zero sense when you and your mates don't know about very basic button handling and basic syntax and variable types and so forth.
So I know you are stuck there, but I am curious about what your learning materials consist in.
Write a sketch that lights LEDs in order R, G, B repeating, advancing once per button press no matter how long the button is pressed... advance when the button gets pushed.
That would be a better first step, and better for this second step you are asked to take.
Button handling and state change detection are nice many-times-solved problems that would make you learn some things, even if you never use it, it would force you to learn some things that wil always be necessary to know.
I am also curious as to what level of school this is.
unless another variable of the same name is in scope later and is the state that is seen there.
I tend to leave off explicit initial values when they would be the default (global ints are initially zero), but this is a case where just to be perfectly clear I might leave it as is.
If you deal with multiple languages, sometimes it is better to just use the extra ink, initial values and parentheses and so forth. Rather than try to remember what country you are in and what the customs are.
Me again. @kelougin, you should get in the habit of using the IDE Autoformat tool. It can make seeing the interpretation of you code the compiler will make easier, so you can see missing { braces } and a few other things.
It also makes it easier for us to read. It is the first thingI did to your Lab #1 code.
Eventually good formatting will live in your elbows and be automatic without relying on a tool.
I'm pretty sure my teacher designed the course material and even though its a 3 hour class he only teaches for 1and fucks off. Its a college program. Is that format tool an extension I can get because I'm doing this on website. Also I've only been coding since I started the program so I only really know what I've been taught.
It is ⌥ ⇧ F, and thanks for pointing out that it is even possible! I've been pasting the entirety of posted code into a new sketch in the desktop IDE, CMD-Ting it, so to speak, and copy-pasting it back into wokwi.
Now I have to figure out what ⌥ ⇧ F means .
Oh, Option-Shift-F. Never saw the little staircase thing on the option key.
@kelougin Well, if you did the first exercise by stepping through a string like "RGB" by indexing it as an array of char, you could do the next exercise by receiving a string "GRB" and be done ASAP. Then exercise 3 might handle "RGBGRBGBRBGR" just as easily.
However, even handling that first "RGB" string as an array of characters, including the null terminator, might be well beyond what you've been taught so far, so there is that to contend with.
I think the user is supposed to be able to 'program' a sequence, so a switch/case for individual color channels wouldn't be appropriate.
I think the intention of the class is to do something along the lines of what @camsysca pointed out in #14. Have an array that describes the sequence, and then make that array settable/modifiable by the user through various means.
That's also a way to do it, but readString should work, too. Serial.readString() - Arduino Reference
The last line about cycling through an array with a for-loop is certainly something that OP would likely end up using somewhere, though.
Btw, helping is a great way to learn; don't hesitate to speak up. If you're wrong at anything, don't worry; others will step in and correct.
just the opposite, actually. There's two activities I see - 'getting character', and 'responding to character' - could even be organized as two function calls. My suggestion was focused around the former, but the switch statement provides the latter.
Ok, I see where you're taking this and yes, that can work: you can cycle through the array using the switch/case section to output the correct color channel. I thought @DaveX intended to use the switch/case directly on user input - which is what his code snippet shows. I think that bit is supposed to work differently (insofar as I understand the assignment's purpose, that is). I thought the idea was that the user could input something like "BRG" and then the code would do B, R and G in that order. Of course, this would also raise questions about array length, queueing of consecutive commands etc., but let's maybe not go there just yet...