NeoPixel Arduino-Uno Programing

alto777 & gonpezzi

Apologies for the radio silence I have been working my butt off trying to get this circuit done in real world. I thought I had taken it as far as I could in the virtual world.

Thanks to you all I have an almost working LED project. few little glitches to work out.

My Current Code:

# include <Adafruit_NeoPixel.h>
#include <Wire.h> 
#include <LiquidCrystal.h>

const int rs = 28, en = 23, d4 = 27, d5 = 26, d6 = 25, d7 = 24 ;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int NeoLEDFront = 2;    // the data pin for the NeoPixels Front
int NeoLEDRear = 3;    // the data pin for the NeoPixels  Rear
int NeoLEDFront2 = 4;    // the data pin for the NeoPixels Front Side 2
int NeoLEDRear2 = 5;    // the data pin for the NeoPixels  Rear Side 2
int FrontPixels = 232;   // How Many NeoPixels Front
int RearPixels = 232;   // How Many NeoPixels Rear
int FrontPixels2 = 232;   // How Many NeoPixels Front Side 2
int RearPixels2 = 232;   // How Many NeoPixels Rear Side 2

int temp = A0;

int MotionPRIFront = 44;       // Motion Sensor Input Side 1 Front
int MotionPRIRear = 45;        // Motion Sensor Input Side 1 Rear
int MotionPRIFront2 = 46;      // Motion Sensor Input Side 2 Front
int MotionPRIRear2 = 47;       // Motion Sensor Input Side 2 Rear
int val = 0;

int heartbeat = 7;
int power = 6;
int fan = 8;
int relayb = 9;
int relayc = 10;
int relayd = 11;

int Override = 12;
int ESTOP = 13;

int normaltemp = 26;
int midtemp = 27;
int h2temp = 30;

int thresholdValue = 0;
int fanOff = LOW;
int fanOn = HIGH;

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
Adafruit_NeoPixel strip_Front2 = Adafruit_NeoPixel(FrontPixels2, NeoLEDFront2, NEO_GRB + NEO_KHZ800);  // Instatiate the NeoPixel from the Library
Adafruit_NeoPixel strip_Rear2 = Adafruit_NeoPixel(RearPixels2, NeoLEDRear2, NEO_GRB + NEO_KHZ800);  // Instatiate the NeoPixel from the Library

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

void setup() {
  
  pinMode(NeoLEDFront, OUTPUT);  // NeoPixel Signal Output
  pinMode(NeoLEDRear, OUTPUT);   // NeoPixel Signal Output
  pinMode(NeoLEDFront2, OUTPUT);  // NeoPixel Signal Output
  pinMode(NeoLEDRear2, OUTPUT);   // NeoPixel Signal Output

  pinMode(MotionPRIFront, INPUT);   // Motion Sensor Front 1 
  pinMode(MotionPRIRear, INPUT);   // Motion Sensor Rear 1
  pinMode(MotionPRIFront2, INPUT);   //Motion Sensor Front 2
  pinMode(MotionPRIRear2, INPUT);   // Motion Sensor Rear 2

  pinMode(ESTOP, INPUT_PULLUP);         //Estop input
  pinMode(Override, INPUT_PULLUP);      // Override Switch
  pinMode(temp, INPUT);             // temp sensor input
  pinMode(fan, OUTPUT);              // Fan Trigger 
  pinMode(heartbeat, OUTPUT);
  pinMode(power, OUTPUT);
  

  lcd.begin(16, 2);                   // Print a message to the LCD.
  lcd.print("0123456789");
  delay(2000);
  lcd.clear();
  lcd.print("TEST");
  delay(2000);
  lcd.clear();

  strip_Front.begin();  // initialize the strip
  strip_Rear.begin();
  strip_Front2.begin();  // initialize the strip
  strip_Rear2.begin();
  strip_Front.show();   // make sure it is visible
  strip_Rear.show();
  strip_Front2.show();   // make sure it is visible
  strip_Rear2.show();
  strip_Front.clear();  // Initialize all pixels to 'off'
  strip_Rear.clear();
  strip_Front2.clear();  // Initialize all pixels to 'off'
  strip_Rear2.clear();
  Serial.begin(9600); 
}

void loop() {

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

const float BETA = 3950; // should match the Beta Coefficient of the thermistor
int analogValue = analogRead(temp);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;

  // Temperature calculation
  celsius = map(((analogRead(temp) - 20) * 3.04), 0, 1023, -40, 125);
  fahrenheit = ((celsius * 9) / 5 + 32);
  
  Serial.print(celsius);
  Serial.print(" C : ");
  Serial.print(fahrenheit);
  Serial.println(" F");
  lcd.print(celsius);
  lcd.print(" C : ");
    delay(600); // 3 seconds delay 
    lcd.setCursor(2,1);
    lcd.clear();
  
  if( celsius<= 30){
   digitalWrite(fan, LOW);
  }
  
  unsigned char normal = 1; // no one else wants to display

  if ( !digitalRead(ESTOP) == true ) {
    Serial.println("E-STOP");
    lcd.print("ESTOP");
    delay(300); // 3 seconds delay 
    lcd.setCursor(2,1);
    lcd.clear();
     for ( int i = 0; i <FrontPixels;i++ ) {       // if value is 0,0,0, then the pixel will be "off"
      strip_Front.setPixelColor(i, strip_Front.Color(225,0 ,0 ));
      strip_Rear.setPixelColor(i, 255, 0, 0 );
      strip_Front2.setPixelColor(i, 255, 0, 0 );
      strip_Rear2.setPixelColor(i, 255, 0, 0 );
     }
  
    normal = 0;
  }
   
   else if ( !digitalRead(Override) == true ) {             // Check to see if the button is down
    Serial.println("Override");
     lcd.print("Override");
     delay(300); // 3 seconds delay 
     lcd.setCursor(2,1);
     lcd.clear();
    for ( int i = 0; i < FrontPixels;  i++ ){       // if value is 0,0,0, then the pixel will be "off"
      strip_Front.setPixelColor(i, 245, 245, 245 );
      strip_Rear.setPixelColor(i, 245, 245, 245 );
      strip_Front2.setPixelColor(i, 245, 245, 245 );
      strip_Rear2.setPixelColor(i, 245, 245, 245 );
    }
    
    normal = 0;
  }

  if ( celsius<= h2temp){
  digitalWrite(fan, LOW);
  Serial.print("Temp_Normal");
  lcd.print("Fan Off Temp Normal");
  delay(300); // .3 seconds delay 
  lcd.setCursor(2,1);
  lcd.clear();
 }

 else if( celsius>= h2temp){
  digitalWrite(fan, HIGH);
  Serial.print("Temp_Above 30 C");
  lcd.print("Fan On");
  delay(300); // .3 seconds delay 
  lcd.setCursor(2,1);
  lcd.clear();
 }

  else if ( !digitalRead(MotionPRIFront) == true ) {             // Check to see if the button is down
    Serial.println("Front Motion!");
    cd.print("Front Motion!");
    delay(300); // .3 seconds delay 
    lcd.setCursor(2,1);
    lcd.clear();
    for ( int i = 0; i <FrontPixels; i++ ){             // if value is 0,0,0, then the pixel will be "off"
     strip_Front.setPixelColor(i, strip_Front.Color(245,245,245 ));
    }
    normal = 0;
  }
   
  else if ( !digitalRead(MotionPRIFront2) == true ) {             // Check to see if the button is down
    Serial.println("Front Motion 2!");
    lcd.print("Front Motion! 2");
     delay(300); // 3 seconds delay 
     lcd.setCursor(2,1);
     lcd.clear();
    for ( int i = 0; i < FrontPixels2; i++ ){             // if value is 0,0,0, then the pixel will be "off"
      strip_Front2.setPixelColor(i, 245, 245, 245 );
    }
    normal = 0;
  }

  else if ( !digitalRead(MotionPRIRear) == true ) {             // Check to see if the button is down
    Serial.println("Rear Motion!");
    lcd.print("Rear Motion!");
     delay(300); // 3 seconds delay 
     lcd.setCursor(2,1);
     lcd.clear();
    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;
  }

  else if ( !digitalRead(MotionPRIRear2) == true ) {             // Check to see if the button is down
   Serial.println("Rear Motion 2!");
   lcd.print("Rear Motion! 2");
     delay(300); // 3 seconds delay 
     lcd.setCursor(2,1);
     lcd.clear();
    for ( int i = 0; i < RearPixels2; i++ ){               // if value is 0,0,0, then the pixel will be "off"
      strip_Rear2.setPixelColor(i, 0, 0, 255 );
   }
   normal = 0;
  }

  if (normal) {
     Serial.println("Normal Operation");
     lcd.print("Normal Operation!");
     delay(300); // 3 seconds delay 
     lcd.setCursor(2,1);
     lcd.clear();
    for ( int i = 0; i < FrontPixels;  i++ ) {
      strip_Front.setPixelColor(i, r, g, b);
      strip_Rear.setPixelColor(i, r, g, b);
      strip_Front2.setPixelColor(i, r, g, b);
      strip_Rear2.setPixelColor(i, r, g, b);
     }
  }
  
    strip_Front.show();
    strip_Rear.show();
    strip_Front2.show();
    strip_Rear2.show();

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

    delay(100);
  }

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

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

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


    digitalWrite(heartbeat, HIGH);
    delay(40);
    digitalWrite(heartbeat, LOW);
    delay(40);
    digitalWrite(heartbeat, HIGH);
    delay(50);
    digitalWrite(heartbeat, LOW);
    delay(50);
    digitalWrite(heartbeat, HIGH);
    delay(30);
    digitalWrite(heartbeat, LOW);
    delay(30);
 }`




ESTOP - Works great
Override - Works great
Fan with Temperature sensor works amazing !!!
1 1000uf capacitor to each led power side
1 330 Ohm resistor to the DIN control to each Led strip

Motion Sensors work and Serial Prints however the leds don't dim up from current values to full intensity. I want them to fade up and back down when motion is detected or not detected.
Also when I am in ESTOP Motion bypasses and turns the LED's white how can I get ESTOP to override all?

Lastly I have noticed that the LED's do weird flickering at startup & Shutdown. What is best practice? do I put a relay in-line on the power for led's and have a delay at startup and power down switch to turn off relays before power off switch is flicked? or is it ok as is?

Thank you in advance for all the help!!!!!

UPDATE: The PRI Motion sensors work sometimes. I have checked all connections and found nothing it has to be with programming.

Normal, you are overriding the PIR readings with an "else if" at the existing temperature. The strange thing is that they work with that code, since one of the temperature conditions will always be met, the code will never pass to the reading of the pir.
You have to rethink the entire "else if" structure of your code. An example: Is that you can never activate the two front PIRs at the same time? Or the two rear ones?

Gonpezzi

Copy thank you !!

Question what commands would you recommend over an" if" & "else if"

I did think of

val = digitalRead( MotionPRIFront);
Serial.println(val);
if( val== 1) {
for ( int i = 0; i <FrontPixels; i++ ){ 
     strip_Front.setPixelColor(i, strip_Front.Color(245,245,245 ));
    }
}
else {
for ( int i = 0; i < FrontPixels;  i++ ) {
      strip_Front.setPixelColor(i, r, g, b);
}
}

Cheers,

If it is a good solution, apply it for each of your PIR.
As for the section that controls the temperature, I would also make it independent from the rest as follows:

 if ( celsius <= h2temp) {
    digitalWrite(fan, LOW);
    Serial.print("Temp_Normal");
    lcd.print("Fan Off Temp Normal");
    delay(300); // .3 seconds delay
    lcd.setCursor(2, 1);
    lcd.clear();
  }
  else {
    digitalWrite(fan, HIGH);
    Serial.print("Temp_Above 30 C");
    lcd.print("Fan On");
    delay(300); // .3 seconds delay
    lcd.setCursor(2, 1);
    lcd.clear();
  }

Greetings

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