Creating a toggle on/off state for serial.print with FSR

Hi i seem to have hit something so obvious i cant see it under my nose type of code.

I have a force sensor attached to my arduino. I read the value in. I map the value so i can have it as a 0 or 1 toggle. Im wanting to use this value to trigger a serial send value to my computer. The trouble is because its in [u]loop[/u] it just keeps sending again and again.

Its probably my if stamenet and i have tried others but i feel so silly that i cant figure out and when im quite a good programmer in processing and im embarrassed i cant understand the syntax for this problem in arduino hence why i am asking here ( and ready for the flaming.)

the reason its an fsr is because a. its what ive got on me and b. its going under a chair seat to tell an on/off when you are sat on it and trigger the pc software to come on or off. - it works like this but it send multiple on commands which after a few seconds causes serious issues with memory as its launched multiple copies.

the most basic arduino code you have ever seen is below - i almost want a threshold and when exceeded switch boolean and then keep testing but not sending a serial value until it drops

int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor
int y;



void setup() 
{ 

  Serial.begin(4800); 

} 



void loop() 
{ 
  sensorValue = analogRead(sensorPin);
 y = map (sensorValue, 0, 1024, 0, 1);
  //
  if (y ==1) {
    Serial.println("ON");
  }

 else
  if (y ==0) {
    Serial.println("OFF");        // switch off
  }
  
}//end loop

any help most appreciated.

Your from range is wrong. The values that analogRead can return range from 0 to 1023.

As you are calling map, now, all possible input values will be mapped to 0. No matter how hard you push on the sensor, you will never get a value of 1024, which is the only value that will cause map to generate an output of 1.

Sure ill edit that sorry - with the resistor i have, it goes better mapping from 0 to 500 that way i get a nice response. //

Anyway the main point is that it keeps cycling and sending those values (because its in the loop) - is any way i can get it so it wont do that and do a check.

So in the serial monitor im always getting a a stream of OFF until i squeeze then i get a stream of ON.

Can you help me with best practice to implement a toggle function that checks if the value has changed - maybe i need to check every millis() to see if the value has changed from the previous value and only if it HAS then send the ON or OFF via serial.println

?

I don’t think that map is really what you want to use. A simple if statement

if(sensorValue > threshold)
   y = 1;
else
   y = 0;

Now, I’m not sure what you are trying to do with y. If you want to send a message only once when sensorValue goes above threshold, and once when it drops below threshold, then you need to keep track of the previous value, too.

if(sensorValue > threshold && prevValue < threshold)
   // Transitioned to above - send a mesage
if(sensorValue < threshold && prevValue > threshold)
   // Transitioned to below - send a message

prevValue = sensorValue;

You might then have issues when the value is close to threshold, bouncing above and below the threshold. If so, you can google hysteresis to learn how to deal with that situation.

Thank you - id just figured out by looking at some processing code that the simplest way was in the last line to have a previousRead = currentRead; - thats the best way to read the value every millis() i.e every frame cycle in processing. and set the currentRead to = the sensorValue after the analogread in

Now it toggles lovely on and off actually.

Thanks lot ill try the threshold levels out because i have it working when i hold the fsr - but under a chair cushion its not quite working when i sit down