Pressure sensor wrong output

Hey guys,

i have a setup with [pressure sensors] which aren't working correctly.

As long as I press the sensor it should output 'c' and if I release the pressure it should output 'd'.

In my case if I press the sensor it gives out 'c' and 'd' and i if I release the pressure it completely stops.

I'm an absolute beginner so sorry for my sloppy explanation. The weird thing is it worked some days ago, so I'm not sure if the sensors are the problem or if I messed up the code because I made some slight adjustments for the threshhold.

Btw I'm German so 'schwellenwert' means threshhold and 'druck' means pressure.

Thanks in advance.

#include <Keyboard.h>

long lastInteraction = -999999;
boolean stat[] = {false, false, false, false};

char keyOn[] = {'a', 'c', 'e', 'g'};
char keyOff[] = {'b', 'd', 'f', 'h'};





int schwellenwert[] = {800, 800, 800, 800};

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

void loop() {

  for(int i = 0; i< 4; i++){
    int druck = analogRead(i);
    //int druck2 = analogRead(A0);
  
    Serial.print("druck");
    Serial.print(i);
    Serial.print(": ");
    Serial.print(druck);
    Serial.println();
  
    if (millis() - lastInteraction > 300) {
      if (!stat[i] && druck > schwellenwert[i]) {
        stat[i] = true;
        lastInteraction = millis();
        Keyboard.print(keyOn[i]);
      } else if(stat[i] && druck < schwellenwert[i]) {
        stat[i] = false;
        lastInteraction = millis();
        Keyboard.print(keyOff[i]);
      }
    }
  }
}

Millis() returns an UNSIGNED long.

Show us a schematic of how your have the sensor connected. Needs to be a voltage divider circuit. The sensor read will give a numeric value of 0 - 1023. What is printing? Certainly not letters.

@aosp3

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

1 Like

In order to select appropriate values for "schwellenwert[]", put in a Serial.print() statement to see the actual output of analogRead(), when the FSR is pressed.

Are you getting the correct pressures in Serial.print(druck)?

Better yet, put in Serial.print after the line:

Serial.println (millis());
Serial.println (stat[i]);
Serial.println (druck);
Serial.println (schwellenwert[i]);

This should help you see where the problem lies

1 Like

I was able to fix the issue guys, thanks for the help!

Thanks for letting us know :+1:
It may help others down the road if you could reply with the changes you made to make the code work.

1 Like

Sure!

#include <Keyboard.h>
long lastInteraction = -999999;
boolean stat[] = {false, false, false, false};

char keyOn[] = {'a', 'c', 'e', 'g'};
char keyOff[] = {'b', 'd', 'f', 'h'};



int schwellenwert[] = {50, 50, 50, 50};

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

void loop() {

  for(int i = 0; i< 4; i++){
    int druck = analogRead(i);
    //int druck2 = analogRead(A0);
  
    Serial.print("druck");
    Serial.print(i);
    Serial.print(": ");
    Serial.print(druck);
    Serial.println();

    Serial.print(stat[i]);
    
  
    if (millis() - lastInteraction > 100) {
      if (!stat[i] && druck > schwellenwert[i]) {
        stat[i] = true;
        lastInteraction = millis();
        Keyboard.print(keyOn[i]);
      } else if(stat[i] && druck < schwellenwert[i]) {
        stat[i] = false;
        lastInteraction = millis();
        Keyboard.print(keyOff[i]);
      }
    }
  }

}
1 Like

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