Beste specialisten,
Ik lees een voltage van 0-5V uit via de analoge pin op de arduino en print dit op een LCD-scherm.
Nu wil ik een relais sturen onder volgende voorwaarden:
- Als het voltage <1 dan schakelt de relais uit.
- Als het voltage >=1 dan schakelt de relais in.
- Als het voltage opnieuw onder 1 zakt dan moet de relais nog 5sec aan blijven alvorens terug uit te schakelen.
Het voltage moet ten allen tijde op het scherm blijven verschijnen.
Ik heb al vanalles geprobeerd (zoals while en do while) maar deze functies blokkeren na 1 uitlezing van de analoge pin.
Kan iemand mijn code eens bekijken en zeggen wat ik fout doe aub? (de vertraging van 5 seconden zit nog niet in de code verwerkt)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);
int sensorValue = 0; // variable to store the value coming from the sensor
int sensorPin = A0; // select the input pin
float voltage = 0;
// the setup routine runs once when you press reset:
void setup() {
// configuratie pins (groene led van de relais is uit bij HIGH en aan bij LOW)
pinMode(13, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Pompsturing");
lcd.setCursor(0,1);
lcd.print("Zonnepanelen");
lcd.setCursor(0,2);
lcd.print("By Gorres");
delay(2000);
lcd.clear();
}
// the loop routine runs over and over again forever:
void loop() {
int sensorValue = analogRead(sensorPin); //readpin and convert to real voltage value
float voltage = sensorValue * (5.0 / 1023.0);
voltage = 0.92*voltage;
while(voltage>=1) { // as long as voltage>=1 switch relais LOW and print the voltage
digitalWrite(13, LOW);
lcd.setCursor(0,2);
lcd.print("Pomp: AAN");
lcd.setCursor(0,0);
lcd.print("V(0-5) :");
lcd.print(voltage);
lcd.print("V");
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("V(0-10) :");
lcd.print(voltage*20/10.0);
lcd.print("V");
lcd.print(" ");
delay(500);
int sensorValue = analogRead(sensorPin);
float voltage = sensorValue * (5.0 / 1023.0);
voltage = 0.92*voltage;
}
// as long as voltage<1 switch relais HIGH and print the voltage
while(voltage < 1) {
digitalWrite(13, HIGH);
lcd.setCursor(0,2);
lcd.print("Pomp: UIT");
lcd.setCursor(0,0);
lcd.print("V(0-5) :");
lcd.print(voltage);
lcd.print("V");
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("V(0-10) :");
lcd.print(voltage*20/10.0);
lcd.print("V");
lcd.print(" ");
delay(500);
int sensorValue = analogRead(sensorPin);
float voltage = sensorValue * (5.0 / 1023.0);
//voltage = 0.9653*voltage + 0.0155;
voltage = 0.92*voltage;
}
}