I Am trying to make the pump turn on through the values of the light sensor. As well as changing the sound through the values of the humidity sensor. However, the pump stays off and the speaker only generates one sound. When I tested this yesterday, everything was working accordingly. The circuit I am using is exactly the same as yesterday, and other than some small changes the code is as well. Could someone help me?
The code I'm using is:
#include <MozziGuts.h>
/*
Analog Input
Demonstrates analog input by reading an analog sensor on analog pin 0 and
turning on and off a light emitting diode(LED) connected to digital pin 13.
The amount of time the LED will be on and off depends on the value obtained
by analogRead().
The circuit:
- potentiometer
center pin of the potentiometer to the analog input 0
one side pin (either one) to ground
the other side pin to +5V
- LED
anode (long leg) attached to digital output 13 through 220 ohm resistor
cathode (short leg) attached to ground
- Note: because most Arduinos have a built-in LED attached to pin 13 on the
board, the LED is optional.
created by David Cuartielles
modified 30 Aug 2011
By Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogInput
*/
#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <tables/sin2048_int8.h> // sine table for oscillator
#include <RollingAverage.h>
#include <ControlDelay.h>
#define INPUT_PIN 1 // analog control input
#define CONTROL_RATE 64
unsigned int echo_cells_1 = 32;
unsigned int echo_cells_2 = 60;
unsigned int echo_cells_3 = 127;
ControlDelay<128, int> kDelay; // 2seconds
// oscils to compare bumpy to averaged control input
Oscil<SIN2048_NUM_CELLS, AUDIO_RATE> aSin0(SIN2048_DATA);
// Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin1(SIN2048_DATA);
// Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin2(SIN2048_DATA);
// Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin3(SIN2048_DATA);
// use: RollingAverage <number_type, how_many_to_average> myThing
RollingAverage<int, 32> kAverage; // how_many_to_average has to be power of 2
int averaged;
//Fysieke pinnen op de Arduino
//INPUT
int lightPin = A0; // select the input pin for the potentiometer
//OUTPUT
int pompPin = 13;
int lightValue = 0; // variable to store the value coming from the sensor
int maxWater = 1024;
void setup() {
Serial.begin(115200);
//output pinnen definieren
pinMode(pompPin, OUTPUT);
kDelay.set(echo_cells_1);
startMozzi();
}
void updateControl() {
int bumpy_input = maxWater - mozziAnalogRead(INPUT_PIN);
averaged = kAverage.next(bumpy_input);
Serial.println(averaged);
if (averaged > 400 && averaged < 400) {
aSin0.setFreq(400);
} else {
aSin0.setFreq(800);
}
// aSin1.setFreq(kDelay.next(averaged));
// aSin2.setFreq(kDelay.read(echo_cells_2));
// aSin3.setFreq(kDelay.read(echo_cells_3));
}
AudioOutput_t updateAudio() {
return MonoOutput::fromAlmostNBit(12,
3 * ((int)aSin0.next()
// +aSin1.next()+(aSin2.next()>>1)
// +(aSin3.next()>>2))
// );
));
}
void loop() {
// read the value from the sensor:
lightValue = analogRead(lightPin);
// reactOnLight(lightValue); // waterpomp
audioHook();
}
void reactOnLight(int value) {
//Do something with light
if (value <= 500) {
Serial.println("Enough light: ");
digitalWrite(pompPin, LOW);
} else {
digitalWrite(pompPin, HIGH);
}
}
Image of the circuit ( the pump is connected to the relay module + a 12v adaptor) :
Did you 'calibrate' the capacitive moisture sensor?
I calibrated my capacitive moisture sensor by putting the sensor in water to get a 100% saturation reading. Then I removed the capacitive moistures sensor from the water, wiped it clean and dry with an absorbent cloth to get the 0% saturation reading.
void fReadAD( void * parameter )
{
float ADbits = 4096.0f;
float uPvolts = 3.3f;
float adcValue_b = 0.0f; //plant in yellow pot
uint64_t TimePastKalman = esp_timer_get_time(); // used by the Kalman filter UpdateProcessNoise, time since last kalman calculation
float WetValue = 1.07f; // value found by putting sensor in water
float DryValue = 2.732f; // value of probe when held in air
float Range = DryValue - WetValue;
float RemainingMoisture = 100.0f;
SimpleKalmanFilter KF_ADC_b( 1.0f, 1.0f, .01f );
for (;;)
{
xEventGroupWaitBits (eg, evtADCreading, pdTRUE, pdTRUE, portMAX_DELAY ); //
adcValue_b = float( adc1_get_raw(ADC1_CHANNEL_3) ); //take a raw ADC reading
adcValue_b = ( adcValue_b * uPvolts ) / ADbits; //calculate voltage
KF_ADC_b.setProcessNoise( (esp_timer_get_time() - TimePastKalman) / 1000000.0f ); //get time, in microsecods, since last readings
adcValue_b = KF_ADC_b.updateEstimate( adcValue_b ); // apply simple Kalman filter
TimePastKalman = esp_timer_get_time(); // time of update complete
RemainingMoisture = 100.0f * (1 - ((adcValue_b - WetValue) / (DryValue - WetValue))); //remaining moisture = 1-(xTarget - xMin) / (xMax - xMin) as a percentage of the sensor wet dry volatges
xQueueOverwrite( xQ_RM, (void *) &RemainingMoisture );
//log_i( "adcValue_b = %f remaining moisture %f%", adcValue_b, RemainingMoisture );
}
vTaskDelete( NULL );
}
////
Knowing the capacitive sensor max and min readings then the % of remaining moisture can be calculated and to give you a range for operations.
This is the task that uses the A:D reading to run the pump.
void fDoMoistureDetector( void * parameter )
{
//wait for a mqtt connection
while ( !MQTTclient.connected() )
{
vTaskDelay( 250 );
}
int TimeToPublish = 5000000; //5000000uS
int TimeForADreading = 100 * 1000; // 100mS
uint64_t TimePastPublish = esp_timer_get_time(); // used by publish
uint64_t TimeADreading = esp_timer_get_time();
TickType_t xLastWakeTime = xTaskGetTickCount();
const TickType_t xFrequency = 10; //delay for 10mS
float RemainingMoisture = 100.0f; //prevents pump turn on during start up
bool pumpOn = false;
uint64_t PumpOnTime = esp_timer_get_time();
int PumpRunTime = 11000000;
uint64_t PumpOffWait = esp_timer_get_time();
uint64_t PumpOffWaitFor = 60000000; //one minute
float lowMoisture = 23.0f;
float highMoisture = 40.0f;
for (;;)
{
//read AD values every 100mS.
if ( (esp_timer_get_time() - TimeADreading) >= TimeForADreading )
{
xEventGroupSetBits( eg, evtADCreading );
TimeADreading = esp_timer_get_time();
}
xQueueReceive(xQ_RM, &RemainingMoisture, 0 ); //receive queue stuff no waiting
//read gpio 0 is water level good. Yes: OK to run pump : no pump off. remaining moisture good, denergize water pump otherwise energize water pump.
if ( RemainingMoisture >= highMoisture )
{
WaterPump0_off();
}
if ( !pumpOn )
{
log_i( "not pump on ");
if ( gpio_get_level( GPIO_NUM_0 ) )
{
if ( RemainingMoisture <= lowMoisture )
{
//has one minute passed since last pump energize, if so then allow motor to run
if ( (esp_timer_get_time() - PumpOffWait) >= PumpOffWaitFor )
{
WaterPump0_on();
log_i( "pump on " );
pumpOn = !pumpOn;
PumpOnTime = esp_timer_get_time();
}
}
//xSemaphoreGive( sema_RemainingMoisture );
} else {
log_i( "water level bad " );
WaterPump0_off();
PumpOffWait = esp_timer_get_time();
}
} else {
/*
pump goes on runs for X seconds then turn off, then wait PumpOffWaitTime before being allowed to energize again
*/
if ( (esp_timer_get_time() - PumpOnTime) >= PumpRunTime )
{
log_i( "pump off " );
WaterPump0_off(); // after PumpRunTime seconds turn pump off
pumpOn = !pumpOn;
PumpOffWait = esp_timer_get_time();
}
}
// publish to MQTT every 5000000uS
if ( (esp_timer_get_time() - TimePastPublish) >= TimeToPublish )
{
xQueueOverwrite( xQ_RemainingMoistureMQTT, (void *) &RemainingMoisture );// data for mqtt publish
TimePastPublish = esp_timer_get_time(); // get next publish time
}
xLastWakeTime = xTaskGetTickCount();
vTaskDelayUntil( &xLastWakeTime, xFrequency );
}
vTaskDelete( NULL );
}// end fDoMoistureDetector()
How was that obvious? There is no sign of any resistor for the speaker in your picture. The only resistor visible there is for the LDR. When I pointed this out you attempted to justify having no resistor because some idiot online didn't have one.
Why not be honest with the forum? We are only trying to help you.
I had looked it up online and checked several circuits on several sites, on which most didn't use a resistor. After you suggested adding a resistor, I did through the breadboard. I thought it was obvious that I meant it like that. I didn't mean to offend you.