Finishing up a project here and am having an issue that is bugging me greatly. I can compile/upload with no errors to my MEGA2560, on IDE 1.8.12.
My code is running a self identification sequence (I modified from the popular i2c_scanner) that pings I2C sensors attached to a breadboard. (some are not plugged in, but in case they were, thats why certain code is there.)
My issue is that in the serial monitor, the last statement it prints out is 'done' in the void loop, and then jumps back to the beginning of setup.
I have made comments in all caps yelling at you to see where the problems supposedly starts/stops.
Thanks in advance if you notice anything!
Russell
// Russell Jones
// Electrical and COmputer Engineering, University of Missouri-Kansas City
// Senior Design 403
#include <Wire.h>
#include "Adafruit_MCP9808.h"
#include "Adafruit_MPL115A2.h"
#include "Adafruit_Si7021.h"
#include "Adafruit_VCNL4010.h"
// Create the sensor objects
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808(); // Address defauts to 0x18
Adafruit_MPL115A2 mpl115a2 = Adafruit_MPL115A2(); // Object Created for Barometric Pressure
Adafruit_Si7021 si7021 = Adafruit_Si7021(); // Object Created for Humidity Sensor
Adafruit_VCNL4010 vcnl; // Object Created for Proximity Sensor
#define WIRE_PORT Wire // Your desired Wire port.
// FOR ANY COMMANDS DEALING WITH SENSOR 2, tempsensor2 must replace tempsensor for operations.
// FOR EXAMPLE - tempsensor.readTempC() READS CELSIUS TEMPERATURE OF SENSOR 1
// FOR EXAMPLE - tempsensor2.readTempF() READS FARHENHEIGHT TEMPERATURE OF SENSOR 2
// Si7021 - Temperature and Humidity sensor: Address - 0x40
// MPL115A2 - Barometric Pressure/Temperature Sensor: Address - 0x60
// VCNL4010 - Proximity Sensor: Address - 0x13
void setup()
{
Wire.begin();
Serial.begin(9600);
while (!Serial);
//Waits for Serial Terminal to be Open Prior to Running Script
Serial.println("Senior Design / ESP32 Module Code"); //CODE JUMPS BACK HERE
delay(2000);
Serial.print("\n");
}
void loop()
{
byte error, address;
int devices;
devices = 0;
for(address = 1; address < 127; address++ )
{
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
if (address == 0x10) Serial.print("UV sensor found, but more code needed to run in current project state.");
delay(50);
if (address == 0x13) Serial.println("Found VCNL4010 Proximity Sensor / Address (0x13)");
delay(50);
if (address == 0x18) Serial.println("Found MCP9808 / Address (0x18)");
delay(50);
if (address == 0x19) Serial.println("Found LSM303 Accelerometer / Address (0x19). More Code/Library is Required to run this Sensor.");
delay(50);
if (address == 0x20) Serial.println("Found Chirp! Water Sensor / Address (0x20). More Code/Library is Required to run this Sensor.");
delay(50);
if (address == 0x28) Serial.println("Found PCT2075 Temperature Sensor / Address (0x28). More Code/Library is Required to run this Sensor.");
delay(50);
if (address == 0x29) Serial.println("Found TSL2591 Light Sensor / Address (0x29). More Code/Library is Required to run this Sensor.");
delay(50);
if (address == 0x1E) Serial.println("Found 9DOF / Address (0x1E). More Code/Library is Required to run this Sensor.");
delay(50);
if (address == 0x31) Serial.println("Found CAP1188 8 Channel Capacitive Touch Sensor / Address (0x31). More Code/Library is Required to run this Sensor.");
delay(50);
if (address == 0x33) Serial.println("Found MLX90640 IR Thermal Camera / Address (0x33). More Code/Library is Required to run this Sensor.");
delay(50);
if (address == 0x38) Serial.println("Found VEML6070 UV Index / Address (0x38). More Code/Library is Required to run this Sensor.");
delay(50);
if (address == 0x39) Serial.println("Found APDS-9960 IR/Color/Proximity Sensor / Address (0x39). More Code/Library is Required to run this Sensor.");
delay(50);
if (address == 0x40) Serial.println("Found Si7021 Humidity/Temp Sensor / Address (0x18)");
delay(50);
if (address == 0x60) Serial.println("Found MPL115A2 Barometric Pressure/Temperature Sensor / Address (0x18)");
delay(50);
devices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (devices == 0){
Serial.println("No I2C devices found\n");
}
else{
Serial.println("done\n"); //CODE SEEMS TO STOP HERE
delay(2000);
}
// Start Code for Temperature Sensor MCP9808
float c = tempsensor.readTempC();
float f = tempsensor.readTempF();
Serial.print("Temp: ");
Serial.print(c, 4); Serial.print("*C\t and ");
Serial.print(f, 4); Serial.println("*F.");
delay(3000);
Serial.print("\n");
// Start Code for Barometric Sensor MPL115A2
Serial.println("Reading Device MPL115A2 ");
Serial.print("\n");
delay(1000);
float pressureKPA = 0, temperatureC = 0;
pressureKPA = mpl115a2.getPressure();
Serial.print("Pressure (kPa): "); Serial.print(pressureKPA, 4); Serial.println(" kPa");
Serial.print("\n");
delay(3000);
// Start Code for Humidity (Si7021) Sensor
Serial.println("Reading Device Si7021 ");
Serial.print("\n");
delay(1000);
Serial.print("Humidity: ");
Serial.print("\n");
Serial.print(si7021.readHumidity(), 2);
Serial.print("\n");
delay(3000);
//Start Code for Proximity (VCNL4010) Sensor
Serial.println("Reading Device VCNL4010 ");
Serial.print("\n");
delay(1000);
Serial.print("Ambient: "); Serial.println(vcnl.readAmbient());
Serial.print("Proximity: "); Serial.println(vcnl.readProximity());
delay(2000);
Serial.print("\n");
delay(3000);
}
I notice that the code first scans for various devices and then proceeds to read those devices whether they are present or not.
I would expect to see the reading of a device only when you detect its presence. I would expect to see functions other than just the loop - NOT the void loop - void is simply the type of variable it returns it is not part of the function’s name.
As all the real work is done by libraries it is hard to say what it will do if the sensor is not present.
your code probably crashes your arduino if you try to access non existing resources and the arduino reboot, hence goes back to the setup
May be the libraries you use would require possibly to have a begin() or some sort of setup being executed too or are not happy to be instantiated if the device is missing.
Grumpy_Mike:
I notice that the code first scans for various devices and then proceeds to read those devices whether they are present or not.
I would expect to see the reading of a device only when you detect its presence. I would expect to see functions other than just the loop - NOT the void loop - void is simply the type of variable it returns it is not part of the function’s name.
As all the real work is done by libraries it is hard to say what it will do if the sensor is not present.
You are correct on both of your assumptions. However, it reacts quite well to the sensors not present, ie. does not print the statement at their address. Im just unsure why it makes it through the else and then does not follow the flow of the code. (The codes seperately work, so I might try to remove all the sensors that 'arent' plugged in and see what happens.
J-M-L:
your code probably crashes your arduino if you try to access non existing resources and the arduino reboot, hence goes back to the setup
May be the libraries you use would require possibly to have a begin() or some sort of setup being executed too or are not happy to be instantiated if the device is missing.
This is also on my list, but I figured if it made it through the initial 'if' statement for checking if the sensor was there or not, it didn't require the library. But I may be wrong.
J-M-L:
your code probably crashes your arduino if you try to access non existing resources and the arduino reboot, hence goes back to the setup
May be the libraries you use would require possibly to have a begin() or some sort of setup being executed too or are not happy to be instantiated if the device is missing.
Removing all of the sensor if statements didn't change much. I also added in a Serial.print("test"); after the 2000 delay with no luck of seeing that on the monitor as well.
Removing all of the sensor if statements didn't change much.
When ever you make an unsuccessful change you are supposed to post the new code so we can check it again.
However, it reacts quite well to the sensors not present, ie. does not print the statement at their address. Im just unsure why it makes it through the else and then does not follow the flow of the code.
That is because you are in control of the access when you are checking if a device is present, but you are not when you try and read a none existent device. That is done by the library and we have no idea how it copes with that.
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();
It is possible that the above line and others like it return a null if there is no device fitted. Then when you try an read the device you jump to a null, which is very much like a power up reset.
Grumpy_Mike:
When ever you make an unsuccessful change you are supposed to post the new code so we can check it again.
That is because you are in control of the access when you are checking if a device is present, but you are not when you try and read a none existent device. That is done by the library and we have no idea how it copes with that.
Here is the new code that I tried. I gave most of the 'if's and the else their own set of brackets. I also added the test Serial.print("test"); at the end of the else. The wire.end and serial.begin had no effect either.
I wonder if this is a problem with the 'for' loop not breaking or something. But Im unsure how to accomplish what im after without it.
Thanks again kind souls.
// Russell Jones
// Electrical and COmputer Engineering, University of Missouri-Kansas City
// Senior Design 403
#include <Wire.h>
#include "Adafruit_MCP9808.h"
#include "Adafruit_MPL115A2.h"
#include "Adafruit_Si7021.h"
#include "Adafruit_VCNL4010.h"
#include "ICM_20948.h"
// Create the sensor objects
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808(); // Address defauts to 0x18
Adafruit_MPL115A2 mpl115a2 = Adafruit_MPL115A2(); // Object Created for Barometric Pressure
Adafruit_Si7021 si7021 = Adafruit_Si7021(); // Object Created for Humidity Sensor
Adafruit_VCNL4010 vcnl; // Object Created for Proximity Sensor
ICM_20948_I2C myICM; // Create an ICM_20948_I2C object - 9DOF
#define WIRE_PORT Wire // Your desired Wire port.
// FOR ANY COMMANDS DEALING WITH SENSOR 2, tempsensor2 must replace tempsensor for operations.
// FOR EXAMPLE - tempsensor.readTempC() READS CELSIUS TEMPERATURE OF SENSOR 1
// FOR EXAMPLE - tempsensor2.readTempF() READS FARHENHEIGHT TEMPERATURE OF SENSOR 2
// Si7021 - Temperature and Humidity sensor: Address - 0x40
// MPL115A2 - Barometric Pressure/Temperature Sensor: Address - 0x60
// VCNL4010 - Proximity Sensor: Address - 0x13
void setup()
{
Wire.begin();
Serial.begin(9600);
while (!Serial);
//Waits for Serial Terminal to be Open Prior to Running Script
Serial.println("Senior Design / ESP32 Module Code");
delay(2000);
Serial.print("\n");
}
void loop()
{
byte error, address;
int devices;
devices = 0;
for(address = 1; address < 127; address++ )
{
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
if (address < 16)
// if (address == 0x10) Serial.print("UV sensor found, but more code needed to run in current project state.");
delay(50);
if (address == 0x13){ Serial.println("Found VCNL4010 Proximity Sensor / Address (0x13)");
delay(50);
}
if (address == 0x18){ Serial.println("Found MCP9808 / Address (0x18)");
delay(50);
}
// if (address == 0x19) Serial.println("Found LSM303 Accelerometer / Address (0x19). More Code/Library is Required to run this Sensor.");
delay(50);
// if (address == 0x20) Serial.println("Found Chirp! Water Sensor / Address (0x20). More Code/Library is Required to run this Sensor.");
delay(50);
// if (address == 0x28) Serial.println("Found PCT2075 Temperature Sensor / Address (0x28). More Code/Library is Required to run this Sensor.");
delay(50);
// if (address == 0x29) Serial.println("Found TSL2591 Light Sensor / Address (0x29). More Code/Library is Required to run this Sensor.");
delay(50);
// if (address == 0x1E) Serial.println("Found 9DOF / Address (0x1E). More Code/Library is Required to run this Sensor.");
delay(50);
// if (address == 0x31) Serial.println("Found CAP1188 8 Channel Capacitive Touch Sensor / Address (0x31). More Code/Library is Required to run this Sensor.");
delay(50);
// if (address == 0x33) Serial.println("Found MLX90640 IR Thermal Camera / Address (0x33). More Code/Library is Required to run this Sensor.");
delay(50);
// if (address == 0x38) Serial.println("Found VEML6070 UV Index / Address (0x38). More Code/Library is Required to run this Sensor.");
delay(50);
// if (address == 0x39) Serial.println("Found APDS-9960 IR/Color/Proximity Sensor / Address (0x39). More Code/Library is Required to run this Sensor.");
delay(50);
if (address == 0x40){ Serial.println("Found Si7021 Humidity/Temp Sensor / Address (0x40)");
delay(50);
}
// if (address == 0x60) Serial.println("Found MPL115A2 Barometric Pressure/Temperature Sensor / Address (0x60)");
delay(50);
// if (address == 0x6B) Serial.println("Found 9DOF / Address (0x6B). More Code/Library is Required to run this Sensor.");
delay(50);
devices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16){
Serial.print("0");
Serial.println(address,HEX);
}
}
}
if (devices == 0)
Serial.println("No I2C devices found\n");
else{
Serial.println("done\n");
delay(2000);
//Wire.end();
//Serial.begin(9600);
Serial.print("test");
// Start Code for Temperature Sensor MCP9808
float c = tempsensor.readTempC();
float f = tempsensor.readTempF();
Serial.print("Temp: ");
Serial.print(c, 4); Serial.print("*C\t and ");
Serial.print(f, 4); Serial.println("*F.");
delay(3000);
Serial.print("\n");
// Start Code for Barometric Sensor MPL115A2
/*
Serial.println("Reading Device MPL115A2 ");
Serial.print("\n");
delay(1000);
float pressureKPA = 0, temperatureC = 0;
pressureKPA = mpl115a2.getPressure();
Serial.print("Pressure (kPa): "); Serial.print(pressureKPA, 4); Serial.println(" kPa");
Serial.print("\n");
delay(3000);
*/
// Start Code for Humidity (Si7021) Sensor
Serial.println("Reading Device Si7021 ");
Serial.print("\n");
delay(1000);
Serial.print("Humidity: ");
Serial.print("\n");
Serial.print(si7021.readHumidity(), 2);
Serial.print("\n");
delay(3000);
//Start Code for Proximity (VCNL4010) Sensor
Serial.println("Reading Device VCNL4010 ");
Serial.print("\n");
delay(1000);
Serial.print("Ambient: "); Serial.println(vcnl.readAmbient());
Serial.print("Proximity: "); Serial.println(vcnl.readProximity());
delay(2000);
Serial.print("\n");
delay(3000);
}
}
Look you are supposed to be a student, you are supposed to be able to learn from instructions.
Why are you ignoring us? You are still trying to access none existent devices. You have done nothing to change this one and only aspect of the code that we are telling you could be wrong.
Piddling about with the if statements in the discovery phase is not going to do anything for you. That bit works already, you have seen that bit works, so why do you think that bit is the problem?
You need to write a function to access each device, and then only call that function when you find that device on the bus. So that is from inside the if statement that detects you have the device available.
PS. Never try and attach or remove devices to the bus when the code is running, or when the Arduino is powered up. That is a whole other level of wrong.