Hello, everyone. I am currently working on a project that involves a tilt sensor and servo motor. My goal is to have the servo motor move 90 degrees only when the tilt sensor senses a tilt. I know this sounds like a rather simple project, but both me and my engineering teacher can not figure out what is wrong. We don’t know whether it is my code or the wiring of the Arduino, because everything seems to be correctly done. There are no errors when I verified the code, and the wiring seems to be correct. This project is overdue, so any immediate help is appreciated! Photos of the wiring are attached. Thank you.
int servoPin = 9;
int switchTilt = 2;
int val;
#include <Servo.h> ;
Servo myservo;
void setup()
{
// put your setup code here, to run once:
pinMode(2, INPUT);
pinMode(13,OUTPUT);
pinMode(9, OUTPUT);
Serial.begin(9600);
}
void loop()
{
// put your main code here, to run repeatedly:
val=digitalRead(switchTilt);
if (val == HIGH)
{
myservo.write(90);
digitalWrite(13, HIGH);
}
else
{
myservo.write(-90);
digitalWrite(13, LOW);
}
}