Arduino runs really slow when i enable pinMode(????, INPUT) using 4 sensors ?

Hi All

I am using Mega 2560 for this project

I am fairly new to this so apologise for coding if a mess

I am trying to do a project where LED strip runs after a certain condition is met

When temperature is plus or minus set point = light up all LED strip
When temperature is plus or minus set point = light up all LED strip and start Radio
When movement is detected = light up LED strip in certain colours

All above works fine it's not perfect but for what i need it's fine.

The problem i am having is when i try to use movement sensors it makes the reaction time so slow
the rest take 5 or more seconds to do anything, so everything is delayed until then.

As soon as i disable the sensor code everything runs perfect i have tried to use an Array, and also do the functions a separate functions for sensor movement

I have spent a month solid trying to solve this without help but i can't solve this, could someone point me in the right direction please?

Part 1 of code

Thanks
Peter

#include <TEA5767Radio.h>
#include <Adafruit_NeoPixel.h>
#include <Button.h>
#include <Wire.h>
#include "SparkFunBME280.h" // temp sensor
#include <LiquidCrystal_I2C.h>

TEA5767Radio radio = TEA5767Radio();

#define  PIXELS 572   // Number of NeoPixels
#define PIN 3         // pin on the Arduino is connected to pin 3 for NeoPixels
#define Radio 40      // Mosfet Pin
#define Fsensor 4
#define Bsensor 5
#define Lsensor 6
#define Rsensor 7

//---< Neo Pixels >---//
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXELS, PIN, NEO_GRB + NEO_KHZ800);

//---< Temp Sensor >---//
BME280 mySensor ; //Uses I2C address 0x76 (jumper closed) // temp sensor

// set the LCD address to 0x27 for a 20 chars 4 line display
LiquidCrystal_I2C lcd (0x27, 20, 4);
/*-----( Declare Variables )-----*/

// const int chipSelect = 4;
// int SensorsPinArray [4] = {4, 5, 6, 7}; // Array for Microwave Sensor PINS //

Button button1(24); // Connect your button between pin number and GND
Button button2(25); // Connect your button between pin number and GND

int alarmTemp = 77; // starting set point for temp adjustments
//int alarmTempLow = 77; // starting set point for temp adjustments

void setup() {

  // Serial.begin(9600); // Used to enable Print data to serial port 9600 is port speed
  button1.begin(); // Used to Higher Temp
  button2.begin(); // Used to Lower Temp
  lcd.init(); // initialize the lcd
  lcd.backlight(); //Backlight ON if under program control
  pinMode(Radio, OUTPUT); // Radio control pin as output
  digitalWrite(Radio, HIGH); // Turn the radio On
  pinMode(Fsensor, INPUT); // Sensor pin
  pinMode(Bsensor, INPUT); // Sensor pin
  pinMode(Lsensor, INPUT); // Sensor pin
  pinMode(Rsensor, INPUT); // Sensor pin
  // pinMode(SensorsPinArray, INPUT);
  strip.begin(); // Neo Pixels Begin
  strip.setBrightness(10); // Brightness control for Neo Pixels
  Wire.begin();
  mySensor.setI2CAddress(0x76); //Connect I2C sensor
  mySensor.beginI2C();
  lcd.init(); // initialize the lcd
  lcd.backlight(); //Backlight ON if under program control
}

Ghost Box Sensors-Radio-Buttons-Temp-lcd backup V16 ext Power working but slow.txt (8.08 KB)

Hi Peter, +1 karma for using code tags on your first post. Did you find out how to do that in the "read this" sticky post? If not, please go read that, because right now we don't even know what kind of Arduino you are using.

part 2

void allpixels_off() //---< Turns All Pixels Off Actions >---//
{
  for (int i = 600; i > 0; i--) {
    strip.setPixelColor(i, strip.Color(0, 0 , 0)); // minus color.
    strip.show(); // This sends the updated pixel color to the hardware.
    strip.clear();
  }
}
//---< Turns All Pixels Off Actions End >---//



void BluePixels() //---< Blue all pixels for plus temp Start >---//
{
  int temp = mySensor.readTempF();
  if (temp >= alarmTemp)
  {
    for (int i = 580; i > 0; i--) {
      strip.setPixelColor(i, strip.Color(0, 50 , 150)); // minus color.
      strip.show(); // This sends the updated pixel color to the hardware.
    }
  }
}
//---< Blue all pixels for plus temp End >---//



void RedPixels() //---< Red affect all pixels for minus temp Start >---//
{
  int temp = mySensor.readTempF();
  if (temp >= alarmTemp)
  {
    for (int i = 580; i > 0; i--) {
      strip.setPixelColor(i, strip.Color(0, 50 , 150)); // minus color.
      strip.show(); // This sends the updated pixel color to the hardware.
    }
  }
}
//---< Red affect all pixels for minus temp Start End >---//


void FrontSensor() //---< Front affect all pixels for front sensor Start >---//
{
  int FRsensor = digitalRead(Fsensor);
  if (FRsensor == HIGH)
  {
    for (int i = 150; i > 0; i--) {
      strip.setPixelColor(i, strip.Color(230, 80, 0)); // minus color.
      strip.show(); // This sends the updated pixel color to the hardware.
    }
  }
  else
  {
    allpixels_off();
  }
}

//---< Front affect all pixels for front sensor End >---//

void BackSensor() //---< Back affect all pixels for Back sensor Start >---//
{
  int BAsensor = digitalRead(Bsensor);
  if (BAsensor == HIGH)
  {
    for (int i = 300; i > 150; i--) {
      strip.setPixelColor(i, strip.Color(255, 255, 0)); // Yellow color.
      strip.show(); // This sends the updated pixel color to the hardware.
    }
  }
  else
  {
    allpixels_off();
  }
}
//---< Back affect all pixels for Back sensor End >---//


void LeftSensor() //---< Left affect all pixels for left sensor Start >---//
{
  int LEsensor = digitalRead(Lsensor);
  if (LEsensor == HIGH)
  {
    for (int i = 450; i > 300; i--) {
      strip.setPixelColor(i, strip.Color(255, 0, 100)); // Pink color.
      strip.show(); // This sends the updated pixel color to the hardware.
    }
  }
  else
  {
    allpixels_off();
  }
}
//---< Left affect all pixels for left sensor End >---//

void RightSensor() //---< Back affect all pixels for Back sensor Start >---//
{
  int RIsensor = digitalRead(Rsensor);
  if (RIsensor == HIGH)
  {
    for (int i = 570; i > 450; i--) {
      strip.setPixelColor(i, strip.Color(0, 255, 255)); // Cyan color.
      strip.show(); // This sends the updated pixel color to the hardware.
    }
  }
  else
  {
    allpixels_off();
  }
}
//---< Right affect all pixels for right sensor End >---//



void SetTemp() //---< Set Temp End >---//
{
  if (button1.pressed())
    alarmTemp = alarmTemp + 1; //Raise alarmTempHi by one

  if (button2.pressed())
    alarmTemp = alarmTemp - 1; //Lower alarmTempLow by one
  //---< Set Temp End >---//
}

void tempdata() //---< Temperature Actions >---//
{

  int temp = mySensor.readTempF();
  lcd.setCursor(1, 0); //Start at character 1 on line 0
  lcd.print("CURRENT TEMP ");
  lcd.setCursor(14, 0); //Start at character 1 on line 0
  lcd.print(mySensor.readTempF(), 0);
  lcd.setCursor(1, 1); //Start at character 1 on line 1
  lcd.print("CURRENT SET ");
  lcd.setCursor(14, 1); //Start at character 10 on line 1
  lcd.print(alarmTemp);

  if (temp >= alarmTemp)
  {
    BluePixels();
    allpixels_off();
    ForwardFrequency();
    digitalWrite(Radio, LOW); // Turn the MOSFET Switch OFF
    delay(50);
    digitalWrite(Radio, HIGH); // Turn the MOSFET Switch On
  }
}

/*
  if (temp <= alarmTempLow) {

   RedPixels();
   ReverseFrequency ();
   digitalWrite(Radio, LOW); // Turn the MOSFET Switch OFF
   delay(50);
  }
  else
  {
  digitalWrite(Radio, HIGH); // Turn the MOSFET Switch On
  allpixels_off();
  }
*/
//---< Temperature Actions End >---//

void ForwardFrequency() //---< Sweep FM Range Forward Actions >---//
{
  int threshold = 1007;
  for ( int i = 850 ; i < 1007 ; i++) { //sweep FM range forward //
    float Frequency = i / 10.0 ;
    radio.setFrequency(Frequency); // pick your own frequency
    lcd.setCursor(1, 3);
    lcd.print("FREQUENCY");
    lcd.setCursor(11, 3); //Start at character 10 on line 1
    lcd.print(i);
    delay(100);
  }
}
//---< Sweep FM Range Forward Actions End  >---//

void ReverseFrequency () //---< Sweep FM Range Reverse Actions >---//
{
  int threshold = 1007;
  for ( int i = 1007 ; i < 850 ; i--) { //sweep FM range forward //
    float Frequency = i / 10.0 ;
    radio.setFrequency(Frequency); // pick your own frequency
    lcd.setCursor(1, 3);
    lcd.print("FREQUENCY");
    lcd.setCursor(11, 3); //Start at character 10 on line 1
    lcd.print(i);
    delay(150);
    //return false;
  }
}
//---< Sweep FM Range Reverse Actions End  >---//


/*

  void sensordata_all() //---< Microwave Sensor Actions >---//
  {
  for (byte x = 0 ; x < (SensorsPinArray) ; ++x) {
  }
  if (SensorsPinArray == HIGH)
  {
    // Serial.println(SensorsPinArray[4]);
    // delay(100);
    for (int i = 0; i < PIXELS; i++) {
      strip.setPixelColor(i, strip.Color(0, 150, 0)); // Moderately bright Red color.
      strip.setBrightness(10); // Brightness
      strip.show(); // This sends the updated pixel color to the hardware.
    }
  }
  if (SensorsPinArray == LOW)
  {
    allpixels_off();
  }
  }
  //---< Microwave Sensor Actions End >---//

*/


void loop() {

  SetTemp();
  tempdata(); // tempdata void
  // sensordata_all();
  FrontSensor(); // Front Sensor affect all Orange pixels when HIGH
  BackSensor();  // Back Sensor affect all Yellow pixels when HIGH
  LeftSensor();  // Left Sensor affect all Pink pixels when HIGH
  RightSensor(); // Right Sensor affect all Cyan pixels when HIGH
}

Ok, I see some mistakes there. You should not call strip.show() after setting every individual pixel, only once after updating all pixels (or after changing all pixels that need changing). Also, strip.clear() is the same as setting all pixels to black, so either use that or a loop to set each pixel black, but not both.

part 2

void allpixels_off() //---< Turns All Pixels Off Actions >---//
{
  for (int i = 600; i > 0; i--) {
    strip.setPixelColor(i, strip.Color(0, 0 , 0)); // minus color.
    strip.show(); // This sends the updated pixel color to the hardware.
    strip.clear();
  }
}
//---< Turns All Pixels Off Actions End >---//



void BluePixels() //---< Blue all pixels for plus temp Start >---//
{
  int temp = mySensor.readTempF();
  if (temp >= alarmTemp)
  {
    for (int i = 580; i > 0; i--) {
      strip.setPixelColor(i, strip.Color(0, 50 , 150)); // minus color.
      strip.show(); // This sends the updated pixel color to the hardware.
    }
  }
}
//---< Blue all pixels for plus temp End >---//



void RedPixels() //---< Red affect all pixels for minus temp Start >---//
{
  int temp = mySensor.readTempF();
  if (temp >= alarmTemp)
  {
    for (int i = 580; i > 0; i--) {
      strip.setPixelColor(i, strip.Color(0, 50 , 150)); // minus color.
      strip.show(); // This sends the updated pixel color to the hardware.
    }
  }
}
//---< Red affect all pixels for minus temp Start End >---//


void FrontSensor() //---< Front affect all pixels for front sensor Start >---//
{
  int FRsensor = digitalRead(Fsensor);
  if (FRsensor == HIGH)
  {
    for (int i = 150; i > 0; i--) {
      strip.setPixelColor(i, strip.Color(230, 80, 0)); // minus color.
      strip.show(); // This sends the updated pixel color to the hardware.
    }
  }
  else
  {
    allpixels_off();
  }
}

//---< Front affect all pixels for front sensor End >---//

void BackSensor() //---< Back affect all pixels for Back sensor Start >---//
{
  int BAsensor = digitalRead(Bsensor);
  if (BAsensor == HIGH)
  {
    for (int i = 300; i > 150; i--) {
      strip.setPixelColor(i, strip.Color(255, 255, 0)); // Yellow color.
      strip.show(); // This sends the updated pixel color to the hardware.
    }
  }
  else
  {
    allpixels_off();
  }
}
//---< Back affect all pixels for Back sensor End >---//


void LeftSensor() //---< Left affect all pixels for left sensor Start >---//
{
  int LEsensor = digitalRead(Lsensor);
  if (LEsensor == HIGH)
  {
    for (int i = 450; i > 300; i--) {
      strip.setPixelColor(i, strip.Color(255, 0, 100)); // Pink color.
      strip.show(); // This sends the updated pixel color to the hardware.
    }
  }
  else
  {
    allpixels_off();
  }
}
//---< Left affect all pixels for left sensor End >---//

void RightSensor() //---< Back affect all pixels for Back sensor Start >---//
{
  int RIsensor = digitalRead(Rsensor);
  if (RIsensor == HIGH)
  {
    for (int i = 570; i > 450; i--) {
      strip.setPixelColor(i, strip.Color(0, 255, 255)); // Cyan color.
      strip.show(); // This sends the updated pixel color to the hardware.
    }
  }
  else
  {
    allpixels_off();
  }
}
//---< Right affect all pixels for right sensor End >---//



void SetTemp() //---< Set Temp End >---//
{
  if (button1.pressed())
    alarmTemp = alarmTemp + 1; //Raise alarmTempHi by one

  if (button2.pressed())
    alarmTemp = alarmTemp - 1; //Lower alarmTempLow by one
  //---< Set Temp End >---//
}

void tempdata() //---< Temperature Actions >---//
{

  int temp = mySensor.readTempF();
  lcd.setCursor(1, 0); //Start at character 1 on line 0
  lcd.print("CURRENT TEMP ");
  lcd.setCursor(14, 0); //Start at character 1 on line 0
  lcd.print(mySensor.readTempF(), 0);
  lcd.setCursor(1, 1); //Start at character 1 on line 1
  lcd.print("CURRENT SET ");
  lcd.setCursor(14, 1); //Start at character 10 on line 1
  lcd.print(alarmTemp);

  if (temp >= alarmTemp)
  {
    BluePixels();
    allpixels_off();
    ForwardFrequency();
    digitalWrite(Radio, LOW); // Turn the MOSFET Switch OFF
    delay(50);
    digitalWrite(Radio, HIGH); // Turn the MOSFET Switch On
  }
}

/*
  if (temp <= alarmTempLow) {

   RedPixels();
   ReverseFrequency ();
   digitalWrite(Radio, LOW); // Turn the MOSFET Switch OFF
   delay(50);
  }
  else
  {
  digitalWrite(Radio, HIGH); // Turn the MOSFET Switch On
  allpixels_off();
  }
*/
//---< Temperature Actions End >---//

void ForwardFrequency() //---< Sweep FM Range Forward Actions >---//
{
  int threshold = 1007;
  for ( int i = 850 ; i < 1007 ; i++) { //sweep FM range forward //
    float Frequency = i / 10.0 ;
    radio.setFrequency(Frequency); // pick your own frequency
    lcd.setCursor(1, 3);
    lcd.print("FREQUENCY");
    lcd.setCursor(11, 3); //Start at character 10 on line 1
    lcd.print(i);
    delay(100);
  }
}
//---< Sweep FM Range Forward Actions End  >---//

void ReverseFrequency () //---< Sweep FM Range Reverse Actions >---//
{
  int threshold = 1007;
  for ( int i = 1007 ; i < 850 ; i--) { //sweep FM range forward //
    float Frequency = i / 10.0 ;
    radio.setFrequency(Frequency); // pick your own frequency
    lcd.setCursor(1, 3);
    lcd.print("FREQUENCY");
    lcd.setCursor(11, 3); //Start at character 10 on line 1
    lcd.print(i);
    delay(150);
    //return false;
  }
}
//---< Sweep FM Range Reverse Actions End  >---//


/*

  void sensordata_all() //---< Microwave Sensor Actions >---//
  {
  for (byte x = 0 ; x < (SensorsPinArray) ; ++x) {
  }
  if (SensorsPinArray == HIGH)
  {
    // Serial.println(SensorsPinArray[4]);
    // delay(100);
    for (int i = 0; i < PIXELS; i++) {
      strip.setPixelColor(i, strip.Color(0, 150, 0)); // Moderately bright Red color.
      strip.setBrightness(10); // Brightness
      strip.show(); // This sends the updated pixel color to the hardware.
    }
  }
  if (SensorsPinArray == LOW)
  {
    allpixels_off();
  }
  }
  //---< Microwave Sensor Actions End >---//

*/


void loop() {

  SetTemp();
  tempdata(); // tempdata void
  // sensordata_all();
  FrontSensor(); // Front Sensor affect all Orange pixels when HIGH
  BackSensor();  // Back Sensor affect all Yellow pixels when HIGH
  LeftSensor();  // Left Sensor affect all Pink pixels when HIGH
  RightSensor(); // Right Sensor affect all Cyan pixels when HIGH
}

PaulRB:
Hi Peter, +1 karma for using code tags on your first post. Did you find out how to do that in the "read this" sticky post? If not, please go read that, because right now we don't even know what kind of Arduino you are using.

Apologize i have amended post to add Board used

Peter

Another error: you set up the strip with 572 pixels but one of your loops goes up to 600. This can have bad, wierd side effects such could cause you to chase any number of wild geese thinking the problem is elsewhere.

Apologize i have amended post to add Board used

That was just one of several reasons I asked you to read the forum guide. Please read it and follow it.

PaulRB:
Ok, I see some mistakes there. You should not call strip.show() after setting every individual pixel, only once after updating all pixels (or after changing all pixels that need changing). Also, strip.clear() is the same as setting all pixels to black, so either use that or a loop to set each pixel black, but not both.

Hi Paul

When you say "You should not call strip.show() after setting every individual pixel" what do you mean please?

Peter

peter_b:
Hi Paul

When you say "You should not call strip.show() after setting every individual pixel" what do you mean please?

Peter

OMG i can't believe it i took out all the strip.show() and just put one in the loop and it work so fast now wow
i have spent a month solid pulling my hair out over this... I wouldn't have looked or noticed that would be the problem in a million years... I cant thank you enough

PS it's not perfect as i need to now read more on when to and where to use strip.show()

I will upload code again when completed to show changes :slight_smile:

Thanks
Peter

now read more on when to and where to use strip.show()

That's easy. Use it when you want the strip to show its current state. This may be after you change one LED or after you change several. It depends on what effect you are aiming for

When your code uses strip.setColor(), it's not actually changing a led. What it in fact does is update an element in an array in the Arduino's memory, which corresponds to that led. When your code calls strip.show(), the entire contents of the array, all 572 elements in your case, are sent out to the strip in a single update. Individual LEDs can't be updated without updating all 572 of them. That's why it is slow and should only be done when all the changes you want have been updated in the array.