Hello,
I have a problem with my project and I require some help please. Im using a wemos D1 mini and I want to measure multiple analog signals. For that I’m using 74HC4067 16 channel mux.
To activate a specific channel I have to set some digitial Pins to high or low.
I would like to set digital Pins in “void sensorData()”, save the value and send it to a virtual pin to blynk. Then activate another channel and save it to anoter virtual pin. But it does not work. I only get values on my Analog pin, if I set the values for the digital pins in void loop(). If I try to set the digital pins inside “void SensorData()” it’s not working
Here is a part of my code which does not work?
/**************************************************************
Measuring Moisture for Plants
Sending E-Mail
}
**************************************************************/
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//#include <SimpleTimer.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = ""; //Enter the Auth code which was send by Blink
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = ""; //Enter your WIFI Name aterm-9fdb33-g
char pass[] = ""; //Enter your WIFI Password 5b62bf4ca241
//Mux control pins
int MUXPinS0 = D0;
int MUXPinS1 = D1;
int MUXPinS2 = D2;
int MUXPinS3 = D3;
BlynkTimer timer;
//unsigned long EmailTimer=0;
void sendSensor()
{
digitalWrite(MUXPinS0,LOW)
digitalWrite(MUXPinS1,LOW)
digitalWrite(MUXPins2,LOW)
digitalWrite(MUXPinS3,LOW)
int sensorData = analogRead(A0); //reading the sensor on A0
sensorData = map (sensorData,0, 1023, 100, 0);
Blynk.virtualWrite(V8, sensorData); //sending to Blynk
Serial.println("Sensor 1: ");
Serial.println(sensorData);
digitalWrite(MUXPinS0,HIGH)
digitalWrite(MUXPinS1,HIGH)
digitalWrite(MUXPins2,LOW)
digitalWrite(MUXPinS3,LOW)
int sensorData1 = analogRead(A0); //reading the sensor on A0
sensorData = map (sensorData1,0, 1023, 100, 0);
Blynk.virtualWrite(V9, sensorData1); //sending to Blynk
Serial.println("Sensor 2: ");
Serial.println(sensorData)
}
void setup()
{
pinMode(MUXPinS0, OUTPUT);
pinMode(MUXPinS1, OUTPUT);
pinMode(MUXPinS2, OUTPUT);
pinMode(MUXPinS3, OUTPUT);
Serial.begin(9600); // See the connection status in Serial Monitor
Blynk.begin(auth, ssid, pass);
timer.setInterval(6000L, sendSensor); // Setup a function to be called every 6 second
}
void loop()
{
Blynk.run(); // Initiates Blynk
timer.run(); // Initiates SimpleTimer
}
You are right. There is small error. But that is not important. I get some reading but without any sense. The values are far from what they should be. If I copy this part:
Hey Pete,
it was after fixing the code.
I worked on the code and I thing I solved the problem. I’m not sure what the problem was, but it’s solved. I’m setting the digital pins and reading the analog pin in a subfunction now. Down below is the code.
Now I have another question. Im saving the analog signal in an array[i]. This data I would like to save to virtual pin V1 to V[i]. Right now I’m doing it with a “if” statement. Is it possible to do it with a loop?
Thank you very much.
/**************************************************************
Measuring Moisture for Plants
Sending E-Mail
}
**************************************************************/
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//#include <SimpleTimer.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = ""; //Enter the Auth code which was send by Blink
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = ""; //Enter your WIFI Name aterm-9fdb33-g
char pass[] = ""; //Enter your WIFI Password 5b62bf4ca241
//Mux control pins
int MUXPinS0 = D4;
int MUXPinS1 = D1;
int MUXPinS2 = D2;
int MUXPinS3 = D3;
int myArray[16];
int pin;
BlynkTimer timer;
//unsigned long EmailTimer=0;
void sendSensor()
{
for (int i = 0; i < 2; i++)
{
myArray[i]= getAnalog(i);
myArray[i] = map (myArray[i],0, 1023, 100, 0);
if (i==0)
{pin = V0;
}
if (i==1)
{pin =V1;
}
Blynk.virtualWrite(pin, myArray[i]); //sending to Blynk
Serial.print("Sensor 1: ");
Serial.print(myArray[0]);
Serial.print(", ");
Serial.print("Sensor 2: ");
Serial.println(myArray[1]);
}
}
void setup()
{
pinMode(MUXPinS0, OUTPUT);
pinMode(MUXPinS1, OUTPUT);
pinMode(MUXPinS2, OUTPUT);
pinMode(MUXPinS3, OUTPUT);
Serial.begin(9600); // See the connection status in Serial Monitor
Blynk.begin(auth, ssid, pass);
timer.setInterval(6000L, sendSensor); // Setup a function to be called every 6 second
}
void loop()
{
Blynk.run(); // Initiates Blynk
timer.run(); // Initiates SimpleTimer
}
float getAnalog(int MUXyPin) {
//MUXyPin must be 0 to 15 representing the analog pin you want to read
//MUXPinS3 to MUXPinS0 are the Arduino pins connected to the selector pins of this board.
digitalWrite(MUXPinS3, HIGH && (MUXyPin & B00001000));
digitalWrite(MUXPinS2, HIGH && (MUXyPin & B00000100));
digitalWrite(MUXPinS1, HIGH && (MUXyPin & B00000010));
digitalWrite(MUXPinS0, HIGH && (MUXyPin & B00000001));
return (float)analogRead(A0);
}