Getting leds to blink under certain analog reading conditions

Current code looks like

 int redPin = 9; //establishes led lights based on where they are plugged in
int bluePin = 10;
int greenPin = 11;
int sensorPin = 0;	// Force sensor is connected to analog pin 0
int sensorValue = analogRead(A0);	// variable to store the value coming from the sensor
int brightnessRED;
int brightnessBLUE;
int brightnessGREEN;
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 1000; //blink interval
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600); //helps establish the sensor
pinMode(redPin, OUTPUT); //allows the leds to blink
pinMode(bluePin, OUTPUT); 
pinMode(greenPin, OUTPUT); 
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(5000); 
sensorValue = analogRead(sensorPin); 
unsigned long currentMillis = millis();
float voltage = sensorValue * (5.0 / 1023.0); //equation to get voltage reading
float resistance = voltage/
Serial.print("Analog reading is "); //outputs words
Serial.println(sensorValue ); //outputs the sensor reading
Serial.print(" Analog voltage reading is ");
Serial.println(voltage);  //outputs the voltage
Serial.println("(update)"); //used to discern between tests

 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis; 
    
if(sensorValue>500)
{
  digitalWrite(redPin, HIGH); //flashes red light
 digitalWrite (redPin, ! digitalRead (redPin));}

else if(sensorValue>250 && sensorValue<499)
{
  digitalWrite(bluePin, HIGH); //flashes blue light
 digitalWrite (bluePin, ! digitalRead (bluePin));}

else if(sensorValue>0 && sensorValue<249)
{
  digitalWrite(greenPin, HIGH); //flashes green light
 digitalWrite (greenPin, ! digitalRead (greenPin));
}
} }

However, whenever a weight is placed on my fsr, the leds do not respond, the reading is displayed, but no led turns on. How can I fix this?

That is going to turn the LED on for only a few microseconds. If you want the LED to blink, use only the second line:
digitalWrite (bluePin, ! digitalRead (bluePin));

if(sensorValue>500)
{ //flashes red light
 digitalWrite (redPin, ! digitalRead (redPin));}

else if(sensorValue>250 && sensorValue<499)
{ //flashes blue light
 digitalWrite (bluePin, ! digitalRead (bluePin));}

else if(sensorValue>0 && sensorValue<249)
{ //flashes green light
 digitalWrite (greenPin, ! digitalRead (greenPin));

I did that, however now when I put a weight and get analog readings of 480-490, all 3 lights turn on instead of just one. Do i have to specify the other two lights to turn off if I want to avoid this?

Yes.

As someone who still has like no idea what he is doing, how exactly do I write that?
Also the code only responds to higher analog readings. I put a lighter weight on, which got a reading of roughly 150. Maybe my conditionals are written wrong?

To turn off a light:
digitalWrite(LEDpin, LOW);

To turn off 'the other two":

  if(sensorValue>500)
  { 
    //flashes red light
    digitalWrite (redPin, ! digitalRead (redPin));
    digitalWrite(bluePin, LOW);
    digitalWrite(greenPin, LOW);
  }
 int redPin = 9; //establishes led lights based on where they are plugged in
int bluePin = 10;
int greenPin = 11;
int sensorPin = 0;	// Force sensor is connected to analog pin 0
int sensorValue = analogRead(A0);	// variable to store the value coming from the sensor
int brightnessRED;
int brightnessBLUE;
int brightnessGREEN;
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 1000; //blink interval
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600); //helps establish the sensor
pinMode(redPin, OUTPUT); //allows the leds to blink
pinMode(bluePin, OUTPUT); 
pinMode(greenPin, OUTPUT); 
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(5000); 
sensorValue = analogRead(sensorPin); 
unsigned long currentMillis = millis();
float voltage = sensorValue * (5.0 / 1023.0); //equation to get voltage reading
float resistance = voltage/
Serial.print("Analog reading is "); //outputs words
Serial.println(sensorValue ); //outputs the sensor reading
Serial.print(" Analog voltage reading is ");
Serial.println(voltage);  //outputs the voltage
Serial.println("(update)"); //used to discern between tests

 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis; 
    
if(sensorValue>500)
{ //flashes red light
 digitalWrite (redPin, ! digitalRead (redPin));
     digitalWrite(bluePin, LOW);
    digitalWrite(greenPin, LOW);}

else if(sensorValue>250 && sensorValue<499)
{ //flashes blue light
 digitalWrite (bluePin, ! digitalRead (bluePin));
     digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);}

else if(sensorValue>0 && sensorValue<249)
{ //flashes green light
 digitalWrite (greenPin, ! digitalRead (greenPin));
     digitalWrite(bluePin, LOW);
    digitalWrite(redPin, LOW);

Even with the changes, all 3 leds still blink whenever a weight is detected. Maybe there is something wrong somewhere else in the code? I still have no idea, because the conditionals should only allow the blue led to flash on. The code also only responds to readings of roughly 400-500, anything lower or higher just does not turn on any led at all.

 int redPin = 9; //establishes led lights based on where they are plugged in
int bluePin = 10;
int greenPin = 11;
int sensorPin = 0;	// Force sensor is connected to analog pin 0
int sensorValue = analogRead(A0);	// variable to store the value coming from the sensor
int brightnessRED;
int brightnessBLUE;
int brightnessGREEN;
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 1000; //blink interval
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600); //helps establish the sensor
pinMode(redPin, OUTPUT); //allows the leds to blink
pinMode(bluePin, OUTPUT); 
pinMode(greenPin, OUTPUT); 
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(5000); 
sensorValue = analogRead(sensorPin); 
unsigned long currentMillis = millis();
float voltage = sensorValue * (5.0 / 1023.0); //equation to get voltage reading
float resistance = voltage/
Serial.print("Analog reading is "); //outputs words
Serial.println(sensorValue ); //outputs the sensor reading
Serial.print(" Analog voltage reading is ");
Serial.println(voltage);  //outputs the voltage
Serial.println("(update)"); //used to discern between tests

 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis; 
    
if(sensorValue>500)
{ //flashes red light
 digitalWrite (redPin, ! digitalRead (redPin));
     digitalWrite(bluePin, LOW);
    digitalWrite(greenPin, LOW);}

else if(sensorValue>250 && sensorValue<499)
{ //flashes blue light
 digitalWrite (bluePin, ! digitalRead (bluePin));
     digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);}

else if(sensorValue>0 && sensorValue<249)
{ //flashes green light
 digitalWrite (greenPin, ! digitalRead (greenPin));
     digitalWrite(bluePin, LOW);
    digitalWrite(redPin, LOW);

Even with the new code written, all 3 leds still blink at the same time. Also the code only seems to respond to analog readings of 400-500. Anything higher or lower, and it just does not respond. Maybe there is something wrong with my conditionals?

I can only guess that there is a wiring mistake. I added some additional serial output and ran it on a good clone of an Arduino MEGA 2560 and it seems to work as expected.

int redPin = 9;  //establishes led lights based on where they are plugged in
int bluePin = LED_BUILTIN;
int greenPin = 11;
int sensorPin = A0;  // Force sensor is connected to analog pin 0

unsigned long previousMillis = 0;     // will store last time LED was updated
const unsigned long interval = 1000;  //blink interval

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);       //helps establish the sensor
  pinMode(redPin, OUTPUT);  //allows the leds to blink
  pinMode(bluePin, OUTPUT);
  pinMode(greenPin, OUTPUT);
}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    int sensorValue = analogRead(sensorPin);

    Serial.print("Analog reading is ");            //outputs words
    Serial.println(sensorValue);                   //outputs the sensor reading

    if (sensorValue > 500) {  //flashes red light
      digitalWrite(redPin, !digitalRead(redPin));
      Serial.print("RED ");
      Serial.println(digitalRead(redPin));
      digitalWrite(bluePin, LOW);
      digitalWrite(greenPin, LOW);
    } else if (sensorValue > 250) {  //flashes blue light
      digitalWrite(bluePin, !digitalRead(bluePin));
      Serial.print("BLUE ");
      Serial.println(digitalRead(bluePin));
      digitalWrite(redPin, LOW);
      digitalWrite(greenPin, LOW);
    } else {  //flashes green light
      digitalWrite(greenPin, !digitalRead(greenPin));
      Serial.print("GREEN ");
      Serial.println(digitalRead(greenPin));
      digitalWrite(bluePin, LOW);
      digitalWrite(redPin, LOW);
    }
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.