// https://forum.arduino.cc/t/neopixel-arduino-uno-programing/986361

# include <Adafruit_NeoPixel.h>

// fixes to logic
// possible now wrong sense of the switches and motion detector inputs! see digitalReads

int NeoLEDFront = 2;    // the data pin for the NeoPixels Front
int NeoLEDRear = 5;    // the data pin for the NeoPixels  Rear
//int numPixels = 40;     // How many NeoPixels we will be using
int FrontPixels = 20;   // How Many NeoPixels Front
int RearPixels = 20;   // How Many NeoPixels Rear
int ESTOP = 13;
int Override = 12;
int i = 0;

Adafruit_NeoPixel strip_Front = Adafruit_NeoPixel(FrontPixels, NeoLEDFront, NEO_GRB + NEO_KHZ800);  // Instatiate the NeoPixel from the Library
Adafruit_NeoPixel strip_Rear = Adafruit_NeoPixel(RearPixels, NeoLEDRear, NEO_GRB + NEO_KHZ800);  // Instatiate the NeoPixel from the Library

int r = 0;			// Global RGB values, change to suit your needs JUST GREEN
int g = 255;
int b = 0;

int MotionFront = 3;     // Motion Sensor Input Side 1 Front
int MotionRear = 4;     // Motion Sensor Input Side 1 Rear


void setup() {
  pinMode(ESTOP, INPUT_PULLUP);         //Estop input
  pinMode(Override, INPUT_PULLUP);      // Override Switch
  pinMode(MotionFront, INPUT_PULLUP);   // PRI Sensor Input
  pinMode(MotionRear, INPUT_PULLUP);    // PRI Sensor Input
  pinMode(NeoLEDFront, OUTPUT);  // NeoPixel Signal Output
  pinMode(NeoLEDRear, OUTPUT);   // NeoPixel Signal Output

  strip_Front.begin();  // initialize the strip
  strip_Rear.begin();

  strip_Front.show();   // make sure it is visible
  strip_Rear.show();
  strip_Front.clear();  // Initialize all pixels to 'off'
  strip_Rear.clear();
  Serial.begin(9600); Serial.println("burp wokwi");
}

void loop() {

  alive();  // run the heartbeat on pin 8

  unsigned char normal = 1; // no one else wants to display, so

  if ( !digitalRead(ESTOP) == true ) {
    Serial.println("E-STOP");
    for ( int i = 0; i < FrontPixels;  i++ ) {       // if value is 0,0,0, then the pixel will be "off"
      strip_Front.setPixelColor(i, 255, 255, 0 );
      strip_Rear.setPixelColor(i, 255, 255, 0 );
    }
  
    normal = 0;
  }

  else if ( !digitalRead(Override) == true ) {             // Check to see if the button is down
    Serial.println("Override");
    for ( int i = 0; i < FrontPixels;  i++ ) {       // if value is 0,0,0, then the pixel will be "off"
      strip_Front.setPixelColor(i, 255, 0, 255 );
      strip_Rear.setPixelColor(i, 255, 0, 255 );
    }
  
    normal = 0;
  }

  else if ( !digitalRead(MotionFront) == true ) {             // Check to see if the button is down
    Serial.println("Front Motion!");
    for ( int i = 0; i < FrontPixels; i++ )               // if value is 0,0,0, then the pixel will be "off"
      strip_Front.setPixelColor(i, 255, 0, 0 );
  
    normal = 0;
  }

  else if ( !digitalRead(MotionRear) == true ) {             // Check to see if the button is down
    Serial.println("Rear Motion!");
    for ( int i = 0; i < RearPixels; i++ )               // if value is 0,0,0, then the pixel will be "off"
      strip_Rear.setPixelColor(i, 0, 0, 255 );
    normal = 0;
  }

  if (normal) {
//    Serial.println("Normal Operation");

    for ( int i = 0; i < FrontPixels;  i++ ) {
      strip_Front.setPixelColor(i, r, g, b);
      strip_Rear.setPixelColor(i, r, g, b);
    }

  }

//  Serial.println("show the strips");

  strip_Front.show();
  strip_Rear.show();

  // delay for the purposes of debouncing the switch
  // use a longer delay just to slow things down a bit

  delay(10);
}

void alive()
{
  static unsigned long lastTime;
  unsigned long now = millis();

  if (!lastTime) pinMode(8, OUTPUT);

  if (now - lastTime > 333) {
    digitalWrite(8, !digitalRead(8));
    lastTime = now;
  }
}