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;
}
}