0

Can anyone help me with this issue

I'm trying to program two FSR sensors through typing this code but I've realized that one is working instead of two.

The logic behind the code is that if you were to squeeze both sensors at the same time it will print out an output on the serial monitor acknowledging that both sensors have been squeezed. I have got only one to work by using this code.

The first sensor is connected to the A0 pin on the arduino and the second sensor is connected to the A1 pin

This is the code that I've written

/* FSR simple testing sketch. 

 Connect one end of FSR to power, the other end to Analog 0.
 Then connect one end of a 10K resistor from Analog 0 to ground 
 For more information see www.ladyada.net/learn/sensors/fsr.html */

int fsrPin = 0;
int fsr2Pin = 1;   // the FSR and 10K pulldown are connected to a1
int fsrReading ;
int fsr2Reading;   // the analog reading from the FSR resistor divider 

void setup(void) {
    // We'll send debugging information via the Serial monitor
    Serial.begin(9600);   
}

void loop(void) {
    fsrReading = analogRead(fsrPin);
    fsr2Reading = analogRead(fsr2Pin); 

    Serial.print("Analog reading = ");
    Serial.print(fsrReading);
    Serial.print("\t");
    Serial.print(fsr2Reading);

    // We'll have a few threshholds, qualitatively determined
    if (fsrReading < 10) {
        Serial.println(" - Nothing");
    } else if (fsrReading < 200) {
        Serial.println(" - Light");
    } else if (fsrReading < 500) {
        Serial.println(" - Lightness");
    } else if (fsrReading < 800) {
        Serial.println(" - Medium");
    } else if (fsr2Reading < 1100) {
        Serial.println(" - No-Pressure");
    } else if (fsr2Reading < 1400) {
        Serial.println(" - Light-touch");
    } else if (fsr2Reading < 1700) {
        Serial.println(" - Light-squeeze");
    } else if (fsr2Reading < 2000) {
        Serial.println(" - Medium-squeeze");
    } else {
        Serial.println(" - Big-squeeze");
    }
    delay(1000);
} 

So could anyone tell how to edit this code correctly in order to get two sensors to work?

The one connected to the A0 pin is working but not the other This how its physically connected

  • The picture is not really easy to check connections, so I'll take it for granted they match the diagram (which is correct). First, using analogRead will give values between 0 and 1023, hence why do your tests check thresholds above that (i.e. 1100, 1400, 1700, 2000)? Then your code is testing the second FSR only if the first one is pressed enough (analog read must be >= 800). That-s because of the first else if checking fsr2Reading: else if (fsr2Reading < 1100). You should remove the else there. – jfpoilpret May 6 '16 at 8:25
1

As JFPoilpret says you if logic is wrong. It might make it easier if you took that whole if block out and made it into a function that accepted the value from the sensor as a parameter.

void CheckReading(int reading)
{
    if (reading < 10) {
        Serial.println(" - Nothing");
    } else if (reading < 200) {
        Serial.println(" - Light");
    } else if (reading < 500) {
        Serial.println(" - Lightness");
    } else if (reading < 800) {
        Serial.println(" - Medium");
    } else {
        Serial.println(" - Big-squeeze");
    }
}

Notice that the values only go up to 800, going larger than 1023 won't work anyway.

| improve this answer | |
  • The error message says that a function-definition is not allowed here before the token click this link postimg.org/image/7uy3kqwch – rich1234 May 6 '16 at 13:32
  • @rich1234 That's brilliant, as Holmez said functions can't be declared inside other functions (Lambda functions can, but don't worry about them for now) so moving the code should make the error go away. However you have called the parameter fsr2reading which is also the name of a global variable, this is a bad idea, it could be confusing. Finally you could call the function from within you setup function by doing this fsrReading(analogRead(fsrPin)); fsrReading(analogRead(fsr2Pin)); and you don't need the global variables fsrReading and fsr2Reading. – Code Gorilla May 6 '16 at 22:10
1

Try changing the pins to use A0 and A1, as pins 0 and 1 relate to the digital pins.

int fsrPin = A0;
int fsr2Pin = A1;   // the FSR and 10K pulldown are connected to a1

As has been mentioned by Matt, make sure you also only test for values between 0 and 1023.

You are also missing the pinMode, which sets the pins as input/output in the setup function

void setup(void) {
    // We'll send debugging information via the Serial monitor
    Serial.begin(9600);   
    pinMode(fsrPin, INPUT);
    pinMode(fsr2Pin, INPUT);
}
| improve this answer | |
  • Hi I have received this error when typing the code matt has suggested do u have any idea how to fix it have a look at the link here postimg.org/image/7uy3kqwch – rich1234 May 6 '16 at 13:38
  • It looks like you are trying to declare a function within a function. If you cut the function as suggested by Matt, and paste it at the bottom, after the last }, then that should fix the error you're receiving. – Holmez May 6 '16 at 14:21
0

@Rich1234 I have taken your code, JFPoilpret's, Holmez's and my answers and put them altogether. Hopefully this explains what we mean and will give you something to work from. It does compile, but I don't know if it works, because I don't have two sensors to try it with. The code is by no means perfect, but its a start. If there are any problems, give us a shout.

/* FSR simple testing sketch. 
 Connect one end of FSR to power, the other end to Analog 0.
 Then connect one end of a 10K resistor from Analog 0 to ground 
 For more information see www.ladyada.net/learn/sensors/fsr.html */

const int fsrPin = A0;      // Change to A0 so it refers to the correct pin
const int fsr2Pin = A1;     // Change to A0 so it refers to the correct pin
// int fsrReading ;     // Change Not required
// int fsr2Reading;     // Change Not required

// Function     : Read the data from the pin and write a meaningful description to the serial output.
// Parameters   : The analog pin to read from.
// Returns      : The value read from the pin
int checkReading(int pin)
{
    const int reading = analogRead (pin);       // Get the reading from the pin. (const because it won't change within the scope (life) of this function)
    Serial.print(reading);
    Serial.print(" - ");
    if (reading < 10) {
        Serial.println("Nothing");
    } else if (reading < 200) {
        Serial.println("Light");
    } else if (reading < 500) {
        Serial.println("Lightness");
    } else if (reading < 800) {
        Serial.println("Medium");
    } else {
        Serial.println("Big-squeeze");
    }
    return reading;
}

void setup(void) {
    // We'll send debugging information via the Serial monitor
    Serial.begin(9600);   
    pinMode(fsrPin, INPUT);     // Change to define the pin 'direction'
    pinMode(fsr2Pin, INPUT);
}

void loop(void) {

    Serial.print("Analog reading A0 = ");
    checkReading(fsrPin);
    Serial.print("Analog reading A1 = ");
    checkReading(fsr2Pin);

    delay(1000);
} 
| improve this answer | |
  • Yes it all works for me now thank you very much everyone I really appreciate your help – rich1234 May 7 '16 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.