110 lines
2 KiB
C++
110 lines
2 KiB
C++
#include <bitset>
|
|
#include <string>
|
|
|
|
// WAS D5 D6 D7
|
|
#define MULTIPLEX_PINA 17
|
|
#define MULTIPLEX_PINB 16
|
|
#define MULTIPLEX_PINC 4
|
|
|
|
// WAS A0
|
|
#define ANALOG_PIN 34
|
|
|
|
/*
|
|
000 0
|
|
001 1
|
|
010 2
|
|
011 3
|
|
100 4
|
|
101 5
|
|
110 6
|
|
111 7
|
|
*/
|
|
|
|
#define SOIL_A 4
|
|
#define SOIL_B 6
|
|
#define WATER_LEVEL_A 0
|
|
#define WATER_LEVEL_B 1
|
|
|
|
|
|
// Ref https://stackoverflow.com/a/523737/1460422
|
|
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
|
|
|
|
float referenceVoltage = 3.3;
|
|
|
|
|
|
|
|
void read_analog(int channel) {
|
|
|
|
const char* bin = std::bitset<8>(channel).to_string().c_str();
|
|
|
|
bool b0 = bin[7] == u'1';
|
|
bool b1 = bin[6] == u'1';
|
|
bool b2 = bin[5] == u'1';
|
|
|
|
Serial.print(" binary ");
|
|
Serial.print(bin);
|
|
Serial.print(" b2 ");
|
|
Serial.print(b2);
|
|
Serial.print(" b1 ");
|
|
Serial.print(b1);
|
|
Serial.print(" b0 ");
|
|
Serial.print(b0);
|
|
Serial.print(" ");
|
|
|
|
digitalWrite(MULTIPLEX_PINA, b2);
|
|
digitalWrite(MULTIPLEX_PINB, b1);
|
|
digitalWrite(MULTIPLEX_PINC, b0);
|
|
|
|
int rawValue = analogRead(ANALOG_PIN);
|
|
float parsedValue = ((float)rawValue) / 1024.0 * referenceVoltage;
|
|
|
|
Serial.print(" channel ");
|
|
Serial.print(channel);
|
|
Serial.print(" raw value ");
|
|
Serial.print(rawValue);
|
|
Serial.print(" parsed value: ");
|
|
Serial.print(parsedValue);
|
|
Serial.println("V");
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
Serial.println("Analogue Soil Sensor Test");
|
|
Serial.println("=========================");
|
|
pinMode(MULTIPLEX_PINA, OUTPUT);
|
|
pinMode(MULTIPLEX_PINB, OUTPUT);
|
|
pinMode(MULTIPLEX_PINC, OUTPUT);
|
|
}
|
|
|
|
void loop() {
|
|
Serial.print("C0");
|
|
read_analog(0);
|
|
Serial.print("C1");
|
|
read_analog(1);
|
|
Serial.print("C2");
|
|
read_analog(2);
|
|
Serial.print("C3");
|
|
read_analog(3);
|
|
Serial.print("C4");
|
|
read_analog(4);
|
|
Serial.print("C5");
|
|
read_analog(5);
|
|
Serial.print("C6");
|
|
read_analog(6);
|
|
Serial.print("C7");
|
|
read_analog(7);
|
|
|
|
|
|
// Serial.print("SOIL A: ");
|
|
// read_analog(SOIL_A);
|
|
// Serial.print("SOIL B: ");
|
|
// read_analog(SOIL_B);
|
|
// Serial.print("WATER LEVEL A: ");
|
|
// read_analog(WATER_LEVEL_A);
|
|
// Serial.print("WATER LEVEL B: ");
|
|
// read_analog(WATER_LEVEL_B);
|
|
|
|
Serial.println("\n\n\n\n\n");
|
|
|
|
delay(5000);
|
|
}
|