Hello everyone.
I'm working on this project for one of my university classes but they haven't really taught us much on arduino or coding so we are basically teaching ourselves. For this project, I am using a servo motor, an ultrasound distance sensor, an active buzzer and a 2 color led (KY-011).
The idea is that the legs of the robot retract when my hand gets closer to the ultrasound sensor and the buzzer turn on while the red light turns on and when I move my hand away, the buzzer stops and a green light comes on. The legs retracting part and the buzzer seem to be working fine. However, once the light switches from green to red, it just never goes back to green when I move my hand away again. Can someone please check my script and offer solution?
// included libraries in the code
#include <VarSpeedServo.h> // servo variable speed library
#include <HCSR04.h> // ultrasonc distance library
// Variables
int maxSensorDistance = 100; // ultra sonic sensor will read a maximum distance of 100cm
int servoSpeed = 50; // servo motor will run at 50% of maximum speed
float distance = 0; // variable distance with a 0 value
int roomTemprature = 25; // variable that contains the room temprature value
VarSpeedServo ahmadservo; // new servo object called ahmadservo to control the servo motor
HCSR04 ultrasonicSensor(12, 13, roomTemprature, maxSensorDistance); // initialize ultrasonic sensor and set pins
const int power = 11; // variable power with vallue 11 to be used as a pin on the arduino
int previousangle; // will be used to store some old information during the script
int buzzerPin = 8; //
int redPin = 7; //
int greenPin = 4; //
int val = 200; //
// this part will only run once in the beginning when we power up the arduino
;
void setup() {
ahmadservo.attach(10); // declaring that servo motor is attached to pin 10
ahmadservo.write(180, servoSpeed, true); // set servo to 180 degrees at the speed of 50 when the arduino is powered.
pinMode (power, OUTPUT); // set pin 11 as an power output to power our sensor
ultrasonicSensor.begin(); // start the ultrasonic sensor
pinMode (buzzerPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}
void loop() {
digitalWrite(power, HIGH); // power the sensor by supplying power to pin 11
distance = ultrasonicSensor.getMedianFilterDistance(); // take the average of three distance measurement readings
if (distance != HCSR04_OUT_OF_RANGE)
{
int angle = map(distance, 0, maxSensorDistance, 0, 180);
ahmadservo.write(angle, servoSpeed, true);
previousangle = angle;
delay(25);
digitalWrite (buzzerPin, HIGH);
}
else
{
ahmadservo.write(previousangle, servoSpeed, true);
digitalWrite (buzzerPin, LOW);
}
if (distance != HCSR04_OUT_OF_RANGE)
{
digitalWrite (greenPin, HIGH);
}
if (distance = HCSR04_OUT_OF_RANGE)
{
digitalWrite (redPin, HIGH);
}
}
Also based what I think to be right in my head, the placement of greenPin and redPin should be swapped from what is currently in the script but when I initially had them swapped, the light was red from the get go and never changed to green. This way I have it green at first until my hand gets close and then it turns red but never turns green again after I remove my hand.
Any help will be greatly appreciated. Thank you.