Beginner here needing some help

I have this issue where my Force sensitive resistor(FSR) is causing my Temperature sensor(LM35) to spike up when i apply pressure to the FSR. I don't think this should be happening as they both are not connected together.

Also how do I make it so where a timer/counter starts when the FSR hits the threshold and after more than 1 minute, it activates a Alarm and a LED?
Help is much appreciated!

Codes:

int fsrAnalogPin = 3; // FSR is connected to analog 3
int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
int fsrReading; // the analog reading from the FSR resistor divider
float temp;
float startTime;
const int threshold = 800;//an arbitrary threshold level that's in the range of the analog input

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

void loop(void) {

fsrReading = analogRead(fsrAnalogPin);
Serial.print("Analog reading = ");
Serial.println(fsrReading);

if(fsrReading > threshold){
startTime = millis();
}

if(fsrReading > threshold && startTime >= 5000){
digitalWrite(LEDpin,HIGH);
}

temp = analogRead(A1);
temp = temp*0.48828125;
Serial.print("TEMPERATURE:");
Serial.print(temp);
Serial.print("*C");
Serial.println();

delay(500);
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

You need to detect when the force becomes above the threshold rather than when it is above the threshold

See the StateChangeDetection example in the IDE

You should first rule out hardware issues. Use decoupling capacitors and make sure you don't have ground loops. Use a single-point ground or a beefy (i.e. very low-resistance) ground bus.

shouldn't this be A3 (/hardware/arduino/avr/variants/standard/pins_arduino.h)

why not simply (startTime doesn't serve any purpose)

    digitalWrite (LEDpin, fsrReading > threshold);

Whilst it is more explicit to use A3 as the pin number, which I would encourage, the compiler is smart enough to use the correct pin with analogRead(3);

Arduino/hardware/arduino/avr/variants/standard/pins_arduino.h

#define PIN_A3   (17)

There is an excellent tutorial here on how to use millis:

Using millis for timing

if(fsrReading > threshold && startTime >= 5000){
digitalWrite(LEDpin,HIGH);
}

The way you have it startTime will be more than 5000 if it is made equal to millis more than 5 seconds after power is applied to your Arduino. Study the tutorial to see how to use millis correctly.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.