'blushing' LEDs

Howdy! I’m making a simple circuit with 2 force sensors and 2 LEDs. What I need to happen is to have the LEDs both light up when one or both of the force sensors has pressure applied to it.

Here is my original code using just one force sensor and one LED:

/*

“One is never so dangerous when one has no shame, than when one has grown too old to blush.”
-Marquis de Sade (brainyquote.com)

*/

int ledValue = 0; // LED brightness
int ledPin1 = 9; // LED is connected to digital pin 9
int sensorPin = 0; // force sensor is connected to analog pin 0
int sensorValue = 0; // variable to store the value coming from the sensor

void setup()
{
pinMode(ledPin1, OUTPUT); // sets ledPin1 to be an output
Serial.begin(9600); //initialize the serial port
digitalWrite(ledPin1, HIGH); // turn the LED on
}

void loop() // run over and over again
{
sensorValue = analogRead(sensorPin);
ledValue = map(sensorValue,0,1000,0,255); // set the high and low outputs of the sensor and the LED
analogWrite(ledPin1, ledValue); // turn the LED on at the brightness set by the sensor
Serial.println(sensorValue); // send that value to the computer
delay(100); // delay for 1/10 of a second

if (sensorValue>300){ // if the value coming into the computer is greater than 300
analogWrite(ledPin1, HIGH); // turns on the LED
}

else {
(sensorValue<300);
{ // if the value coming into the computer is less than 300
analogWrite(ledPin1, LOW); // turns off the LEDs
}
}
}

The second LED is attached to pin 5 and the second force sensor is attached to analog pin 1. I can post my attempts at adding a second force sensor and LED if need be however, at this point, my code is more than a little muddled.

Any suggestions would be most welcome!

The code to add before setup() is pretty straightforward. Did you have problems with that?

The code to add in setup() is also pretty straightforward. Did you have problems with that?

In loop, you need to read both force sensors. That’s easy.

   analogWrite(ledPin1, HIGH); // turns on the LED

Actually, this code will turn it almost all the way off, if the LED is connected to a PWM pin, since HIGH has a value of 1. If the LED is not connected to a PWM pin, this code will do nothing.

   (sensorValue<300);

What do YOU think this code is doing?

Post the code that you came up with, and we’ll help you get that working. You’ll learn more than if we just give you the answer.

Thanks!

The setup is okay (I think), it’s the loop that’s got me stuck. Both LEDs are connected to PWM pins. Here is my code thus far:

/*

“One is never so dangerous when one has no shame, than when one has grown too old to blush.”
-Marquis de Sade (brainyquote.com)

*/

int ledValue1 = 0; // LED brightness
int ledValue2 = 1; // LED brightness
int ledPin1 = 6; // LED is connected to digital pin 6
int ledPin2 = 9; // LED is connected to digital pin 9
int sensorPin1 = 1; // force sensor is connected to analog pin 0
int sensorPin2 = 0; // force sensor is connected to analog pin 1
int sensorValue1 = 0; // variable to store the value coming from the sensor
int sensorValue2 = 0; // variable to store the value coming from the sensor

void setup()
{
pinMode(ledPin1, OUTPUT); // sets ledPin1 to be an output
pinMode(ledPin2, OUTPUT); // sets ledPin2 to be an output
Serial.begin(9600); //initialize the serial port
digitalWrite(ledPin1, HIGH); // turn the LED on
digitalWrite(ledPin2, HIGH); // turn the LED on
}

void loop() // run over and over again
{
sensorValue1 = analogRead(sensorPin1);
ledValue1 = map(sensorValue1,0,1000,0,255); // set the high and low outputs of the sensor and the LED
analogWrite(ledPin1, ledValue1); // turn the LED on at the brightness set by the sensor
Serial.println(sensorValue1); // send that value to the computer
delay(100); // delay for 1/10 of a second

sensorValue2 = analogRead(sensorPin2);
ledValue2 = map(sensorValue2,0,1000,0,255); // set the high and low outputs of the sensor and the LED
analogWrite(ledPin2, ledValue2); // turn the LED on at the brightness set by the sensor
Serial.println(sensorValue2); // send that value to the computer
delay(100); // delay for 1/10 of a second

if (sensorValue1>300 || sensorValue1>300){ // if the value coming into the computer is greater than 300
analogWrite(ledPin1, HIGH); // turns on the LED
analogWrite(ledPin2, HIGH); // turns on the LED
}

else (sensorValue1<300 || sensorValue2<300); {// if the value coming into the computer is less than 300
analogWrite(ledPin1, LOW); // turns off the LED
analogWrite(ledPin2, LOW); // turns off the LED
}
}

led1 blinks when I press on the force sensor (not good) and led2 ‘blushes’ (good). They’re still only lighting up individually when I need them to both light up regardless of what sensor is being touched.

What I thought was doing was telling the LEDs to ‘blush’ if the value from one or both sensors was within a certain range.

Not so apparently. Suggestions?

Some things I would change. The output from analogRead is a value between 0 and 1023. Currently, if the sensor DID output a value greater than 1000, you would map it to a value greater than 255, and then use that value in a call to analogWrite, which would not produce the desired result.

You could change the from range in map, to 0 to 1023, or you could note that there are 1024 possible outputs from analogRead and 256 possible inputs to analogWrite, and simply divide the output from analogRead by 4 to create the input to analogWrite.

 digitalWrite(ledPin1, HIGH);      // turn the LED on
 digitalWrite(ledPin2, HIGH);      // turn the LED on

Why do you turn the LEDs on at the start?

Why are you using analogWrite() with values of HIGH and LOW? HIGH and LOW should be used digitalWrite. Why, after setting the partially lit state, do you then want to turn the LEDs off? This is effectively what you are doing with analogWrite and HIGH (1) or LOW (0).

Both sensors should be read, and both LEDs set, BEFORE any delay occurs.

Thanks for all your help PaulS.

I’ve made a few edits and here’s what I have now:

/*

“One is never so dangerous when one has no shame, than when one has grown too old to blush.”
-Marquis de Sade (brainyquote.com)

*/

int ledValue1 = 0; // LED brightness
int ledValue2 = 0; // LED brightness
int ledPin1 = 6; // LED is connected to digital pin 6
int ledPin2 = 9; // LED is connected to digital pin 9
int sensorPin1 = 1; // force sensor is connected to analog pin 1
int sensorPin2 = 0; // force sensor is connected to analog pin 0
int sensorValue = 0; // variable to store the value coming from the sensor

void setup()
{
pinMode(ledPin1, OUTPUT); // sets ledPin1 to be an output
pinMode(ledPin2, OUTPUT); // sets ledPin2 to be an output
Serial.begin(9600); //initialize the serial port
}

void loop() // run over and over again
{
sensorValue = analogRead(sensorPin1 || sensorPin2);
ledValue1 = map(sensorValue,0,1023,0,255); // set the high and low outputs of the sensor and the LED
ledValue2 = map(sensorValue,0,1023,0,255); // set the high and low outputs of the sensor and the LED
analogWrite(ledPin1, ledValue1); // turn the LED on at the brightness set by the sensor
analogWrite(ledPin2, ledValue2); //turn the LED on at the brightness set by the sensor
delay(100); // delay for 1/10 of a second

if (sensorValue>300){ // if the value coming into the computer is greater than 300
digitalWrite(ledPin1 && ledPin2, HIGH); // turns on the LED
}
else (sensorValue<300); {// if the value coming into the computer is less than 300
digitalWrite(ledPin1 && ledPin2, LOW); // turns the LEDs
}
}

Everything works except for the fact that the LEDs are only reacting to the sensor connected to analog pin 1.

Does this have something to do with one of my ‘||’ statements?

Does this have something to do with one of my '||' statements?

Yes, it does.

Please explain what you would like this statement:

 sensorValue = analogRead(sensorPin1 || sensorPin2);

to do.

(Whatever it is, that's not what it's doing.)

This:

ledValue1 = map(sensorValue,0,1023,0,255);

is equivalent to:

ledValue1 = sensorValue/4;

but takes longer.

Please explain what you would like this statement:  digitalWrite(ledPin1 && ledPin2, HIGH); // turns on the LED to do.

(Whatever it is, that's not what it's doing.)