I took your code and adapted it to what I was planning, can you let me know if is going in the right direction? I am also going to try to simulate it using that simulator.
#include <FastLED.h>
#include <HCSR04.h>
const byte PinMotorUp = 2;
const byte PinMotorDown = 3;
const byte PinLedUp = 7;
const byte PinLedDown = 8;
const byte LED_1 = 5;
const byte LED_2 = 6;
const byte PinBut = 4;
enum { LedOff = HIGH, LedOn = LOW };
enum { MotorOff = LOW, MotorOn = HIGH };
enum { DoorDown, DoorUp };
int doorPos = DoorDown;
enum { S_Idle, S_Raise, S_Lower };
int state = S_Idle;
// distance to stop the motor in inches
const int DistanceUp = 2;
const int DistanceDown = 30;
// Define the pins for the ultrasonic sensor
const byte TRIGGER_PIN = 10;
const byte ECHO_PIN = 11;
// ------------------------LEDS-----------------------------------------------------
// how many LEDS on the strip there will be on each side of the door and other LED programming
#define NUM_LEDS_PER_STRIP 50
#define COLOR_ORDER GRB
#define CHIPSET WS2812B
#define BRIGHTNESS 60
#define VOLTS 5
#define MAX_AMPS 500
CRGB leds[NUM_LEDS_PER_STRIP];
// setting parameters for fading amount
int fadeUpAmount = 10;
int fadeDownAmount = 3;
//-------------------------------------ULtrasonic Sensor---------------------------------------------------
// ultrasonic sensor using float to allow decimals
float duration;
float distanceCM;
float distanceIN;
// --------------------------------------SETUP---------------------------------------
void
setup (void)
{
Serial.begin (9600);
digitalWrite (PinMotorUp, MotorOff);
digitalWrite (PinMotorDown, MotorOff);
pinMode (PinMotorUp, OUTPUT);
pinMode (PinMotorDown, OUTPUT);
digitalWrite (PinLedUp, LedOff);
digitalWrite (PinLedDown, LedOff);
pinMode (PinLedUp, OUTPUT);
pinMode (PinLedDown, OUTPUT);
// ---------- start the US sensor and get base reading ------------
// start with clean signal
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
//send trigger signal
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
// return pulse duration in microseconds
// if set to HIGH, pulseIN() waits for the pin to go from LOW to HIGH
// stops timing when pin goes back LOW
duration = pulseIn(ECHO_PIN, HIGH);
// convert m/s to in/microsec
// 343 m/s = .034 cm/microseconds
distanceCM = (duration * 0.034) / 2;
// convert to inches, 1in = 2.54cm
distanceIN = distanceCM / 2.54;
// print distance
Serial.print("Distance: ");
Serial.print(distanceCM);
Serial.print(" cm | ");
Serial.print(distanceIN);
Serial.print(" in");
delay(100);
if (DistanceDown >= digitalRead(distanceIN)) {
digitalWrite (PinLedDown, LedOn);
doorPos = DoorDown;
Serial.println ("Door Down");
}
else if (DistanceUp <= digitalRead(distanceIN)) {
digitalWrite (PinLedUp, LedOn);
doorPos = DoorUp;
Serial.println ("Door Up");
}
else
Serial.println ("Door partially open");
// Initialize the NeoPixel strip
FastLED.addLeds<CHIPSET,LED_1,COLOR_ORDER>(leds, NUM_LEDS_PER_STRIP);
FastLED.addLeds<CHIPSET,LED_2,COLOR_ORDER>(leds, NUM_LEDS_PER_STRIP);
FastLED.setMaxPowerInVoltsAndMilliamps(VOLTS,MAX_AMPS);
FastLED.setBrightness(BRIGHTNESS);
FastLED.clear();
FastLED.show();
// Set button pins as input
pinMode(PinBut, INPUT_PULLUP);
// Set the trigger and echo pins for the ultrasonic sensor
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// All done
Serial.println("Setup completed");
}
// -----------------------------------------------------------------------------
void loop() {
//execute the state machine for the door
doorState();
//complete the LED funciton in accordance to where the door is
ledState();
//obtain the ultrasonic sensor readings
sensorState();
}
void doorState() {
switch (state) {
case S_Idle:
digitalWrite (PinMotorDown, MotorOff);
digitalWrite (PinMotorUp, MotorOff);
if (LOW == digitalRead (PinBut)) {
if (DoorDown == doorPos) {
state = S_Raise;
digitalWrite (PinMotorUp, MotorOn);
digitalWrite (PinLedDown, LedOff);
Serial.println ("Raise Door");
}
else {
state = S_Lower;
digitalWrite (PinMotorDown, MotorOn);
digitalWrite (PinLedUp, LedOff);
Serial.println ("Lower Door");
}
}
break;
case S_Raise:
if (DistanceUp <= digitalRead(distanceIN)) {
state = S_Idle;
doorPos = DoorUp;
digitalWrite (PinLedUp, LedOn);
Serial.println ("Door Up");
}
break;
case S_Lower:
if (DistanceDown >= digitalRead(distanceIN)) {
state = S_Idle;
doorPos = DoorDown;
digitalWrite (PinLedDown, LedOn);
Serial.println ("Door Down");
}
break;
}
}
void ledState() {
if (digitalRead(distanceIN) <= 2) {
for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
leds[i] = CRGB::White;
leds[i].maximizeBrightness(fadeDownAmount);
}
FastLED.show();
fadeDownAmount --;
}
// this will detect when the motor is lowering the door and change all the LED's to red
if (PinMotorDown == HIGH) {
for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
leds[i] = CRGB::Red;
leds[i].maximizeBrightness(fadeUpAmount);
FastLED.show();
fadeUpAmount ++;
delay(100); // use this to control how fast the LED's change
}
}
// this will activate once the door stops at the bottom and fade out the lights, it should fade the lights out
if (digitalRead(distanceIN) >= 29) {
for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
leds[i] = CRGB::Red;
leds[i].maximizeBrightness(fadeDownAmount);
}
FastLED.show();
fadeDownAmount --;
}
if (PinMotorUp == HIGH) {
for(int i = NUM_LEDS_PER_STRIP; i >=0 ; i--) { // decrements from the end of the strip to the begining
leds[i] = CRGB::Red;
leds[i].maximizeBrightness(fadeUpAmount);
FastLED.show();
fadeUpAmount ++;
delay(100);
}
}
}
void sensorState() {
// start with clean signal
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
//send trigger signal
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
// return pulse duration in microseconds
// if set to HIGH, pulseIN() waits for the pin to go from LOW to HIGH
// stops timing when pin goes back LOW
duration = pulseIn(ECHO_PIN, HIGH);
// convert m/s to in/microsec
// 343 m/s = .034 cm/microseconds
distanceCM = (duration * 0.034) / 2;
// convert to inches, 1in = 2.54cm
distanceIN = distanceCM / 2.54;
// print distance
Serial.print("Distance: ");
Serial.print(distanceCM);
Serial.print(" cm | ");
Serial.print(distanceIN);
Serial.print(" in");
delay(100);
}
// Helper routine to track state machine progress
void displayState(String currState) {
static String prevState = "";
if (currState != prevState) {
Serial.println(currState);
prevState = currState;
}
}