Sorry for the length, just trying to be as thorough as possible
Hello everyone, I'd first like to start off by saying I'm as new as it gets to this. I'd really like to learn but as this is a school project my deadline is coming up very quickly and this is just one part of a much bigger and complex architectural model I'm working on. However, I've devoted a tremendous amount of time to this and I cannot get it to work for the life of me. I've been looking all over the internet and tried to study other sketches, have tried some cut and paste of a bunch of sketches, etc.
Anyway, what I'm working on is basically a water level sensor. I have a small fish tank set up and will be pumping water into it. I would like an RGB LED to change color based on the height of the water.
For tests, I've been using a potentiometer
Here is my code so far. Also worth noting is I've been working on this for about 2 weeks and have dropped my expectations pretty far as to what I'm hoping for this to do. I've also tried all kinds of small variations to things that I think might be the problem (ie in this latest attempt I used code like "else if(1 < sensorValue < 341)" instead of "else if(sensorValue < 341)" but since I don't have any coding experience I don't know what's right and what's not.
int sensorPin = A0; // Analog input, (using a potentiometer for testing)
int redPin = 11; // Output pins that the LEDs are connected to
int greenPin = 10;
int bluePin = 9;
int sensorValue = 0; // value read from the sensor
int colorValue = 0; // value output to the PWM (analog out) pin
int redValue = 0; // values being output to the LED
int greenValue = 0;
int blueValue = 0;
void setup() {
Serial.begin(9600); // readouts to the serial monitor
pinMode(redPin, OUTPUT); // setting pins as outputs
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin); // reading the value from the sensor (potentiometer for test)
colorValue = map(sensorValue, 0, 1023, 0, 255); // remapping 0-1023 to read as RGB values
redValue = analogRead(redPin); // reads what values go into each of the RGB pins
greenValue = analogRead(greenPin);
blueValue = analogRead(bluePin);
if(sensorValue == 0)
{
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
}
else if(1 < sensorValue < 341)
{
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
}
else if(342 < sensorValue < 682)
{
analogWrite(redPin, 255);
analogWrite(greenPin, 153);
analogWrite(bluePin, 0);
}
else
{
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t");
Serial.print("mapped as = " );
Serial.print(colorValue);
Serial.print("\t");
Serial.print("red = " );
Serial.print(redValue);
Serial.print("\t");
Serial.print("green = " );
Serial.print(greenValue);
Serial.print("\t");
Serial.print("blue = " );
Serial.println(blueValue);
delay(1000);
}
So let me explain, as I mentioned, I'm working on a water level sensor and would like RGB LED to visually display the height of the water based on the following:
When no water in the tank, I would like R=255, G=255, B=255 (white) as I assume the sensor would be reading as 0
I've divided 1023 (analog output max) by 3 (representing low water level, medium water level, high water level). This is where 341 (low) and 682 (medium) come from, with 1023 representing high.
I would like low to be represented as green (0,255,0). Medium as orange (255,153,0). Lastly high as red (255,0,0)
Originally I wanted the colors to fade in and out of each other so I was using the following code where I thought mapping the changes separately would both give the colors I wanted and each water level and fade them in and out of each other.
int sensorPin = A0; // Analog input, water level sensor
int redPin = 11; // Output pins that the LEDs are connected to
int greenPin = 10;
int bluePin = 9;
int sensorValue = 0; // value read from the sensor
int colorValue = 0; // value output to the PWM (analog out) pin
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
void setup() {
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);
redValue = analogRead(redPin);
greenValue = analogRead(greenPin);
blueValue = analogRead(bluePin);
if(sensorValue == 0)
{
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
}
else if(sensorValue <= 341)
{
analogWrite(redPin, map(sensorValue, 1, 341, 255, 0));
analogWrite(greenPin, 255);
analogWrite(bluePin, map(sensorValue, 1, 341, 255, 0));
}
else if(sensorValue <= 682)
{
analogWrite(redPin, map(sensorValue, 342, 682, 0, 255));
analogWrite(greenPin, map(sensorValue, 342, 682, 0, 153));
analogWrite(bluePin, 0);
}
else
{
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}
Serial.print("input = " );
Serial.print(sensorValue);
Serial.print("\t");
Serial.print("red = " );
Serial.print(redValue);
Serial.print("\t");
Serial.print("green = " );
Serial.print(greenValue);
Serial.print("\t");
Serial.print("blue = " );
Serial.println(blueValue);
delay(1000);
}
I've basically been working on them simultaneously hoping one would work. As I mentioned before, I've been trying small changes hoping I'm just not using the right syntax. But after over 2 weeks I've got nothing. Needless to say, my professor isn't happy. Before you start bashing on my professor, lets just please keep this about the code.
Current Setup:
Since I don't know how to draw those fancy diagrams I'll just quickly explain it here.
Using an Arduino Uno
RGB LED is Common Anode, using 220 resistors, ground is going into 3.3V
For testing I'm using a potentiometer (10k I believe), plugged into the 5V
Problem:
I've either been getting no color or shades of purple.
Also the readings I've been getting from the first code I uploaded are when the pot is set to 0 the serial monitor says it's reading (roughly) 100,190,216 which is a dull cyan type of a color, however the LED isn't even on.
When pot is at 341 the serial monitor reads roughly 327,336,334 which shouldn't even be giving me a color (rgb max is 255) but the LED is lit up to a purple.
When pot is at 682 the serial monitor reads roughly 590,533,504 and the LED is still purple
I'll take any help I can get.
Thank you for taking the time to read this in advance.
-E