I know that there are some posts talking about this same topic but i already read them and cannot see a solution for my problem.
I have an Arduino Mega with a a Vibration Sensor SW-420 for Arduino and i want to have some LED lights lid up when an impact is registered.
So far so good, and my code was working flawless until something went wrong, the led makes what is suposed to do but sometimes it keeps on flashing and the light on the sensor for the D0 pin is out and stays out for some time until it returns to normal or if we registered another impact.
This is the code:
//Arduino Code - Vibration Sensor
const int ledPin = 11; // led connected to digital pin 5
int Vibration_signal = 7; //Define the Digital Input on the Arduino for the sensor signal
int Sensor_State = 1;
int blinkCounter = 0;
void setup() {
pinMode(Vibration_signal, INPUT); //Set pin as input
pinMode(11, OUTPUT); //Set pin as input
Serial.begin(9600); // Start the serial communication
}
void loop() {
Serial.print("Vibration status: ");
Sensor_State = digitalRead(Vibration_signal);
if (Sensor_State == 1) {
//Acende o led
while(blinkCounter <= 10)
{
Serial.println(blinkCounter);
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);
blinkCounter++;
}
blinkCounter = 0;
delay(50);
}
else {
//Sensor_State = 0;
//digitalWrite(11, LOW);
Serial.println("No vibration");
}
delay(50);
}
So, is there something wrong on the code that could trigger this event?
Already swap for 5 new Sensors and they all do the same thing.
I'am connecting 9v 330ma on the arduino and i have all connected directly on the arduino
I can put on the shematics here for you to check connections.
Just made a test and found out that, this happens if we rub the side of the sensor, somewhat like a short circuit, maybe the problem is on the type of sensor used, or the voltage usage?
The sensor is just a switch, buffered with an opamp as threshold detector.
Try to re-write your code as the StateChangeDetector example in the IDE.
To only register a hit when the pin has been LOW, and changes from LOW to HIGH.
Leo..