Lighting LEDs with a FSR Project Help

Hello,

I am in real need of some help I am a beginner doing some Arduino for a University project. I need to set up something just like the links below, a way of showing FSR can detect different levels of force.

http://www.hackerscapes.com/2014/11/lighting-multiple-leds-with-sensors-force-sensitive-resistor-or-potentiometer/

I have copied the circuit diagram I believe accurately to create the image attached. I have copied the code accurately but when I upload it this error comes up, can anyone help me with this?

Error
‘gt’ was not declared in this scope

Thanks

here is a screen shot of the error

Post the code you have.

The code in that tutorial is not right. I have never seen an if statement like that. I suspect that the syntax is wrong. Also the gf, amp and lt variables are not declared so the error that you post about is not the only one.

Here is a working example of controlling 3 LEDs with a FSR. I replaced the 220 Ohm resistor with a 10K resistor to get a wider range of variables with my FSR.

const byte redPin = 9;
const byte bluePin = 10;
const byte yellowPin = 11;
const byte sensorPin = A0;


void setup()
{
   Serial.begin(115200);
   pinMode(redPin, OUTPUT);
   pinMode(bluePin, OUTPUT);
   pinMode(yellowPin, OUTPUT);
}

void loop()
{
   int sensorValue = analogRead(sensorPin); // read sensor
   if (sensorValue >= 0 && sensorValue <= 199) // all off
   {
      digitalWrite(redPin, LOW);
      digitalWrite(bluePin, LOW);
      digitalWrite(yellowPin, LOW);
   }
   else if (sensorValue >= 200 && sensorValue <= 599)  // red on
   {
      digitalWrite(redPin, HIGH);
      digitalWrite(bluePin, LOW);
      digitalWrite(yellowPin, LOW);
   }
   else if (sensorValue >= 600 && sensorValue <= 799) // blue on
   {
      digitalWrite(redPin, LOW);
      digitalWrite(bluePin, HIGH);
      digitalWrite(yellowPin, LOW);
   }
   else if (sensorValue >= 800 && sensorValue <= 1023) // yellow on
   {
      digitalWrite(redPin, LOW);
      digitalWrite(bluePin, LOW);
      digitalWrite(yellowPin, HIGH);
   }

}

This is my Code:

/* Test 1 typing out code from source */

int sensorPin = 0; int ledPin1 = 9; int ledPin2 = 10; int ledPin3 = 11; int sensorValue = 0;

void setup() { Serial.begin(9600); pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); }

void loop() { sensorValue = analogRead(sensorPin); Serial.print("Force value = "); Serial.println(sensorValue);

if(sensorValue > 0 && sensorValue <= 500) { digitalWrite(ledPin3, HIGH); } else digitalWrite(ledPin3, LOW); }

Thank you - this code has uploaded without any problems but it does not work with my current circuit (see photo of original post) what do I need to change to get it to work?

groundFungus:
The code in that tutorial is not right. I have never seen an if statement like that. I suspect that the syntax is wrong. Also the gf, amp and lt variables are not declared so the error that you post about is not the only one.

Here is a working example of controlling 3 LEDs with a FSR. I replaced the 220 Ohm resistor with a 10K resistor to get a wider range of variables with my FSR.

const byte redPin = 9;

const byte bluePin = 10;
const byte yellowPin = 11;
const byte sensorPin = A0;

void setup()
{
  Serial.begin(115200);
  pinMode(redPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
}

void loop()
{
  int sensorValue = analogRead(sensorPin); // read sensor
  if (sensorValue >= 0 && sensorValue <= 199) // all off
  {
      digitalWrite(redPin, LOW);
      digitalWrite(bluePin, LOW);
      digitalWrite(yellowPin, LOW);
  }
  else if (sensorValue >= 200 && sensorValue <= 599)  // red on
  {
      digitalWrite(redPin, HIGH);
      digitalWrite(bluePin, LOW);
      digitalWrite(yellowPin, LOW);
  }
  else if (sensorValue >= 600 && sensorValue <= 799) // blue on
  {
      digitalWrite(redPin, LOW);
      digitalWrite(bluePin, HIGH);
      digitalWrite(yellowPin, LOW);
  }
  else if (sensorValue >= 800 && sensorValue <= 1023) // yellow on
  {
      digitalWrite(redPin, LOW);
      digitalWrite(bluePin, LOW);
      digitalWrite(yellowPin, HIGH);
  }

}

I know that the code works. I have wired the circuit on my breadboard and am running the code on my Uno.

It is hard to tell but it looks like the LEDs are installed backwards. If you look closely at the led you will see that at the bottom of the LED plastic there is a flat bit on one side. That is the negative side and must connect to ground either directly or through the resistor. Another way to tell is the longer leg of the LED is the positive side and should connect to 5V through the resistor or directly if the resistor is on the negative side. Turn the LEDs 180 degrees and try it.

75d32c2a600870305d2537e50b2a8cdc2a6e0d6f.jpg

How to post images.

hello thank you I have them round the correct way yet it still does not work have I used the wrong resistors or are they the wrong way round? to LED they are Brown Black Black Orange Orange then to the FSR Brown Gold Black Black Brown

groundFungus: ...I replaced the 220 Ohm resistor with a 10K resistor to get a wider range of variables with my FSR...

I don't know much about those FSRs but the OP has just 10 ohms (as instructed in the hackerscapes link). Can that be right?

I don't know much about those FSRs but the OP has just 10 ohms (as instructed in the hackerscapes link). Can that be right?

No that can't be right. I misread the resistance (old eyes ain't what they used to be), but I did say in reply #3 to replace that resistor with a 10K resistor.

I replaced the 220 Ohm resistor with a 10K resistor to get a wider range of variables with my FSR.

DESIGN CHALLENGE

Hello I need some help I have no real experience with electronics / Arduino. I need to create something which I can press (FSR) and the harder the force applied the more LEDs light up. It would also be good if you could explain how to alter the levels of force required to light each LED so I can alter this to best fit my prototype.

I have purchased these: https://www.amazon.co.uk/gp/product/B01DGD2GAO/ref=ppx_yo_dt_b_asin_title_o00__o00_s00?ie=UTF8&psc=1

https://www.amazon.co.uk/gp/product/B076Q1BP4Y/ref=ppx_yo_dt_b_asin_title_o00__o00_s00?ie=UTF8&psc=1

Please could someone post or create a tutorial for this which has the code and a circuit diagram so I can easily create this as a complete newbie.

Thank!

Here is code that I wrote for another member a couple of days ago. It lights one of 3 LEDs based on pressure on an FSR.

const byte redPin = 9;
const byte bluePin = 10;
const byte yellowPin = 11;
const byte sensorPin = A0;


void setup()
{
   Serial.begin(115200);
   pinMode(redPin, OUTPUT);
   pinMode(bluePin, OUTPUT);
   pinMode(yellowPin, OUTPUT);
}

void loop()
{
   int sensorValue = analogRead(sensorPin); // read sensor
   if (sensorValue >= 0 && sensorValue <= 199) // all off
   {
      digitalWrite(redPin, LOW);
      digitalWrite(bluePin, LOW);
      digitalWrite(yellowPin, LOW);
   }
   else if (sensorValue >= 200 && sensorValue <= 599)  // red on
   {
      digitalWrite(redPin, HIGH);
      digitalWrite(bluePin, LOW);
      digitalWrite(yellowPin, LOW);
   }
   else if (sensorValue >= 600 && sensorValue <= 799) // blue on
   {
      digitalWrite(redPin, LOW);
      digitalWrite(bluePin, HIGH);
      digitalWrite(yellowPin, LOW);
   }
   else if (sensorValue >= 800 && sensorValue <= 1023) // yellow on
   {
      digitalWrite(redPin, LOW);
      digitalWrite(bluePin, LOW);
      digitalWrite(yellowPin, HIGH);
   }

}

FSR_LED.jpg

Hello,

Would someone be able to add to this code for me I am new to arduino. I have this set up with 3 LEDs and it works great but it would be better if I could have 5 or 6 LEDs lighting up at different levels of pressure. Could someone add to the code for me?

Thanks

const byte redPin = 9;
const byte bluePin = 10;
const byte yellowPin = 11;
const byte sensorPin = A0;

void setup()
{
Serial.begin(115200);
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(yellowPin, OUTPUT);
}

void loop()
{
int sensorValue = analogRead(sensorPin); // read sensor
if (sensorValue >= 0 && sensorValue <= 199) // all off
{
digitalWrite(redPin, LOW);
digitalWrite(bluePin, LOW);
digitalWrite(yellowPin, LOW);
}
else if (sensorValue >= 200 && sensorValue <= 599) // red on
{
digitalWrite(redPin, HIGH);
digitalWrite(bluePin, LOW);
digitalWrite(yellowPin, LOW);
}
else if (sensorValue >= 600 && sensorValue <= 799) // blue on
{
digitalWrite(redPin, LOW);
digitalWrite(bluePin, HIGH);
digitalWrite(yellowPin, LOW);
}
else if (sensorValue >= 800 && sensorValue <= 1023) // yellow on
{
digitalWrite(redPin, LOW);
digitalWrite(bluePin, LOW);
digitalWrite(yellowPin, HIGH);
}

}

Could someone add to the code for me?

Have you tried to do it yourself ?

It seems to me that all you need to do is to change the ranges in the if tests, add some more for different ranges and turn on LEDs to match when the value is in range

Or you could, of course, pay someone to do it for you

I note that this is not your first thread on the subject.

Could someone add to the code for me?

No. Why beg for handouts when you could actually learn a bit yourself with our help?

if (sensorValue >= 800 && sensorValue <= 1023)

Look at that line the code following it will happen if the sensor value is greater than 800 and less than 1023.
You take the lines like that and make the ranges narrower. A sensor value of 0 is the lowest you can get and one of 1023 is the biggest you can get. So to have more things happen simple divide those ranges into more and smaller bands.

UKHeliBob: I note that this is not your first thread on the subject.

You know what willfuller22, what annoys people here the most is posters like you how keep on asking the same question over and over on different threads. People waste their time duplicating answers and quite clearly you have not got the intellectual capability of understanding any of them.

Hi, Put a Serial.println statement in your code and see what values you are getting from the FSR in the IDE monitor.

Tom.. :) PS. Who suggested you do this project and what year of the course are you in?

Hello

I am a beginner and I have set up a circuit which works fine with this code lighting 3 LED’s. Would someone be able to write the code to light up 6 LED’s at different levels of pressure?

Thanks

const byte redPin = 9;
const byte bluePin = 10;
const byte yellowPin = 11;
const byte sensorPin = A0;

void setup()
{
Serial.begin(115200);
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(yellowPin, OUTPUT);
}

void loop()
{
int sensorValue = analogRead(sensorPin); // read sensor
if (sensorValue >= 0 && sensorValue <= 199) // all off
{
digitalWrite(redPin, LOW);
digitalWrite(bluePin, LOW);
digitalWrite(yellowPin, LOW);
}
else if (sensorValue >= 200 && sensorValue <= 599) // red on
{
digitalWrite(redPin, HIGH);
digitalWrite(bluePin, LOW);
digitalWrite(yellowPin, LOW);
}
else if (sensorValue >= 600 && sensorValue <= 799) // blue on
{
digitalWrite(redPin, LOW);
digitalWrite(bluePin, HIGH);
digitalWrite(yellowPin, LOW);
}
else if (sensorValue >= 800 && sensorValue <= 1023) // yellow on
{
digitalWrite(redPin, LOW);
digitalWrite(bluePin, LOW);
digitalWrite(yellowPin, HIGH);
}

}

I am a beginner and I have set up a circuit which works fine with this code lighting 3 LED's.

Using code that I wrote for you. Do I get credit?

So you think that no one will notice that you started a new thread with the same question that you asked at least twice before. Well I did because I recognize the code that I posted for you (twice). Quit cross posting.

If you can't figure out how to change the ranges by now, I suggest that you find a different hobby.