1

I am building an art piece that takes in input from three rotary encoders and two buttons. and outputs data to a large neopixel display and 2 large 4 digit seven segment displays. The displays are powered by the arduino 5V the neopixels are powered by a separate power source.

Everything works exactly as expected for a time. Then seemingly at random the code discontinues responding to any input. The seven segment displays continue to light up.

I believe it might be a power issue with the arduino overheating due to the power consumption of the seven segment displays (~300mA), however, the arduino is still "on" or reviecing power.

I have attached my code below as I worry that problem might come from a runtime error there rather that a power issue.

//3 rotary encoders
//2 buttons

//Displays:
//1 8x32 neopixel array
//2 4 digit 7 segment displays
//2 button LEDs

#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
#include <Adafruit_VS1053.h>

//Music Maker Logic/Variables
#define mp_mcs 7
#define mp_dcs 6
#define mp_dreq 3
#define mp_ccs 4 
#define mp_reset -1

char* music="TRACK001.wav";

//Music maker constructor
Adafruit_VS1053_FilePlayer mp = 
  Adafruit_VS1053_FilePlayer (mp_reset, mp_mcs, mp_dcs, mp_dreq, mp_ccs);

//Neopixel Logic/Variables
#define neoPixelPin 26
#define pixelNumber 256
#define neoInterupt 50

Adafruit_NeoPixel neoArray = Adafruit_NeoPixel(pixelNumber, neoPixelPin, NEO_GRB + NEO_KHZ800);
uint32_t color;
bool flip;
bool nullColor[32] = {false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false};
bool colorTable[32] = {false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false};
long currentTime;
long pastTime;
long startingTime;
long pastTimeAlt;

//Encoder Logic/Variables
#define outputA1 40
#define outputB1 41
#define outputA2 42
#define outputB2 43
#define outputA3 44
#define outputB3 45

byte a1State;
byte a1LastState;
byte a2State;
byte a2LastState;
byte a3State;
byte a3LastState;
byte topDay;

//7 Segment Logic/Variables
Adafruit_7segment matrix1 = Adafruit_7segment();
Adafruit_7segment matrix2 = Adafruit_7segment();

byte month = 1;
byte day = 1;
short year = 2000;
String holder;
String holder1;
String holder2;

//button Logic/Variables
#define altResetPin 30
#define engPin 32


void setup() {
  Serial.begin(9600);
  //Serial.println("In setup");
  //Input initializations
  pinMode(altResetPin, INPUT_PULLUP);
  pinMode(engPin, INPUT_PULLUP);
  pinMode(outputA1, INPUT_PULLUP);
  pinMode(outputB1, INPUT_PULLUP);
  pinMode(outputA2, INPUT_PULLUP);
  pinMode(outputB2, INPUT_PULLUP);
  pinMode(outputA3, INPUT_PULLUP);
  pinMode(outputB3, INPUT_PULLUP);
  //pinMode(neoInterupt, INPUT);

  Serial.println("made it to structures");

  //begin structures
  neoArray.begin();
  neoArray.setBrightness(75);
  neoArray.show();
  Serial.println("Neo Array initialize");
  matrix1.begin(0x70);
  matrix2.begin(0x72);
  matrix1.setBrightness(4);
  matrix2.setBrightness(4);
  Serial.println("matrix initialize");
  //attachInterrupt(digitalPinToInterrupt(engPin), neoPixelSuprise, LOW);

  Serial.println("made it to initializations");

  //Music Player initialization
  if (! mp.begin()) { 
   Serial.println(F("Music Maker connection failed, or not present"));
   while (1); // don't do anything more
  }
  Serial.println(F("Music Maker connection successful"));

  //SD card Check
  if (!SD.begin(mp_ccs)) {
   Serial.println(F("SD failed, or not present"));
   while (1);  // don't do anything more
  }
  Serial.println(F("SD Inserted and found"));

  mp.setVolume(10,10);
  mp.useInterrupt(VS1053_FILEPLAYER_PIN_INT);

  //initial Reads
  a1LastState = digitalRead(outputA1);
  a2LastState = digitalRead(outputA2);
  a3LastState = digitalRead(outputA3);

  //initial Outputs
  matrix1.print(combine(month,day),DEC);
  matrix1.writeDisplay();

  matrix2.print(year,DEC);
  matrix2.writeDisplay();

}

void loop() {
  //read current states
  a1State = digitalRead(outputA1);
  a2State = digitalRead(outputA2);
  a3State = digitalRead(outputA3);

  //deal with encoder readings
  //first encoder, encodes the month
   if (a1State != a1LastState){     
     // If the B state is different to the A state, that means the encoder is rotating clockwise
     if (digitalRead(outputB1) != a1State) { 
       //clockwise rotation
       if(month == 12){
        month = 1;
       }else{
        month ++;
       }
     } else {
        //cc-rotation
       if(month == 1){
        month = 12; 
       }else{
        month--;
       }
     }
    } 
   a1LastState = a1State; // Updates the previous state of the outputA with the current state

   //second encoder, encodes the day
   if (a2State != a2LastState){     
     // If the B state is different to the A state, that means the encoder is rotating clockwise
     topDay = daysInMonth(month,year);
     if (digitalRead(outputB2) != a2State) { 
       //clockwise rotation
       if(day >= topDay){
        day = 1;
       }else{
        day ++;
       }
     } else {
        //cc-rotation
       if(day == 1){
        day = topDay; 
       }else{
        day--;
       }
     }
    } 
   a2LastState = a2State; // Updates the previous state of the outputA with the current state

   //third encoder. encodes the year
   if (a3State != a3LastState){     
     // If the B state is different to the A state, that means the encoder is rotating clockwise
     if (digitalRead(outputB3) != a3State) { 
       //clockwise rotation
       if(year == 2100){
        year = 1900;
       }else{
        year ++;
       }
     } else {
        //cc-rotation
       if(year == 1900){
        year = 2100; 
       }else{
        year--;
       }
     }
    } 
   a3LastState = a3State; // Updates the previous state of the outputA with the current state

  //update 7segment display
  matrix1.print(combine(month,day),DEC);
  matrix1.writeDisplay();

  matrix2.print(year,DEC);
  matrix2.writeDisplay();

  //read buttons and act on them
  if(digitalRead(altResetPin)==HIGH){
    day = 1;
    month = 1;
    year = 2000;
  }

  if(digitalRead(engPin)==LOW){
    neoPixelSuprise();  
  }
}

void neoPixelSuprise(){
  //put a crazy neopixel event here
  Serial.println("EngPin Hit");
  startingTime = millis();
  pastTime = millis();
  currentTime = millis();
  pastTimeAlt = millis();
  bool holdList[32] ={false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false};
  byte counter = 0;

  mp.startPlayingFile(music);

  while(currentTime - startingTime > 8000){
    currentTime = millis();

    if(currentTime - pastTimeAlt> 100){
      colorTable[counter] = flipper(colorTable[counter]);
      colorTable[31-counter] = flipper(colorTable[31-counter]);
      pastTimeAlt = currentTime;
    }

    //flickerControl
    if(currentTime - pastTime > 50){
      flip = flipper(flip);
      if(flip){
        fillRow(colorTable);
      }else{
        fillRow(nullColor);
      }
      pastTime = currentTime;
    }


    counter = counter+2;
    if(counter>31){
      counter = 0;
    }
  }
}

void fillRow(bool truthList[32]){
  //fills the panel with random color
  for(int i=0; i<32; i++){
    randomSeed(analogRead(A11));
    if(truthList[i]){
      //fills particular row with random color
      for(int j=0; j<8; j++){
        color = Adafruit_NeoPixel::Color(random(255),random(255),random(255));
        neoArray.setPixelColor((i*8)+j,color);
      }
    }else{
      //turns off particular row
      for(int j=0; j<8; j++){
        color = Adafruit_NeoPixel::Color(0,0,0);
        neoArray.setPixelColor((i*8)+j,color);
      }
    }  
  }
}

bool flipper(bool whatIs){
  //flips a boolean
  if(whatIs){
    whatIs = false;
  }else{
    whatIs = true;
  }
  return whatIs;
}

byte daysInMonth(byte month, byte year){
  //based on the year it will tell you how many days there were in that given month
  //leap year condition, if month is 2
  if(month == 2){
    if((year%400==0) || (year%4==0 && year%100!=0)){ 
      return 29;
    }else{  
      return 28;
    }
  }
  //months which have 31 days
  else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month==12){ 
    return 31;
  }else{    
    return 30;
  }
}

short combine(byte month, byte day){
  //takes the two bytes and truns them into one long digit
  holder1 = String(month);
  holder2 = String(day);

  if(holder1.length()==1){
    holder1 = "0" + holder1;
  }
  if(holder2.length()==1){
    holder2 = "0" + holder2;
  }


  holder = holder1 + holder2;

  return(holder.toInt());
}
  • don't use String class – Juraj May 26 '19 at 14:39
  • char* is equivalent correct? I will look it up as well, however, what is so wrong with String class? – Andrew A. May 26 '19 at 14:41
  • Your combine() function seems like you were searching for the most convoluted way of computing 100*month+day. – Edgar Bonet May 26 '19 at 16:06
  • @Juraj thank you for the cool link! – Andrew A. May 26 '19 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.