Phototransistor coding, help for a beginner!

As you will be able to tell from my code I am very new to this but I think what I'm trying to achieve is very simple.

When the value of the sensor is above 100, I want the RGB LED to glow red.

When the value of the sensor is below 100, I want the RGB LED to glow blue for 2 seconds and then glow green until the value is above 100 again.

This code seems to work, but once it turns green it then doesn't react to any light changes and the serial monitor stops recording values.

Please help!

Thank you!

PHOTOTRANSISTOR.ino (856 Bytes)

This is a "forever loop":

while (sensorValue < sensorThres) {
    setColor(0, 0, 255); // Green Color
    }

aarg:
This is a "forever loop":

while (sensorValue < sensorThres) {

setColor(0, 0, 255); // Green Color
   }

oh I see thanks so much!

what should I use instead?

What did you do for red and blue?

They are both if statements, but if I made that while statement an if statement too then the if statement for green and blue would be the same.

This is another version of the code I tried but then it just flicks between blue and green. I need it to stay green until the red statement is true again.

int sensorValue;

const int red = 9;      // LED connected to digital pin 9
const int blue = 10;    // LED connected to digital pin 10
const int green = 11;   // LED connected to digital pin 11

int sensorThres = 100;

void setColor(int redValue, int blueValue, int greenValue) {
analogWrite(red, redValue);
analogWrite(blue, blueValue);
analogWrite(green, greenValue);
}

void setup() {
pinMode(red, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
Serial.begin(9600);
}

void loop(){

sensorValue = analogRead(A5);
Serial.println(sensorValue);

if (sensorValue > sensorThres) {
    setColor(255, 0, 0); // Red Color
    } 
    
if (sensorValue < sensorThres) {
    setColor(0, 255, 0); // Blue Color
    delay(2000);
    setColor(0, 0, 255); // Green Color
    delay(1000);
    }
}

You have posted code using quote tags instead of the proper code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower right corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

thank you!

Sorry new to both arduino and forums!

Like this?

void loop(){

sensorValue = analogRead(A5);
Serial.println(sensorValue);

if (sensorValue > sensorThres) {
    setColor(255, 0, 0); // Red Color
    } 
    
if (sensorValue < sensorThres) {
    setColor(0, 255, 0); // Blue Color
    delay(2000);
    setColor(0, 0, 255); // Green Color
    }
}

So with that code you never actually see the green light as it flashes back to blue so quickly. I need it to stay green until the red if statement is true.

if (sensorValue > sensorThres) {
    setColor(255, 0, 0); // Red Color
    }
    
if (sensorValue < sensorThres) {
    setColor(0, 255, 0); // Blue Color
    delay(2000);
    setColor(0, 0, 255); // Green Color
    delay(1000);
    }