0

Sketch slows down sensing after awhile running
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Sketch slows down sensing after awhile running

by rizla420 on Wed Sep 28, 2016 9:56 am

Hey Guys/Gals,

I was wondering if someone might have an idea on whats causing my sketch to slow down sensing turning on lights (neopixels).

When I power up the arduino, i have a sketch with a couple IR (TCRT5000) sensors that sense an object and turns on 6 neopixel LED's when its a certain height it changes from red to green.

The problem is that when it first starts up it changes the colors almost instantly as expected. So last night i decided to leave it on for about 8 hours. This morning I went and fiddled with it and there was a considerable delay (2 seconds or so) between the sensing and the color changing. Then I decided to open up my serial monitor to see the output and then it started behaving correctly.

Its very weird, not sure what could be causing this. Is there some buffer or something that filling up. I'm using a couple libraries. After compiling for the Sparkfun Pro Micro, I'm using:
Sketch uses 17,334 bytes (60%) of program storage space. Maximum is 28,672 bytes.
Global variables use 798 bytes (31%) of dynamic memory, leaving 1,762 bytes for local variables. Maximum is 2,560 bytes.



I've attached my code below:

Code: Select all | TOGGLE FULL SIZE

#include <Adafruit_NeoPixel.h>
#include "WS2812_Definitions.h"
#include "U8glib.h"
#include "Wire.h"

#define rgbPIN 4
#define LED_COUNT 6

//Analog Mux control digital pins
int s0 = 5;
int s1 = 6;
int s2 = 7;
int s3 = 8;

//TCA9548A i2c MUX for OLED Displays
#define dispMUX         0x70  //Multiplexer Address
#define DISPLAY     0x3C  //OLED Display Address

//74HC4067 Analog MUX "SIG" pin analog input pin
int SIG_pin = 18;

//SET OLED
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);   // I2C / TWI

void drawOn(void) {
  // graphic commands to redraw the complete screen should be placed here 
  u8g.setFont(u8g_font_helvB14);
  u8g.drawStr( 0, 14, "LINE 1");
  u8g.drawStr( 0, 34, "LINE 2");
  u8g.drawStr( 0, 54, "LINE 3");

}
void drawOff(void) {
  // graphic commands to redraw the complete screen should be placed here 
  u8g.drawStr( 0, 14, "LINE 1");
  u8g.drawStr( 0, 34, "LINE 2");
  u8g.drawStr( 0, 54, "LINE 3");

}

void draw(void) {
  // graphic commands to redraw the complete screen should be placed here 


}


// Create an instance of the Adafruit_NeoPixel class called "leds".
Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, rgbPIN, NEO_GRB + NEO_KHZ800);

void setup()
{
    leds.begin();  // Call this to start up the LED strip.

    Serial.begin(115200);


 /* ----------------- START PIN SETUP ----------------- */

    //Address PINS for Analog READ MUX PINS
    pinMode(s0, OUTPUT);
    pinMode(s1, OUTPUT);
    pinMode(s2, OUTPUT);
    pinMode(s3, OUTPUT);
 
    digitalWrite(s0, LOW);
    digitalWrite(s1, LOW);
    digitalWrite(s2, LOW);
    digitalWrite(s3, LOW);

    //RGB LED Control PIN
    pinMode(rgbPIN, OUTPUT);

/* --------------- END PIN SETUP ---------------------- */

/* ------------------ OLED U8GLIB SETUP --------------- */
    //OLED U8GLIB SETUP OPTIONS

  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }   

/* ------------------ END OLED U8GLIB SETUP --------------- */

 /* ----------------------OLED MUX Example portion -------------------------- */

 Wire.begin();

 mux(0);  //select Display on channel 0
 initializeDISP(DISPLAY);
 mux(1);  //select Display on channel 1
 initializeDISP(DISPLAY);
 mux(0xFF);  //disable all channels. This is not required
   
}

/*  FUNCTION DEFINITIONS for sketch */

void mux(byte channel)
{
 byte controlRegister = 0x04; 
 controlRegister |= channel;
 Wire.beginTransmission(dispMUX);
 if (channel == 0xFF){Wire.write(0x00);} //deselect all channels
 else {Wire.write(controlRegister);}     //set to selected channel
 Wire.endTransmission();
}

void initializeDISP(uint8_t address)
{
 Serial.println("Initiating Quick Start");
 Wire.beginTransmission(DISPLAY);
 Wire.write(0x06);
 Wire.write(0x40);
 Wire.write(0x00);
 Wire.endTransmission(); 
}

void sendDISP(int _disp, char rootStr[])
{
 /* Not sure what these lines are for... */
 Wire.beginTransmission(_disp);
 Wire.write(0x04);
 Wire.endTransmission();
 
 // picture loop
 //u8g.begin();
 //u8g.firstPage(); 
 //do {
 //  draw(rootStr);
 //} while( u8g.nextPage() );
}

//ANALOG MUX READ FUNCTION
int readMux(int channel){
  int controlPin[] = {s0, s1, s2, s3};

  int muxChannel[16][4]={
    {0,0,0,0}, //channel 0
    {1,0,0,0}, //channel 1
    {0,1,0,0}, //channel 2
    {1,1,0,0}, //channel 3
    {0,0,1,0}, //channel 4
    {1,0,1,0}, //channel 5
    {0,1,1,0}, //channel 6
    {1,1,1,0}, //channel 7
    {0,0,0,1}, //channel 8
    {1,0,0,1}, //channel 9
    {0,1,0,1}, //channel 10
    {1,1,0,1}, //channel 11
    {0,0,1,1}, //channel 12
    {1,0,1,1}, //channel 13
    {0,1,1,1}, //channel 14
    {1,1,1,1}  //channel 15
  };

  //loop through the 4 sig
  for(int i = 0; i < 4; i ++){
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }

  //read the value at the SIG pin
  int val = analogRead(SIG_pin);

  //return the value
  return val;
}


/* ------------------- START MAIN LOOP ------------------- */
void loop()
{
    // read the input on analog pin 0:
//  int tcrt1 = analogRead(A0);
//  int tcrt2 = analogRead(A1);

int tcrt1=readMux(0);//first sensor
int tcrt2=readMux(1);// second sensor
int thresholdVal=200;


//PRINT CURRENT VALUE OF TCRT SENSORS
Serial.println(tcrt1);
Serial.println("\n");
Serial.println(tcrt2);

//LEDs are assigned in a clockwise direction the PCB


if (tcrt1 > thresholdVal){
Serial.println("SENSOR1 ACTIVATED!");
  Serial.println("\n");
leds.setPixelColor(0,255,0,0);
leds.setPixelColor(1,255,0,0);
leds.setPixelColor(2,255,0,0);

  u8g.firstPage(); 
  do {
    draw();
    drawOff();
  } while( u8g.nextPage() );

}else{
leds.setPixelColor(0,0,255,0);
leds.setPixelColor(1,0,255,0);
leds.setPixelColor(2,0,255,0); 
  u8g.firstPage(); 
  do {
    draw();
    drawOn();
  } while( u8g.nextPage() );

}

if (tcrt2 > thresholdVal ){
  Serial.println("SENSOR2 ACTIVATED!");
  Serial.println("\n");
leds.setPixelColor(3,255,0,0);
leds.setPixelColor(4,255,0,0);
leds.setPixelColor(5,255,0,0);

  u8g.firstPage(); 
  do {
    draw();
    drawOff();
  } while( u8g.nextPage() );

}else{
leds.setPixelColor(3,0,255,0);
leds.setPixelColor(4,0,255,0);
leds.setPixelColor(5,0,255,0);

  u8g.firstPage(); 
  do {
    draw();
    drawOn();
  } while( u8g.nextPage() );

}


//SHOW LEDS
leds.show();



}

/* ------------------- END MAIN LOOP ------------------- */


rizla420
 
Posts: 332
Joined: Thu Feb 05, 2009 9:03 pm

Re: Sketch slows down sensing after awhile running

by adafruit_support_bill on Wed Sep 28, 2016 10:07 am

I don't see anything suspicious in your loop. Try commenting out the display code to see if the delay changes or goes away. That should narrow down the source of the slowdown.

Then I decided to open up my serial monitor to see the output and then it started behaving correctly.

Opening the serial monitor probably triggered a reset, so you were starting from scratch again.

adafruit_support_bill
 
Posts: 67257
Joined: Sat Feb 07, 2009 10:11 am

Re: Sketch slows down sensing after awhile running

by rizla420 on Wed Sep 28, 2016 10:16 am

adafruit_support_bill wrote:I don't see anything suspicious in your loop. Try commenting out the display code to see if the delay changes or goes away. That should narrow down the source of the slowdown.

Then I decided to open up my serial monitor to see the output and then it started behaving correctly.

Opening the serial monitor probably triggered a reset, so you were starting from scratch again.


Thanks Bill for the quick reply!
Thats what I suspect as well... Time to figure out if its the OLED's or the Neopixels. The Analog mux is pretty straight forward.

Do you by anychance have any experience with using the U8GLIB library in conjunction with the TCA? I found some code that I pasted into my sketch, but I havent gotten it to work just yet. I was getting other basic stuff squared away.

rizla420
 
Posts: 332
Joined: Thu Feb 05, 2009 9:03 pm

Re: Sketch slows down sensing after awhile running

by adafruit_support_bill on Wed Sep 28, 2016 10:21 am

I don't have any experience with that library. But I'll ask around to see if anyone else here does.

adafruit_support_bill
 
Posts: 67257
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.