I have a project where I want a door to be raised up into the wall to create a entrance for my kid, the idea is that when they press a button (like an elevator) that the door will rise to a pre determined height reading that they Ultrasonic sensor detects and then stop the motor. and when the other button is pressed, the door will go down. I have been able to test the wiring and I have figured out the hardware side of the project but the coding side is still giving me issues.
The end goal is that the door should raise to a certain distance from the US sensor and stop but I also want to put LED's on the side that are red when the door is closed but as the door raises up the LED's turn from red to white.
My current code I have not been able to control the LED's the way I want, I found some code to have the strip one color and then as the distance on the US is reduced, the pixels go out, I like that but I want the strip to be red at max distance and then to change to white as the distance is reduced.
The other issue that I am having is the motor right now will just turn when the US sensor is detecting anything more than 76 cm, also without a button being pressed. The other thing that I believe is an issue is that I would like the motor to execute the action with only a button press and not have to hold the button.
This is the code that I have so far. Any help would be appreciated.
#include <Adafruit_NeoPixel.h>
#include <HCSR04.h>
#define LED_COUNT 50
// Define button pins
int button_1 = 8;
int button_2 = 9;
// Define output pins
int motor_ccw = 2;
int motor_cw = 3;
// Define LED pins
#define LED_1 5
#define LED_2 6
Adafruit_NeoPixel strip1(LED_COUNT, LED_1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2(LED_COUNT, LED_2, NEO_GRB + NEO_KHZ800);
// Define the pin for the ultrasonic sensor
#define TRIGGER_PIN 10
#define ECHO_PIN 11
// Define the maximum distance for the color change
#define MAX_DISTANCE 76.2 // in centimeters
// Initialize variables for distance and color
int distance = 0;
int red = 0;
int green = 0;
int blue = 0;
void setup() {
// Initialize serial connection to display values
Serial.begin(9600);
// Initialize the NeoPixel strip
strip1.begin();
// Initialize the NeoPixel strip
strip2.begin();
// Set button pins as input
pinMode(button_1, INPUT_PULLUP);
pinMode(button_2, INPUT_PULLUP);
// Set the trigger and echo pins for the ultrasonic sensor
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Set output pins as output
pinMode(motor_ccw, OUTPUT);
pinMode(motor_cw, OUTPUT);
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
}
// Main loop
void loop() {
// Send a pulse to the ultrasonic sensor
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
// Read the duration of the pulse from the echo pin
long duration = pulseIn(ECHO_PIN, HIGH);
// Calculate the distance in centimeters
distance = duration * 0.034 / 2;
strip1.clear();
for(int i=0; i<distance; i++) // For each pixel in strip...
{
strip1.setPixelColor(i, 0,0,255);
}
strip1.show(); // Send the updated pixel colors to the hardware.
// Print the distance and color values for debugging
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm, Color: ");
Serial.print(red);
Serial.print(", ");
Serial.print(green);
Serial.print(", ");
Serial.println(blue);
// Delay for smoother color change
delay(100);
// Map the distance to a value between 0 and 255 for the color change
red = map(distance, 0, MAX_DISTANCE, 0, 255);
//make sure motor is off
digitalWrite(motor_ccw, LOW);
digitalWrite(motor_cw, LOW);
// Read button states
int button1State = digitalRead(button_1);
int button2State = digitalRead(button_2);
// If button 1 is pressed, activate output 1 for 2 seconds
if (button1State == LOW) {
if (distance < 77.2) { //if distance is less than 76.2 cm
digitalWrite(motor_ccw, HIGH); //turn on motor
} else { //if distance is less than or equal to 76.2 cm
digitalWrite(motor_ccw, LOW); //turn off motor
}
}
// If button 2 is pressed, activate output 2 for 2 seconds
if (button2State == LOW) {
if (distance > 1) { //if distance is greater than 1 cm
digitalWrite(motor_cw, HIGH); //turn on motor
} else { //if distance is greater than or equal to 1 cm
digitalWrite(motor_cw, LOW); //turn off motor
}
}
}
