esp8266 analog and if statements.

Hey guys, I’m new to programming so this might be a super easy fix but I’ve done some research and haven’t been able to find an answer anywhere. Basically I’m using a sw-18020p vibration sensor and a Nodemcu esp8266. My plan is to have it detect vibration from my washer and send me an email using IFTTT when it is done. I have the sw-18020p connected to the A0 pin and a 3.3v pin and I was able to get it to work by printing a line every time vibration was detected. So my next step is to have it check fro vibration every couple minutes.

int vibrationpin = A0;
int sensorvalue = 0;
bool vibration = false;
unsigned long previousmillis = 0;
const long interval = 1000;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println();
}
void loop() {
  // put your main code here, to run repeatedly:
  sensorvalue = analogRead(vibrationpin);

  unsigned long currentmillis = millis();

  if (sensorvalue > 1023) {
    vibration == true;
  } else {
    vibration == false;
  }

  if (currentmillis - previousmillis >= interval) {
    previousmillis = currentmillis;
    if (vibration == true) {
      Serial.println("vibration detetced.");
    } else {
      Serial.println("no vibration.");
    }
  }
}

In the serial monitor it prints no vibration every second like it should but even if I have it under constant vibration it doesn’t detect any and only prints “no vibration”. I’m not sure why, like I said I’m sure this is a easy fix but I appreciate any help.

Please send your full code. There are many possibilities of the problem comes from. It could be because of the sensor library which should be related to the sampling rate, or any other thing. Please send full code.

isn't the max analog value 1023?

    if (sensorvalue > 1023) {
        vibration == true;
    if (sensorvalue > 1023) {
        vibration == true;

== ?

UKHeliBob:

    if (sensorvalue > 1023) {

vibration == true;



== ?

is that wrong? i didn't know if mattered if it was "=" or "=="

gcjr:
isn't the max analog value 1023?

    if (sensorvalue > 1023) {

vibration == true;

oops, you're right i'll change it and see if it helps.

ArnavPawarAA:
Please send your full code. There are many possibilities of the problem comes from. It could be because of the sensor library which should be related to the sampling rate, or any other thing. Please send full code.

that is my full code, am I missing a library or something?

I updated my code to this and now it's only printing "vibration detected"

int vibrationpin = A0;
int sensorvalue = 0;
bool vibration = false;
unsigned long previousmillis = 0;
const long interval = 1000;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println();
}
void loop() {
  // put your main code here, to run repeatedly:
  sensorvalue = analogRead(vibrationpin);

  unsigned long currentmillis = millis();

  if (sensorvalue > 1000) {
    vibration = true;
  } else {
    vibration = false;
  }

  if (currentmillis - previousmillis >= interval) {
    previousmillis = currentmillis;
    if (vibration = true) {
      Serial.println("vibration detetced.");
    } else {
      Serial.println("no vibration.");
    }
  }
}

I updated my code to this and now it's only printing "vibration detected"

suggest you use Serial.prints to confirm what value the sensor returns fro different amounts of vibration?

could it be that a lower value represents a higher level of vibration?

is that wrong? i didn't know if mattered if it was "=" or "=="

Trust me. It matters

x = 123;  //sets x to 123
x == 123;  //compares x with 123