Problem printing array values

Hello everybody! I have a code that is a logging system, my problem is that I store the readings of a proximity sensor on an array. I use the serial monitor as an interface with commands like "start", "stop", "settings", "getdata", etc. My main problem is that the command "getdata" should print all the values stored in the array on the serial monitor, I used a For loop to do this but it makes the command "start" stop working properly. I commented out the part of the code that makes the start command not to work properly. I was thinking that may be related to the Serial.print(samples[i]); Any ideas?

Thank you guys, have a nice day.

#define USE_TIMER_1 true // You have to define the timer you use
#define USE_TIMER_2 false // You have to define the timer you use
#define SCREENW 128 // OLED display width, in pixels
#define SCREENH 64 // OLED display height, in pixels
#define LED 5

bool isRunning = false;

#include "TimerInterrupt_Generic.h"
#include "ISR_Timer_Generic.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(SCREENW, SCREENH, &Wire, -1);
int TRIGGER = 4;
int ECHO = 3;
volatile int PULSETIMEUS;
float SAMPLETIME;
float SAMPLETIMEINPUT = 100.0;
volatile int DISTANCEMM;
String message;

int val = 0;
int NOSAMPLES = 256;
int samples[256];
int ARRAYVALUES = 0;
volatile int ARRAYVALUES2 = 0;

int analogPin = A0;
int TOPCAPSAMPLE =256;
int BOTTOMCAPSAMPLE = 10;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(TRIGGER, OUTPUT);
  pinMode(ECHO, INPUT);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
}

void loop() {
  SAMPLETIME = SAMPLETIMEINPUT / 1000.0;
  val = analogRead(analogPin);  // read the input pin
  NOSAMPLES = map(val, 1023, 0, BOTTOMCAPSAMPLE, TOPCAPSAMPLE);
  if (Serial.available() > 0) {
    message = Serial.readString();

    if (message == "start") {
      isRunning = true;
      Serial.println("start");
    } 
    else if (message == "stop") {
      isRunning = false;
      display.clearDisplay();
      display.display();
      ARRAYVALUES = 0;
    }

    if (message == "getdata") {
      Serial.println("Samples:");
      // for(int i = 0; i < ARRAYVALUES; i++){
      //   Serial.print(samples[i]);
      //   Serial.print(" ");
      // }
    }
    if (message == "settings") {
      Serial.print("Sample time: ");
      Serial.print(SAMPLETIME);
      Serial.println(" s");
      Serial.print("Number of samples: ");
      Serial.println(NOSAMPLES);
    }
    
    settimer();
  }
  if (isRunning == true) {
    // ITimer1.init();
    // ITimer1.attachInterruptInterval(SAMPLETIMEINPUT, sensor);
    sensor();
    // Read sensor value
    int DISTANCECM = DISTANCEMM / 10;
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0, 0);
    display.print("distance: ");
    display.print(DISTANCECM);
    display.println(" cm");

    display.display();

    led();
  }
} 
void led(){
  digitalWrite(LED, HIGH);
  delay(250);
  digitalWrite(LED, LOW);
}

void sensor() {
  digitalWrite(TRIGGER,LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGGER,HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER,LOW);

  PULSETIMEUS = pulseIn(ECHO,HIGH);

  int DISTANCE = (PULSETIMEUS* 0.034 / .2);
  if (DISTANCE <= 2000 && DISTANCE >= 50){
    DISTANCEMM = DISTANCE;
    if(ARRAYVALUES < NOSAMPLES){
      samples[ARRAYVALUES] = DISTANCEMM; // Store the value in the samples array
      Serial.println(samples[ARRAYVALUES]);
      ARRAYVALUES++; // Increment the array index
      if (ARRAYVALUES == NOSAMPLES){
        ARRAYVALUES = 0;
      }
    }
  }
  // delay(SAMPLETIMEINPUT);
}
void settimer(){
  if (message == "s1"){
    SAMPLETIMEINPUT = 100;
  } else if (message == "s2"){
    SAMPLETIMEINPUT = 200;
  } else if (message == "s3"){
    SAMPLETIMEINPUT = 300;
  } else if (message == "s4"){
    SAMPLETIMEINPUT = 400;
  } else if (message == "s5"){
    SAMPLETIMEINPUT = 500;
  } else if (message == "s6"){
    SAMPLETIMEINPUT = 600;
  } else if (message == "s7"){
    SAMPLETIMEINPUT = 700;
  } else if (message == "s8"){
    SAMPLETIMEINPUT = 800;
  } else if (message == "s9"){
    SAMPLETIMEINPUT = 900;
  } else if (message == "s10"){
    SAMPLETIMEINPUT = 1000;
  } else if (message == "s11"){
    SAMPLETIMEINPUT = 1100;
  } else if (message == "s12"){
    SAMPLETIMEINPUT = 1200;
  } else if (message == "s13"){
    SAMPLETIMEINPUT = 1300;
  } else if (message == "s14"){
    SAMPLETIMEINPUT = 1400;
  } else if (message == "s15"){
    SAMPLETIMEINPUT = 1500;
  } else if (message == "s16"){
    SAMPLETIMEINPUT = 1600;
  } else if (message == "s17"){
    SAMPLETIMEINPUT = 1700;
  } else if (message == "s18"){
    SAMPLETIMEINPUT = 1800;
  } else if (message == "s19"){
    SAMPLETIMEINPUT = 1900;
  } else if (message == "s20"){
    SAMPLETIMEINPUT = 2000;
  }
}


To debug, use the serial.Print() to display each value being used to access the array. Ensure the array boundaries are never exceeded.

Hello! thanks for your reply, I already debugged it, and seems that the "is running" flag is remains true (so that start command works) but it does not refresh (i.e. the OLED screen keeps the same and sensor(); function only runs once.

Fix it!

Your code seems to indicate the use of interrupts but there is no evidence of that. I would suggest trimming out all the superfluous libraries and code to make is clearer and easier to debug.

On a personal note, ALLCAPITOLVARIABLES are terrible. ymmv.

1 Like

Which Arduino are you using?

Due to poor memory management, String variable operations cause Arduinos to crash or misbehave. Avoid using them, especially on with Arduino Uno and the like.

Use C-strings (zero terminated character arrays) instead.

Hello! hope you are doing well, thanks for your reply. I forgot to erase them, I tried to approach the issue of the printing array using timer interrupts but I got same result. Also thanks for the personal note, I will take it into account from now on!

Hello! thanks for your reply, Im using an Arduino uno, but for the presentation, the lecturer asked to do it on an arduino NANO. I will try using C-strings, thanks for the idea. I will update soon to say whether it worked or not.

It seems to me that you do something like resizing the array:

Why do you do this every start of the loop() ? Do you understand that by doing this you are violating the order of saving and subsequent printing of values?
Moreover, this looks like an absolutely meaningless action, because the array of the maximum size has ALREADY BEEN CREATED at this point and you cannot change it.

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