Help with coding 2-color LED module

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.

Where are the LEDs turn off in you code ?

I didn't think to add any because I thought I could just switch between the two based on the distance value of the ultrasound sensor. so always either green or red, never off. but I am also super new to this. this is our first week working with arduino and I don't really know what I am doing. I've figured all of this out so far by watching tutorials.

The LEDs need to be turned OFF for them to go OFF, else they will stay ON :slight_smile: .

Hello
My standard recipe:
Study the IPO model as a basis and then take a bag of input/output handlers, a bag of timers and a fine little finite state machine and then stir it all together and season it until the desired functionality is achieved.

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