Hi
Yes I'm looking at that subject. Since I already have a code for all the other options, it's hard to see for me on where I can apply the example to my existing code.
Since I only received my first Arduino 3 days ago and I never program any code before, I have to fight with a lot of unknown here.
This is my code to read One Air pressure sensor (PSI)
to read an Oxygen sensor (O2)
To send the result to a LCD 16,2
One button to calibrate the O2 sensor
3 buttons to control 3 LED
All this is working but now I need to make the LED #3 to flash when I press the button #3
six minute off, one second on, six minutes on one second off until I press the button again
This code is coming from 3 different person and example on the web. I'm sure that one day
I will clean it but for now I'm just happy that it's working not so bad
//#define _DEBUG
#include <Wire.h>
#include <Adafruit_ADS1015.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <RunningAverage.h>
#define RA_SIZE 20
RunningAverage RA(RA_SIZE);
Adafruit_ADS1115 ads_O2 (0x49);
Adafruit_ADS1115 ads_PSI (0x48);
// activer les LED et boutons
int pinButton1 = 8;
int LED1 = 3;
int stateLED1 = LOW;
int stateButton1;
int pinButton2 = 9;
int LED2 = 4;
int stateLED2 = LOW;
int stateButton2;
int pinButton3 = 10;
int LED3 = 5;
int stateLED3 = LOW;
int stateButton3;
// debounce
int previous = LOW;
long time = 0;
long debounce = 300;
// section pour le PSI
int16_t rawADCvalue; // The is where we store the value we receive from the ADS1115
float scalefactor = 0.1250F; // This is the scale factor for the default +/- 4096 Volt Range we will use
float PSI = 0.0; // The result of applying the scale factor to the raw value
LiquidCrystal_I2C lcd(0x27,16,2); // your i2c address might differnt. if not working try scanning i2c first.
const int buttonPin=2; // push button
//const int ledPin = 13; // led
double calibrationv;
float multiplier;
int programState = 0;
int buttonState;
long buttonMillis = 0;
const long intervalButton = 2000; // 2 sec button hold to calibration
/*
Calculate MOD (Maximum Operating Depth)
*/
float max_po1 = 1.40;
float max_po2 = 1.60;
float cal_mod (float percentage, float ppo2 = 1.4) {
return 10 * ( (ppo2/(percentage/100)) - 1 );
}
int read_sensor(int x=0) {
int16_t millivolts = 0;
if (x == 0) {
millivolts = ads_O2.readADC_Differential_0_1();
RA.addValue(millivolts);
}
}
void setup(void)
{
//input output 3 boutons
pinMode(pinButton1, INPUT);
pinMode(LED1, OUTPUT);
pinMode(pinButton2, INPUT);
pinMode(LED2, OUTPUT);
pinMode(pinButton3, INPUT);
pinMode(LED3, OUTPUT);
#ifdef _DEBUG
Serial.begin(9600);
#endif
pinMode(buttonPin,INPUT_PULLUP);
// The ADC input range (or gain) can be changed via the following
// functions, but be careful never to exceed VDD +0.3V max, or to
// exceed the upper and lower limits if you adjust the input range!
// Setting these values incorrectly may destroy your ADC!
// ADS1015 ADS1115
// ------- -------
// ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default)
// ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
// ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV
// ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV
// ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV
// ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV
ads_O2.begin(); // ads1115 Pour O2 initialize
ads_PSI.begin(); // ads1115 pour PSI initialize
ads_O2.setGain(GAIN_TWO);
multiplier = 0.0625F;
ads_PSI.setGain(GAIN_ONE);
// multiplier = 0.1250F;
lcd.init();
lcd.backlight();
lcd.clear();
RA.clear();
for(int cx=0; cx<= RA_SIZE; cx++) {
read_sensor(0);
}
calibrationv = EEPROMReadInt(0);
if (calibrationv < 100) calibrationv=calibrate(0);
}
void EEPROMWriteInt(int p_address, int p_value)
{
byte lowByte = ((p_value >> 0) & 0xFF);
byte highByte = ((p_value >> 8) & 0xFF);
EEPROM.write(p_address, lowByte);
EEPROM.write(p_address + 1, highByte);
}
unsigned int EEPROMReadInt(int p_address)
{
byte lowByte = EEPROM.read(p_address);
byte highByte = EEPROM.read(p_address + 1);
return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
}
int calibrate(int x){
lcd.clear();
lcd.print("Calibrating");
double result;
for(int cx=0; cx<= RA_SIZE; cx++) {
read_sensor(0);
}
result = RA.getAverage();
result = abs(result);
EEPROMWriteInt(x, result); // write to eeprom
lcd.clear();
return result;
}
void analysing(int x, int cal, String txt) {
double currentmv;
double result;
double mv = 0.0;
read_sensor(x);
currentmv = RA.getAverage();
currentmv = abs(currentmv);
result = (currentmv / cal) * 20.9;
if (result > 99.9) result = 99.9;
mv = currentmv * multiplier;
lcd.setCursor(0,0);
if (mv < 0.02) {
lcd.print("Sensor error! ");
} else
{
lcd.print(txt);
lcd.print(result,1);
lcd.print("% ");
lcd.print(mv,1);
lcd.print("mv");
lcd.setCursor(0,1);
lcd.print("Pression = ");
lcd.print(PSI,0);
Serial.begin(9600);
Serial.print("Oxygen ");
Serial.print(result,1);
Serial.print("% ");
Serial.print(mv,1);
Serial.print("mv");
if (result > 30)
{
stateLED2 = LOW;
}
// lcd.setCursor(0,1);
// lcd.print("MOD ");
// lcd.print(cal_mod(result,max_po1),1);
// lcd.print("m ");
// lcd.print(cal_mod(result,max_po2),1);
// lcd.print("m ");
}
}
void loop(void)
{
// section pour le PSI
rawADCvalue = ads_PSI.readADC_SingleEnded(0);
PSI = (rawADCvalue * scalefactor) -35.0;
if (PSI > 500)
{
stateLED1 = LOW;
stateLED2 = LOW;
}
{
Serial.begin(9600);
// Serial.print("Raw data pressure = ");
// Serial.print(rawADCvalue);
Serial.print("\tPSI = ");
Serial.println(PSI,0);
Serial.println();
}
lcd.clear();
lcd.home();
unsigned long currentMillis = millis();
buttonState = digitalRead(buttonPin);
if (buttonState == LOW && programState == 0) {
buttonMillis = currentMillis;
programState = 1;
} else if (programState == 1 && buttonState == HIGH) {
programState = 0;
}
if(currentMillis - buttonMillis > intervalButton && programState == 1) {
calibrationv=calibrate(0); // calibration
programState = 1;
}
analysing(0,calibrationv,"O2 ");
delay(500);
{
//bouton 1
stateButton1 = digitalRead(pinButton1);
if(stateButton1 == HIGH && previous == LOW && millis() - time > debounce) { //if button is pressed and LED is off
if(stateLED1 == HIGH){
stateLED1 = LOW;
} else {
stateLED1 = HIGH;
}
time = millis();
}
digitalWrite(LED1, stateLED1);
previous == stateButton1;
// bouton 2
stateButton2 = digitalRead(pinButton2);
if(stateButton2 == HIGH && previous == LOW && millis() - time > debounce) { //if button is pressed and LED is off
if(stateLED2 == HIGH){
stateLED2 = LOW;
} else {
stateLED2 = HIGH;
}
time = millis();
}
digitalWrite(LED2, stateLED2);
previous == stateButton2;
//bouton 3
stateButton3 = digitalRead(pinButton3);
if(stateButton3 == HIGH && previous == LOW && millis() - time > debounce) { //if button is pressed and LED is off
if(stateLED3 == HIGH){
stateLED3 = LOW;
} else {
stateLED3 = HIGH;
}
time = millis();
}
digitalWrite(LED3, stateLED3);
previous == stateButton3;
}
}