I am trying to make an automatic hand sanitizer with a servo, ultrasonic sensor and two LEDs that are supposed to light up when the sensor detects my hands.
The schematic diagram:
The code:
#include <Servo.h>
#define trigPin 12
#define echoPin 13
#define MAX_DISTANCE 500
int yellow LED = 3, red LED = 2;
Servo Red LED myservo; // Create object 1 servo motor
void setup () {
Serial.begin (115200); // Serial communication speed
pinMode(trigPin, OUTPUT); // Set the Trigger pin as
pinMode(echoPin, INPUT); // Set Echo pin as
pinMode(red LED, OUTPUT); // Set red LED as pinMode output
pinMode(yellow LED, OUTPUT); // Set green LED as output
myservo.attach (9); // Set servo on PWM pin 9
}
void loop () {
int duration, distance, position = 0, i;
digitalWrite (trigPin, LOW);
delayMicroseconds (2);
digitalWrite (trigPin, HIGH);
delayMicroseconds (10);
digitalWrite (trigPin, LOW);
duration = pulseIn (echoPin, HIGH);
distance = (duration / 2) / 29.1;
Serial.print (distance);
Serial.println ("cm");
if (distance <= 5) // Distance (cm) you can adjust
{
digitalWrite (yellow, LOW); // LED green off
digitalWrite (LED red, HIGH); // the red LED is on
myservo.write (180); // Servo position at 180 degrees
delay (450); // delay
digitalWrite (red LED, LOW); // The red LED is off
myservo.write (90); // Servo position at 90 degrees
delay (450); // Delay
digitalWrite (red LED, HIGH); // the red LED is on
myservo.write (0); // Servo position at 0 degrees
delay (450); // delay
digitalWrite (red LED, LOW); // The red LED is off
myservo.write (90); // Servo position at 90 degrees
}
else {// if the distance is more than specified
digitalWrite (red LED, LOW); // The red LED is off
digitalWrite (LEDYellow, HIGH); // Vivid
yellow LED myservo.write (90); // Servo position at 90 degrees
}
delay (450); // delay
}
The code keeps printing an error message "exit status 1
expected initializer before 'LED' ". Please help
Your code with various mistakes removed. WARNING: If the ultrasonic sensor fails, or is blocked, or doesn't sense an echo, your dispenser will dispense repeatedly until you turn it off. You should check for a 'distance' of zero and treat that as "out of range".
#include <Servo.h>
const byte TriggerPin = 12;
const byte EchoPin = 13;
const byte ServoPin = 9;
const byte YellowLEDPin = 3;
const byte RedLEDPin = 2;
Servo MyServo; // Create object 1 servo motor
const int MAX_DISTANCE = 500;
void setup ()
{
Serial.begin (115200); // Serial communication speed
pinMode(TriggerPin, OUTPUT);
pinMode(EchoPin, INPUT);
pinMode(RedLEDPin, OUTPUT);
pinMode(YellowLEDPin, OUTPUT);
MyServo.attach (ServoPin);
}
void loop ()
{
unsigned long duration;
int distance;
digitalWrite (TriggerPin, LOW);
delayMicroseconds (2);
digitalWrite (TriggerPin, HIGH);
delayMicroseconds (10);
digitalWrite (TriggerPin, LOW);
duration = pulseIn (EchoPin, HIGH);
distance = (duration / 2) / 29.1;
Serial.print (distance);
Serial.println ("cm");
if (distance <= 5) // Distance (cm) you can adjust
{
digitalWrite (YellowLEDPin, LOW); // LED green off
digitalWrite (RedLEDPin, HIGH); // the RedLEDPin is on
MyServo.write (180); // Servo position at 180 degrees
delay (450); // delay
digitalWrite (RedLEDPin, LOW); // The RedLEDPin is off
MyServo.write (90); // Servo position at 90 degrees
delay (450); // Delay
digitalWrite (RedLEDPin, HIGH); // the RedLEDPin is on
MyServo.write (0); // Servo position at 0 degrees
delay (450); // delay
digitalWrite (RedLEDPin, LOW); // The RedLEDPin is off
MyServo.write (90); // Servo position at 90 degrees
}
else // if the distance is more than specified
{
digitalWrite (RedLEDPin, LOW); // The RedLEDPin is off
digitalWrite (YellowLEDPin, HIGH); // Vivid
MyServo.write (90); // Servo position at 90 degrees
}
delay (450); // delay
}