Hello, I´m trying to make a automatic water pump and humidity sensor.
For the humidity sensor I'm using a neopixel and a potentiometer only for testing.
I´ve tried to establish serial communication with Python, could you help me in the arduino code side?
Here is the code:
#include <Adafruit_NeoPixel.h>
#define PIN 4 // pin Neopixel is attached to
#define SENSOR A0 //input pin for Potentiometer
#define NUMPIXELS 1 // number of neopixels in strip
/Estos valores establecen lo que se puede considerar como seco o humedo/
#define dryThreshold 50 //below this value, begin alerting dry, turn red;
#define wetThreshold 200 //above this value, begin alerting wet,turn blue;
#define thresholdCenter (dryThreshold + wetThreshold)/2 //Brightest Green point
#define crossFade 20 //how much blue and red should fade in to green
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 100; // delay en ms
int redColor = 0;
int greenColor = 0;
int blueColor = 0;
//lectura de valores
int sensorValue = 0;
int transitionValue = 0;
void setup() {
pinMode(2, OUTPUT);
pixels.begin();
pinMode(SENSOR,INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(2, HIGH); //enciende el relay
delay(10000); // timeout 10 segundos
digitalWrite(2, LOW); //apaga el relay
while(1){}
sensorValue = analogRead(SENSOR);
transitionValue = map(sensorValue,0,1023,0,255);
setColor();
//valores rgb
pixels.setPixelColor(0,redColor,greenColor,blueColor);
//envia el valor
pixels.show();
//delay
delay(delayval);
}
void setColor(){
//color rojo -> resistencia/seco (mayor humedad)
redColor = ((transitionValue <= dryThreshold + crossFade) && (transitionValue >= 0 ))? map(transitionValue,0,dryThreshold + crossFade,255,0) : 0;
//color azul <- resistencia/humedo (menor humedad)
blueColor = (transitionValue >= wetThreshold - crossFade && transitionValue <= 255)? map(transitionValue,wetThreshold - crossFade,255,0,255):0;
//color verde <=> resistencia (humedad media)
if(transitionValue >= dryThreshold && transitionValue <= thresholdCenter)
{
greenColor = map(transitionValue,dryThreshold,thresholdCenter,0,255);
}
else if(transitionValue > thresholdCenter && transitionValue < wetThreshold)
{
greenColor = map(transitionValue,dryThreshold,thresholdCenter,255,0);
}
else{
greenColor = 0;
}
}