So, I just started Arduino few days ago. I'm having this fun project of controlling the RGB module with one potentiometer only. Is that possible? Because most people made it with 3 potentiometers, each LED having its own potentiometer. My plan is to have an option on which LED to adjust its value (volt), then after adjusting, I will type "stop" on the serial monitor and this repeats the question allowing you to adjust another LED without turning off the former LED that you just adjusted.This is my unfinished code:
int ReadPin=A0;
int R=3;
int G=5;
int B=6;
int Volt;
int CalcVolt;
String Answer;
String msg1="Which color to adjust?";
String msg2="";
String msg3="";
void setup() {
// put your setup code here, to run once:
pinMode(R,OUTPUT);
pinMode(G,OUTPUT);
pinMode(B,OUTPUT);
pinMode(ReadPin,INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Volt=analogRead(ReadPin);
CalcVolt=(255./1023.)*Volt;
Serial.println(msg1);
while(Serial.available()==0){
}
Answer=Serial.readString();
if (Answer=="Red"){
while (Answer=="Red"){
analogWrite(R,CalcVolt);
}
}
}
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).
When using Cstrings you must use strcmp() to compare values rather than ==
Your program would be simpler if you just send 'S' for stop, 'R' for red, 'G' for green etc.
Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data.
The code to get input from users is often more complex and lengthy than the code that uses the data when it is received. There are so many ways in which the user can mess things up
Your code “doesn’t work” since inside the while loop you are not re-reading the serial input.
Therefore, once Answer=="Red", it cannot possibly change and so the loop will not exit.
There’s also the problem of line endings to consider, unless you have turned them off in serial monitor.
Serial input is not the easiest skill to master for a beginner. I would suggest a different approach: add a button to switch channels. When the button is pressed cycle through the colour being adjusted R->G->B
Please read the post at the start of any forum , entitled "How to use this Forum".
OR http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
If you look at the sticky posts at the top of each forum section there is loads of useful information. Firstly it will tell you how to post on the forum, what information you should give and how to use code tags to display your code in a way that makes it easier for others to help you. You can go back and edit your post to include them.
There are a number of really good tutorials and Robin has pointed out a couple (and written many of them). You will learn there how to use serial input better but, more importantly how to plan your code so that it is broken up into nice logical chunks in functions that are easy to debug, read and change to suit your needs.
Troubleshooting is an integral part of writing code so read the tutorials and make it easy for yourself and for others when you get stuck.