How can I have my Arduino read data without serial monitor?

I'm still quite new to this. I used my Arduino Nano 33 ble to become an accelerometer and depending on which direction it moved, it lights up different LED's on my board. However, this only works when it is plugged into my computer with the serial monitor open. IF I just plug in my breadboard and supply it with power that way, nothing happens and it apparently is not reading any data. How can I get it to read the data without having it plugged into my computer?

This is my code, in which I just modified the example code given for the accelerometer.

#include <Arduino_LSM9DS1.h>

int Green1 = D2;
int Yellow1 = D3;
int Red1 = D4;
int Green2 = D5;
int Yellow2 = D6;
int Red2 = D7;
int Z1 = D8;
int Z2 = D9;

void setup() {
pinMode(Green1,OUTPUT);
pinMode(Yellow1,OUTPUT);
pinMode(Red1,OUTPUT);
pinMode(Green2,OUTPUT);
pinMode(Yellow2,OUTPUT);
pinMode(Red2,OUTPUT);
pinMode(Z1,OUTPUT);
pinMode(Z2,OUTPUT);

Serial.begin(9600);
while (!Serial);
Serial.println("Started");

if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}

Serial.print("Accelerometer sample rate = ");
Serial.print(IMU.accelerationSampleRate());
Serial.println(" Hz");
Serial.println();
Serial.println("Acceleration in G's");
Serial.println("X\tY\tZ");
}

void loop() {
float x, y, z;

if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);

Serial.print(x);
if (x <= .05 && x >= -.05){
digitalWrite(Green1,HIGH);
digitalWrite(Yellow1,LOW);
digitalWrite(Red1,LOW);
}else if(x > 0.05){
digitalWrite(Yellow1,HIGH);
digitalWrite(Green1,LOW);
digitalWrite(Red1,LOW);
}else{
digitalWrite(Red1,HIGH);
digitalWrite(Yellow1,LOW);
digitalWrite(Green1,LOW);
}
Serial.print('\t');
Serial.print(y);
if(y >= -0.05 && y <= 0.2){
digitalWrite(Green2,HIGH);
digitalWrite(Yellow2,LOW);
digitalWrite(Red2,LOW);
}else if(y > 0){
digitalWrite(Red2,HIGH);
digitalWrite(Yellow2,LOW);
digitalWrite(Green2,LOW);
}else{
digitalWrite(Yellow2,HIGH);
digitalWrite(Red2,LOW);
digitalWrite(Green2,LOW);
}
Serial.print('\t');
Serial.println(z);
if (z > .75){
digitalWrite(Z2,HIGH);
digitalWrite(Z1,LOW);
}else{
digitalWrite(Z1,HIGH);
digitalWrite(Z2,LOW);
}
delay(150);
}
}

Change

while (!Serial);

to

//while (!Serial);

and report back :slight_smile:

I figured it would have something to do with commenting something out, but just did not know which part. Thank you so much!

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