The serial is not being printed

I am trying to get my arduino Nano to print serial data to a screen. The problem is there is a portion of the code that is not printing serial data. I know this because the TX led is not on when it is supposed to.
the serial printing in the setup and log_loop function works fine I believe. It is the standard_loop that has the serial that is not printing.

I have tested the input button connected to the log_button pin, and it is working fine.

#include <DHT.h>

#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht (DHTPIN, DHTTYPE);

#include <Wire.h>
#include <Adafruit_MLX90614.h>

Adafruit_MLX90614 mlx = Adafruit_MLX90614();
#include <SD.h>

#include <SPI.h>

File myFile; //Sets up file commands
// sets up values and pin numbers
int pinCS = 6;

int mode = 0;

int hum = 0;
int temp = 0;

int log_value = 0;
int final_log = 0;
int log_button = 3;

const int trigPin = 9;
const int echoPin = 10;
const int irPin = 3;
long duration;
int distance;

int sensorValue;
int digitalValue;

int ObjectC = 0;
int AmbientC = 0;
int ObjectF = 0;
int AmbientF = 0;

void setup() {
  //Sets up pin modes and begins libraries
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(13, OUTPUT);
  pinMode(irPin, INPUT);
  pinMode(pinCS, OUTPUT);
  pinMode(log_button, INPUT_PULLUP);
  Serial.begin(9600);
  dht.begin();
  mlx.begin();
  //Debug and testing of SD card system
  digitalWrite(LED_BUILTIN, LOW);
  if (SD.begin()) {
    digitalWrite(LED_BUILTIN, LOW);
    myFile = SD.open("test.txt", FILE_WRITE); // Create/Open file
    if (myFile) { // if the file opened okay, write to it:
      myFile.println("Testing text 1, 2 ,3..."); // Write to file
      myFile.close(); // close the file
    } else { // if the file didn't open, print an error:
      digitalWrite(LED_BUILTIN, HIGH);
    }
  } else {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  // Sets the page to the menu
  Serial.print("page Menu");
  next_command();
}

void standard_loop() {
  
  hum = dht.readHumidity() * 10; // DHT temps and humidity readings
  temp = dht.readTemperature() * 10;
  // MQ-135 readings
  sensorValue = analogRead(0); // read analog input pin 0
  digitalValue = digitalRead(2); 

  ObjectC = mlx.readObjectTempC() * 10; // Ambient and Obtect temperatures from IR temp sensor in both Celsius and Farienhight
  AmbientC = mlx.readAmbientTempC() * 10;
  ObjectF = mlx.readObjectTempF() * 10;
  AmbientF = mlx.readAmbientTempF() * 10;
  
  //Gets values form Ultrasonic sensor
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance= duration*0.034/2; //maths to find distance. RIP coolmaths games

  // This entire section is just the arduino sending the values from the sensors to the Nexiton screen via serial

  Serial.print("Range.val=");
  Serial.print(distance * 10);
  next_command();
  
  Serial.print("Humidity.val=");
  Serial.print(hum);
  next_command();
  
  Serial.print("Temp.val=");
  Serial.print(temp);
  next_command();
  
  Serial.print("Toxicity.val=");
  Serial.print(sensorValue, DEC);
  next_command();

  Serial.print("AmbientC.val=");
  Serial.print(AmbientC); 
  next_command();
  
  Serial.print("ObjectC.val=");
  Serial.print(ObjectC);
  next_command();
  
  Serial.print("AmbientF.val=");
  Serial.print(AmbientF);
  next_command();
  
  Serial.print("ObjectF.val=");
  Serial.print(ObjectF);
  next_command();
  
}

void log_loop() {
  
  Serial.print("page Log"); //Displays the log page, locking the user from the regular gui because it won't even work while its logging
  next_command();
  
  for(int i = 1; i<60; i++) {//every 5 seconds, for 5 minutes or 60 times, the MQ-135 logs the value and then adds it to the reading pool
    sensorValue = analogRead(0);
    log_value = log_value + sensorValue ;
    delay(5000);
  }
  final_log = log_value / 60; //The average of all the readings is taken

  //the reading is sent to the SD Card
  
  myFile = SD.open("data.txt", FILE_WRITE); // Create/Open file
  if (myFile) { // if the file opened okay, write to it:
    myFile.println(final_log); // Write to file
    myFile.close(); // close the file
  } else { // if the file didn't open, print an error:
    digitalWrite(LED_BUILTIN, HIGH);
  }
  Serial.print("page Menu"); //Puts the screen back into the main interface
  next_command();
  mode = 0; //the normal loop is reenabled
}

void next_command(){
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
}

void loop() {
  int button = digitalRead(log_button); //Checks if the log button has been pressed
  if (button == LOW) {
    log_loop();
  } 
  standard_loop();
  delay(100);  //Prevents burnout
}

For reference, this is a previous iteration if code that worked properly, in terms of only the standard_loop code, the only difference is that the previous iteration did not have the code in functions.

  #include <DHT.h>

#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht (DHTPIN, DHTTYPE);

#include <Wire.h>
#include <Adafruit_MLX90614.h>

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

int hum = 0;
int temp = 0;

const int trigPin = 9;
const int echoPin = 10;
const int irPin = 3;
long duration;
int distance;

int sensorValue;
int digitalValue;

int ObjectC = 0;
int AmbientC = 0;
int ObjectF = 0;
int AmbientF = 0;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(13, OUTPUT);
  pinMode(irPin, INPUT);
  Serial.begin(9600);
  dht.begin();
  mlx.begin(); 
}

void loop() {
  hum = dht.readHumidity() * 10;
  temp = dht.readTemperature() * 10;

  sensorValue = analogRead(0); // read analog input pin 0
  digitalValue = digitalRead(2); 

ObjectC = mlx.readObjectTempC() * 10;
AmbientC = mlx.readAmbientTempC() * 10;
ObjectF = mlx.readObjectTempF() * 10;
AmbientF = mlx.readAmbientTempF() * 10;

  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance= duration*0.034/2;
  // Prints the distance on the Serial Monitor
  Serial.print("Range.val=");
  Serial.print(distance * 10);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  
  Serial.print("Humidity.val=");
  Serial.print(hum);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  
  Serial.print("Temp.val=");
  Serial.print(temp);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  
  Serial.print("Toxicity.val=");
  Serial.print(sensorValue, DEC);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);

  Serial.print("AmbientC.val=");
  Serial.print(AmbientC); 
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  
  Serial.print("ObjectC.val=");
  Serial.print(ObjectC);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  
  Serial.print("AmbientF.val=");
  Serial.print(AmbientF);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  
  Serial.print("ObjectF.val=");
  Serial.print(ObjectF);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);

  if (hum >=700 ) {
    digitalWrite(LED_BUILTIN, LOW);
  } else {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  delay(100);
}

Also, right now, the SD card reader is not connected, in case that may be the problem

I do not know if this is the source of your problem, but it is an issue.

const int echoPin = 10;
pinMode(echoPin, INPUT); // Sets the echoPin as an Input

For SPI to work, pin 10 must be set as an output.

From the SPI library reference:

All AVR based boards have an SS pin that is useful when they act as a slave controlled by an external master. Since this library supports only master mode, this pin should be set always as OUTPUT otherwise the SPI interface could be put automatically into slave mode by hardware, rendering the library inoperative.

The echo pin is used to operate a ultrasonic sensor, the reading from the sensor is read by the echo pin. And this setup was working with the previous iteration of the code, included at the bottom of my post.

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