#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myservo;
#define ir_enter 2
#define ir_back 4
#define ir_car1 8
#define ir_car2 9
int S1 = 0, S2 = 0;
int flag1 = 0, flag2 = 0;
int slot = 2;
int RedLed=11;
int GreenLed=12;
void setup() {
Serial.begin(9600);
pinMode(ir_car1, INPUT);
pinMode(ir_car2, INPUT);
pinMode(ir_enter, INPUT);
pinMode(ir_back, INPUT);
pinMode(RedLed, OUTPUT);
pinMode(GreenLed, OUTPUT);
myservo.attach(7);
myservo.write(90);
lcd.init();
lcd.backlight();
lcd.setCursor(2, 0);
lcd.print("A Final Year");
lcd.setCursor(0, 1);
lcd.print("Project By P.M");
delay(3000);
lcd.clear();
delay(1000);
Read_Sensor();
int total = S1 + S2;
slot = slot - total;
}
void loop() {
Read_Sensor();
lcd.setCursor(0, 0);
lcd.print("Available slot:");
lcd.print(slot);
lcd.print(" ");
lcd.setCursor (0,1);
if (S1 == 1) {
lcd.print("S1:Fill ");
digitalWrite(GreenLed, LOW);
digitalWrite( RedLed, HIGH);
}
else {
lcd.print("S1:Empty");
digitalWrite(GreenLed, HIGH);
digitalWrite(RedLed, LOW);
}
lcd.setCursor (8,1);
if (S2 == 1) {
lcd.print("S2:Fill ");
digitalWrite(GreenLed, LOW);
digitalWrite(RedLed, HIGH);
}
else {
lcd.print("S2:Empty");
digitalWrite(GreenLed, HIGH);
digitalWrite(RedLed, LOW);
}
if (S1 == 1 && S2 == 1) {
digitalWrite(GreenLed, LOW);
digitalWrite(RedLed, HIGH);
}
else {
digitalWrite(GreenLed, HIGH);
digitalWrite(RedLed, LOW);
}
if (digitalRead (ir_enter) == 0 && flag1 == 0) {
if (slot > 0) {
flag1 = 1;
if (flag2 == 0) {
myservo.write(90);
slot = slot - 1;
}
} else {
lcd.setCursor (0,0);
lcd.print(" Sorry No Space! ");
delay(1500);
}
}
if (digitalRead (ir_back) == 0 && flag2 == 0) {
flag2 = 1;
if (flag1 == 0) {
myservo.write(90);
slot = slot + 1;
}
}
if (flag1 == 1 && flag2 == 1) {
delay(1000);
myservo.write(90);
flag1 = 0, flag2 = 0;
}
delay(1);
}
void Read_Sensor() {
S1 = 0, S2 = 0;
ssss
if (digitalRead(ir_car1) == 0) {
S1 = 1;
}
if (digitalRead(ir_car2) == 0) {
S2 = 1;
}
}
Good day everyone.
I was using this code to run my project successfully, but after a while, i try to run it but the servo motor is not working. So i don't know if some can help me to fix this issue. Thank you.
Which Arduino are you using and how is it and the servo powered ?
What does that mean? "Not working" conveys no useful information. Does it mean that the servo does nothing? It vibrates? It smokes? It moves but not to commanded position? It moves to commanded position, but won't hold position? It spins around like a top?
Schematics and a link to the servo datasheet, please.
Delete the other thread... (where you posted English on the Spanish page)
... where you did not follow up with my answer.
I am guessing your Servo is using Vcc from your Arduino. Use a suitable external power supply and connect the external power supply Vcc to your Servo and connect the external power supply GND to the Arduino GND.
... and did not follow up with the responses from January...
Source of borrowed code:
Moderator:
Cross post