Arduino Uno Project 12 knock lock problem unknown

Hello, I'm having a problem with the Arduino Uno Knock Lock project. When I power up the project, the red LED turns on but the Servo Motor will not move without help. It also makes these weird twitching motions and makes some weird noises. Whenever I tap the piezo it just makes bigger twitchy motions, and nothing happens when I touch the button. I have tested all the components and have rechecked/written the code twice, as well as checking the (component placement? wiring design?) multiple times. I have no idea what the problem could be.
Thanks so much for any help.

Here's the code:
[/code]
#include <Servo.h>
Servo myServo;

const int piezo = A0;
const int switchPin = 2;
const int yellowLed = 3;
const int greenLed = 4;
const int redLed = 5;

int knockVal;
int switchVal;

const int quietKnock = 10;
const int loudKnock = 100;

boolean locked = false;
int numberOfKnocks = 0;

void setup() {
// put your setup code here, to run once:
myServo.attach(9);
pinMode (yellowLed, OUTPUT);
pinMode (redLed, OUTPUT);
pinMode (greenLed, OUTPUT);
pinMode (switchPin, INPUT);
Serial.begin(9600);

digitalWrite(greenLed, HIGH);
myServo.write(0);
Serial.println("The box is unlocked!");
}

void loop() {
// put your main code here, to run repeatedly:
if(locked == false){
switchVal = digitalRead(switchPin);
if(switchVal == HIGH){
locked = true;
digitalWrite(greenLed, LOW);
digitalWrite(redLed, HIGH);
myServo.write(90);
Serial.println("The box is locked!");
delay (1000);
}
}
if(locked == true){
knockVal = analogRead(piezo);

if(numberOfKnocks < 3 && knockVal > 0){
if(checkForKnock(knockVal) == true){
numberOfKnocks++;
}
Serial.print(3-numberOfKnocks);
Serial.println(" more knocks to go");
}
if(numberOfKnocks >= 3){
locked = false;
myServo.write(0);
delay(20);
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
Serial.println("The box is unlocked!");
numberOfKnocks = 0;
}
}
}
boolean checkForKnock(int value){

if(value > quietKnock && value <loudKnock){

digitalWrite(yellowLed, HIGH); 
delay (50); 
digitalWrite(yellowLed, LOW); 
Serial.print("Valid knock of value "); 
Serial.println(value);

return true;

}
else {
Serial.print("Bad knock value ");
Serial.println(value);
return false;
}
}
[/code]

Hello,
somehow a timer function and a small FSM will help

Hi @squid2234
please, use tags </> to post your sketch.

What source are you using to power the servo motor?

Post the schematic of your project.

RV mineirin

It could be a motor sizing problem. Post a schematic of how it is wired, not a frizzy thing. Include links to technical information on each of the hardware devices.

Hello everybody, thanks so much for helping me with my project. It turned out the problem was my USB cable wasn't uploading, a wrong resistor and a wrong port setting. Thanks so much anyways

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