FSR processing looping value 0-200 with added pressure

edited August 2014 in Arduino

Hello, I'm using a FSR resistor, and having an issue with mapping it's value on procesing. This wasn't an issue before however now as i add pressure it goes from 0-200 then repeats three to four times as i add pressure, rather than just stop at the 200 limit. I'm using the following code on processing:

import processing.serial.*;

  //Globals
  Serial due; //Arduino serial connection
  byte[] fsrReading; //FSR readings

  //FSR Reading Indices
  final int FORCE = 2;
  final int NReading = 3;

  void setup() {
    size (500,500);
    due = new Serial(this,"COM11",115200);
    fsrReading=new byte[NReading];
  }

  void draw() {
    readValues();
    float x = map(getReading(FORCE),0,255,0,200); //(Value, original low limit, original high limit, New low limit, new high limit)

    rect(0,0,200,50);
    fill(255);

    rect(0,0,x,50);
    fill(0);

    smooth();
    println(x);

  }

  void readValues() {
    while (due.available() >= NReading) {
      due.readBytes(fsrReading);
    }
  }

  int getReading(int reading) {
    return (int)fsrReading[reading] & 0xff;
  }

And this is what i'm using on my Arduino MEGA:

    #include <Wire.h>
    // FSR Test 
    int fsrPin = A0;     // the FSR and 10K pulldown are connected to a0
    int fsrReading;     // the analog reading from the FSR resistor divider

    void setup(void) {
      Serial.begin(115200);
    }

    void loop(void) {
      // read the value from the sensor:
      fsrReading = analogRead(fsrPin);  
      sendFSR();
    } 

    void sendFSR(void) {
      byte data[1] =
      {fsrReading,};
      Serial.write(data,1);
    }

The FSR sensor is connected as instructed here https://learn.adafruit.com/force-sensitive-resistor-fsr/using-an-fsr

Thanks Enzi

Tagged:

Answers

  • edited August 2014 Answer ✓

    I think the reason is that you get a value from 0-1024 on your sensor and you used this " & 0xff" which pretty much is same as % 255 (modulo 255). so you probably wanna divide the incomming value with 4 and subtract 1. or even better just map from 0 to 1024.

    also you read the third value maybe giving a delay so maybe set force to 0.

  • edited August 2014

    ... " & 0xff" which pretty much is same as % 255 (modulo 255).

    Actually & 255 = & 0xFF = % 256 = % 0x100 = % 0400 = % (1<<8). =:)

  • edited August 2014

    oh ups :D yea. Edit: oh ups again i see u already send it as byte, idk then.

  • edited August 2014

    I tried changing the value of the initial limits and that didn't help. I still get it looping but at either a lower or higher limit.

    Also the 1st and 2nd values are used for something else.

    Any other ideas??

  • Jellyfish, your advice worked, not sure exactly why as I thought the same as GoToLoop but thanks :)

Sign In or Register to comment.