Unresponsive programme

This programme hangs after an hour or so (time to be determined).
It stops responding to button presses requiring removal of power to restore it.
Previously it ran all day until I added the pushbuttons.

/**************************************************************************
 This is an example for our Monochrome OLEDs based on SSD1306 drivers

 Pick one up today in the adafruit shop!
 ------> http://www.adafruit.com/category/63_98

 This example is for a 128x64 pixel display using I2C to communicate
 3 pins are required to interface (two I2C and one reset).

 Adafruit invests time and resources providing this open
 source code, please support Adafruit and open-source
 hardware by purchasing products from Adafruit!

 Written by Limor Fried/Ladyada for Adafruit Industries,
 with contributions from the open source community.
 BSD license, check license.txt for more information
 All text above, and the splash screen below must be
 included in any redistribution.
 **************************************************************************/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Adafruit_VEML6075.h"
#include <ezButton.h>
Adafruit_VEML6075 uv = Adafruit_VEML6075();

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define NUMFLAKES     10 // Number of snowflakes in the animation example

#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16
ezButton button1(2);  // create ezButton object that attach to pin 2;
ezButton button2(3);  // create ezButton object that attach to pin 3;
ezButton button3(4);  // create ezButton object that attach to pin 4;
ezButton button4(5);  // create ezButton object that attach to pin 5;
ezButton button5(6);  // create ezButton object that attach to pin 6;

float dose_div;

void setup() {
  Serial.begin(9600);
  button1.setDebounceTime(50); // set debounce time to 50 milliseconds
  button2.setDebounceTime(50); // set debounce time to 50 milliseconds
  button3.setDebounceTime(50); // set debounce time to 50 milliseconds
  button4.setDebounceTime(50); // set debounce time to 50 milliseconds
  button5.setDebounceTime(50); // set debounce time to 50 milliseconds

  Serial.println("VEML6075 Full Test");
  if (! uv.begin()) {
    Serial.println("Failed to communicate with VEML6075 sensor, check wiring?");
  }
  Serial.println("Found VEML6075 sensor");

  // Set the integration constant
  uv.setIntegrationTime(VEML6075_100MS);dose_div=10;
  // Get the integration constant and print it!
  Serial.print("Integration time set to ");
  switch (uv.getIntegrationTime()) {
    case VEML6075_50MS: Serial.print("50"); break;
    case VEML6075_100MS: Serial.print("100"); break;
    case VEML6075_200MS: Serial.print("200"); break;
    case VEML6075_400MS: Serial.print("400"); break;
    case VEML6075_800MS: Serial.print("800"); break;
  }
  Serial.println("ms");

  // Set the high dynamic mode
  uv.setHighDynamic(true);
  // Get the mode
  if (uv.getHighDynamic()) {
    Serial.println("High dynamic reading mode");
  } else {
    Serial.println("Normal dynamic reading mode");
  }

  // Set the mode
  uv.setForcedMode(false);
  // Get the mode
  if (uv.getForcedMode()) {
    Serial.println("Forced reading mode");
  } else {
    Serial.println("Continuous reading mode");
  }
  uv.setCoefficients(2.22, 1.33,  // UVA_A and UVA_B coefficients
                     2.95, 1.74,  // UVB_C and UVB_D coefficients
                     0.001461, 0.002591); // UVA and UVB responses
  //SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
   Serial.println(F("SSD1306 allocation failed"));
   for(;;); // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  // display.display() is NOT necessary after every single drawing command,
  // unless that's what you want...rather, you can batch up a bunch of
  // drawing operations and then update the screen all at once by calling
  // display.display(). These examples demonstrate both approaches...

 //testdrawline();      // Draw many lines
 // testdrawrect();      // Draw rectangles (outlines)
 // testdrawchar();      // Draw characters of the default font

  display.setTextSize(3);      // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE); // Draw white text
  display.setCursor(0, 0);     // Start at top-left corner
  display.cp437(true);         // Use full 256 char 'Code Page 437' font

  // Not all the characters will fit on the display. This is normal.
  // Library will draw what it can and the rest will be clipped.


}


float UVA_Dose =0;
float UVB_Dose =0;
float UVA_Dose_inc =0;
float UVB_Dose_inc =0;
float INDEX;
float UVA_mW;
float UVB_mW;
int j;
int mode;  //1 is read UVB and UVA in mW cM^2
           //2 is UVA Dose in mJ
           //3 is UVB dose in mJ


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void loop() {
  
  button1.loop(); // MUST call the loop() function first
  button2.loop(); // MUST call the loop() function first
  button3.loop(); // MUST call the loop() function first
  button4.loop(); // MUST call the loop() function first
  button5.loop(); // MUST call the loop() function first
  int btn1State = button1.getState();
  int btn2State = button2.getState();
  int btn3State = button3.getState();
  int btn4State = button4.getState();
  int btn5State = button5.getState();

  if(button1.isReleased()){
    Serial.println("The button 1 is released");
    // Code to initialise display for reading uv goes here
    init_display_reading();
    mode=1;
    
  }

    if(button2.isReleased())
    {
    Serial.println("The button 2 is released");
    // Code to initialise display for dose reading goes here
     init_display_dose('A');
     mode=2;
  }

  if(button3.isReleased()){
    Serial.println("The button 3 is released");
   
    mode=3;
  
  }
  if(button4.isReleased()){
    Serial.println("The button 4 is released");
  
     mode=4;
  }
  if(button5.isReleased()){
    Serial.println("The button 5 is released");
       init_display_dose('B');
    mode=5;
  }

switch (mode) {
  case 1://UVA and UVB reading mode
     display_reading(UVA_mW,UVB_mW,INDEX);    
    break;
  case 2://UVA dose in mJ 
    display_dose(UVA_Dose);
     
    //
    break;
  case 3:// 
  
     
    //
    break;    
  case 4:
    // 
    break;

    break;    
  case 5:// UVB Dose in millijoules
    display_dose(UVB_Dose);
    break;


  default:
    // if nothing else matches, do the default
    // default is optional
    break;
}


 //Serial.print("Raw UVA reading:  "); Serial.println(String(uv.readUVA()));
// Serial.print("Raw UVB reading:  "); Serial.println(String(uv.readUVB()));
 //Serial.print("UV Index reading: "); Serial.println(String(uv.readUVI()));


UVA_mW =((uv.readUVA())/1000);
UVB_mW =((uv.readUVB())/1000);
INDEX =((uv.readUVI()));

UVA_Dose_inc =  UVA_Dose_inc + UVA_mW;
UVB_Dose_inc =  UVB_Dose_inc + UVB_mW;
j++;

delay(10); 
Serial.println((j));

if (j>8) {Serial.println(String(UVA_Dose_inc));
UVA_Dose = UVA_Dose + (UVA_Dose_inc/10); // udate total Dose in millijoules every second
UVB_Dose = UVB_Dose + (UVB_Dose_inc/10); // udate total Dose in millijoules every second
j = 0;
UVA_Dose_inc = 0;
UVB_Dose_inc = 0;

}

 //Serial.print("UVA Dose:  ");Serial.println(UVA_Dose);
 //Serial.print("UVB Dose:  ");Serial.println(UVB_Dose);
 }
//++++++++++++++++++++++++++ Functions Below +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

void display_reading(float UVA_mW,float UVB_mW,float INDEX) // Value is expected to be in mW per cm squared
{

  
  char  x;
  char  y;
  char  z;
  char  w;
  char charBuf[8];
  
  String stringOne = (String((UVA_mW)));
  stringOne.toCharArray(charBuf,8);
  w=stringOne[0];
  x=stringOne[1];
  y=stringOne[2];
  z=stringOne[3];

  display.fillRect(78, 6,49,14, SSD1306_BLACK);
  display.setTextSize(2) ;
  display.setCursor(78,6);   
  display.write(w);
  display.write(x);
  display.write(y);
  display.write(z);

  stringOne = (String((UVB_mW)));
  stringOne.toCharArray(charBuf,8);
  w=stringOne[0];
  x=stringOne[1];
  y=stringOne[2];
  z=stringOne[3];
  display.fillRect(78, 32,49,14, SSD1306_BLACK);
  display.setTextSize(2) ;
  display.setCursor(78,32);   
  display.write(w);
  display.write(x);
  display.write(y);
  display.write(z);
  display.display();
}


void init_display_reading(void){ 
  display.clearDisplay();
  display.drawRect(0, 0, display.width(), display.height(), SSD1306_WHITE);
  display.setTextSize(3) ;
  display.setCursor(5, 6);   
  display.write('U');
  display.write('V');
  display.write('A');
  display.setCursor(5, 34);   
  display.write('U');
  display.write('V');
  display.write('B');
  
  display.setTextSize(1);  
  display.setCursor(80, 50);   
  display.write('m');
  display.write('W');
  display.write('/');
  display.write('c');
  display.write('m');
  display.write('^');
  display.write('2');

  display.setTextSize(1);  
  display.setCursor(80, 23);   
  display.write('m');
  display.write('W');
  display.write('/');
  display.write('c');
  display.write('m');
  display.write('^');
  display.write('2');
  display.display();
}


void init_display_dose(char uv)
{ 

display.clearDisplay();
  display.drawRect(0, 0, display.width(), display.height(), SSD1306_WHITE);
  display.setTextSize(2) ;
  display.setCursor(18, 6);   
  display.write('U');
  display.write('V');
  if (uv == 'A')display.write('A');
  if (uv == 'B')display.write('B');
  display.write(' ');
  display.write('D');
  display.write('O');
  display.write('S');
  display.write('E');

  display.setTextSize(1) ;
  display.setCursor(32, 50);   
  display.write('m');
  display.write('i');
  display.write('l');
  display.write('l');
  display.write('i');
  display.write('J');
  display.write('o');
  display.write('u');
  display.write('l');
  display.write('e');
  display.write('s');
  display.display();
}


void display_dose(float DOSE) //Dose is expected in millijoules

{
  
  display.fillRect(20, 28,124,14, SSD1306_BLACK);
  
  char  u;
  char  v; 
  char  w;
  char  x;
  char  y;
  char  z;

  char charBuf[8];
  
  String stringOne = (String((DOSE)));
  stringOne.toCharArray(charBuf,8);

  u=stringOne[0];
  v=stringOne[1];  
  w=stringOne[2];
  x=stringOne[3];
  y=stringOne[4];
  z=stringOne[5];
  
  display.setTextSize(2) ;
  display.setCursor(20,28);  
  display.write(u);
  display.write(v); 
  display.write(w);
  display.write(x);
  display.write(y);
  display.write(z);
  display.display();


}




No allocation for the terminating null character?

What output do you get on Serial Monitor?

Really? I doubt that you are required to send individual characters like that...

Also, you are using the String class with a display that is known to gobble up free RAM. The remaining memory may not leave much space for the heap. The String class may be crashing due to memory fragmentation because of that.

I will investigate that.
Thanks, i do not claim to be an expert programmer.

It's okay. That hidden problem has burned thousands of people.

I have been unable to spot a string type write function for the library.
I thought there must be one.

Depends on what debug prints I use.
Inside the programme does not work (no UV light).
Outside on battery I have no computer access.

I had to upgrade to a Mega because RAM was suggested to be an earlier problem.

There is UV light indoors. Many types of electric lamps emit UV.

Maybe that just increases the time before it runs out of memory... try re-writing the sketch without any use of the String class. Very few applications truly need it.

Check the OLED library example sketches for examples of writing characters strings. Trust me, it's provided.

The sensor i am using (VEML 6075) does not seem to respond to it , i have LED lighting which is somewhat monochromatic. Need to get a flourescent tube i think.

OK i give up.
Where is the programming reference for the GFX library please ?

Adafruit.

Thank you I was looking at git hub.

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