Hi, I am working on a project which takes the input from an Oxygen sensor and subtracts it to find the difference from the number I have supplied. The issue is that it works for until it triggers the else and then starts printing a v= and that number loops indefinitely. I don't know what is wrong with my code.
I attached my serial monitor below
#include "VernierLib.h" //include Vernier functions in this sketch
VernierLib Vernier; //create an instance of the VernierLib library
float sensorReading; //create global variable to store sensor reading
float gateOpen = 0.36;
void setup() {
Serial.begin(9600); //setup communication to display
Vernier.autoID(); //identify the sensor being used
pinMode(11,OUTPUT); // sets digital pin 11 as output
pinMode(12,OUTPUT); // sets digital pin 12 as output
}
void loop() {
sensorReading = Vernier.readSensor(); //read one data value
Serial.print(sensorReading,2); //print data value
Serial.print(" "); //print a space
Serial.println(Vernier.sensorUnits()); //print units and skip to next line
delay(1000); //wait 10 seconds
There are Serial.print statements in that library( assuming the library at link is one you are using)
29 in total some of them are in a "debug block " or behind comments.
One of them prints "v="
pinMode(11,OUTPUT); // sets digital pin 11 as output
pinMode(12,OUTPUT); // sets digital pin 12 as output
.
.
.
But the library looks to use these pins too, one as an input:
VernierLib::VernierLib()
{
pinMode(2, INPUT); //Echo pin; this is the pin that goes high when an echo is received
pinMode(3, OUTPUT);//Trigger Pin used for Motion Detector
pinMode(6, OUTPUT);// set up DCU lines, assuming it is on Digital 2
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT); //multiplexer on the shield, lsb
pinMode(11, OUTPUT); //multiplexer on the shield, msb
pinMode (12, INPUT_PULLUP); //button on DCU
pinMode (13, OUTPUT); //LED on shield
}
You're changing 11 which is used for a "multiplexer on the shield". You've made pin 12 an output but the vernier library suggests it's used for a pushbutton:
_buttonState = digitalRead(12);// button on shield
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (_buttonState == LOW) // button down
{. . .
Blackfin:
You are using pins 11 and 12 for something:
pinMode(11,OUTPUT); // sets digital pin 11 as output
pinMode(12,OUTPUT); // sets digital pin 12 as output
.
.
.
But the library looks to use these pins too, one as an input:
VernierLib::VernierLib()
{
pinMode(2, INPUT); //Echo pin; this is the pin that goes high when an echo is received
pinMode(3, OUTPUT);//Trigger Pin used for Motion Detector
pinMode(6, OUTPUT);// set up DCU lines, assuming it is on Digital 2
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT); //multiplexer on the shield, lsb
pinMode(11, OUTPUT); //multiplexer on the shield, msb
pinMode (12, INPUT_PULLUP); //button on DCU
pinMode (13, OUTPUT); //LED on shield
}
You're changing 11 which is used for a "multiplexer on the shield". You've made pin 12 an output but the vernier library suggests it's used for a pushbutton:
_buttonState = digitalRead(12);// button on shield
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (_buttonState == LOW) // button down
{. . .
So what are you doing with pins 11 and 12?
I am using pin 11 to trigger a relay to open a ball valve to allow oxygen to flow in and pin 12 triggers the relay to close the ball valve
That library is very poorly written.
-pinmode() in constructor.
-incorrectly setting value of char array.
-incorrectly(maybe) doing math with integers.
arduino_new:
That library is very poorly written.
-pinmode() in constructor.
-incorrectly setting value of char array.
-incorrectly(maybe) doing math with integers.
What would you recommend I do to fix it. If I just use one output it does trigger and turn the led on and off. It's once I add that second output that I'm getting the problem with the repeating numbers that aren't accurate (blowing carbon dioxide onto the sensor and the value still doesn't change)
arduino_new:
That library is very poorly written.
-pinmode() in constructor.
-incorrectly setting value of char array.
-incorrectly(maybe) doing math with integers.
What is wrong with using pinmode() in a constructor, The fact that the pin numbers are hardwired/named or the concept in general?
Whandall:
The constructor could be called before the Arduino hardware setup is initialized,
so the pinMode could be undone by the initialization.
I understand what you say
But is that only a problem if user makes a mistake like assign the same GPIO number to two different task in the setup()?
Is that correct or is there another scenario where it can cause a issue?