Subversion Repositories Arduino.arduino

Rev

Blame | Last modification | View Log | Download

#include <TimedAction.h>
void ta_waterlevel();
TimedAction readLVL4cm = TimedAction(3000,ta_waterlevel);

/* Arduino Tutorial - Watel Level Sensor 40mm
   More info: */
struct lvl_sensor {
  
  byte power_pin = 7;    //Sensor is powered with pin 7
  byte signal_pin = A3;  //Sensor AO pin to Arduino pin A0

  int level = 0;         //Variable to store the incomming data
  float level_avg = 0.0; //average over 10 samples
  int level10 = 0;       //level10 = 0; level < 460; low low
                         //level10 = 10; level < 522; low
                         //level10 = 20; level < 570; medium
                         //level10 = 30; level < 585; high
                         //level10 = 40; level > 586; max
                         //level10 = 40; level > 586; max
};

lvl_sensor lvl4cm;

void setup()
{
  //Begin serial communication
  Serial.begin(9600);

  pinMode(lvl4cm.power_pin, OUTPUT);   // configure D7 pin as an OUTPUT
  digitalWrite(lvl4cm.power_pin, LOW); // turn the sensor OFF 
}

void loop()
{

  readLVL4cm.check();
}

void ta_waterlevel() {

  digitalWrite(lvl4cm.power_pin, HIGH);  // turn the sensor ON
  delay(10);                      // wait 10 milliseconds
  lvl4cm.level = analogRead(lvl4cm.signal_pin); // read the analog value from sensor
  digitalWrite(lvl4cm.power_pin, LOW);   // turn the sensor OFF

  //filter measurement, calculate mean over 10 samples
  lvl4cm.level_avg = ((9.0 * lvl4cm.level_avg) + ((float)lvl4cm.level)) / 10.0;
  int ilevel = round(lvl4cm.level_avg);
  int level10 = 50; //max level for domoticz switch
  if (ilevel < 635) level10 = 40;
  if (ilevel < 630) level10 = 30;
  if (ilevel < 620) level10 = 20;
  if (ilevel < 600) level10 = 10;
  if (ilevel < 560) level10 =  0;
  lvl4cm.level10 = level10;

  Serial.print("Resistance:"); 
  Serial.print(lvl4cm.level); 
  Serial.print(",Level10:"); 
  Serial.println(lvl4cm.level10); 
}