Go Down

Topic: HELP ME!!! Desperation! (Read 946 times) previous topic - next topic

Stumplegrumps

Have you gotten the knock sensor to work by itself? It's usually a good idea to get each component working before you start merging them.

If you're able, test your knock sensor to find your ideal threshold. This can be done by using a Serial.print(sensorReading); and watching the serial monitor in the Arduino IDE

Once you've established the threshold, it's time to merge all of the components. I would imagine it would look something like this:

Code: [Select]

#include <Servo.h>

//Servo
Servo first; // create servo object
Servo second; // create servo object
#define firstservopin 8
#define secondservopin 7

//Knock Sensor
#define knockSensor A0
const int threshold = 100; //Sensitivity of the Knock Sensor
int sensorReading;

void setup() {
 first.attach(firstservopin); // attach the servo to digital output 8
 second.attach(secondservopin); // attach the servo to digital output 7
 first.write(90); // center the servo
 second.write(90); // center the servo
}

void loop() {
 sensorReading = analogRead(knockSensor);
 
 if (sensorReading >= threshold) {    
   first.write(0);                        // move first servo to pos 0
   second.write(0);                 // move second servo to pos 0
   delay(1000);
   first.write(120);               // move first servo to pos 120
   second.write(120);         // move second servo to pos 120
   delay(1000);
 }
}



Go Up