Hello!
I have to build a voltmeter with an Arduino and a display with a classmate. Since the measuring range has to go up to 50 volts, we use 3 analog inputs for 50V, 20V and 5V.
The 50V and 20V input have upstream voltage dividers.
We switch the analogue inputs using 3 Mosfets so that we only have one voltage input. Now my question, somehow our code doesn't work and we don't know why, 3.3V and 5V we take from another Arduino shows 3.9V-4.2V and it doesn't seem to switch properly between the mosfets.
Does anyone spot the error in the code?
Thank you in advance and sorry if there are any grammatical errors.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
uint16_t E5 = A0;
uint16_t E20 = A1;
uint16_t E50 = A2;
int Fet5 = 8;
int Fet20 = 9;
int Fet50 = 10;
int messbereich = 0;
void setup() {
lcd.begin(16, 4);
pinMode (Fet5, OUTPUT);
pinMode (Fet20, OUTPUT);
pinMode (Fet50, OUTPUT);
Serial.begin(9600);
}
void Spannungsmessung()
{
float spannung = 0;
digitalWrite(Fet50, HIGH);
digitalWrite(Fet20, LOW);
digitalWrite(Fet5, LOW);
spannung = analogRead(A2);
spannung = ((spannung / 1023) * 50) ;
if (spannung < 20 && spannung > 5) //19.9
{
digitalWrite(Fet50, LOW);
digitalWrite(Fet20, HIGH);
spannung = analogRead(A1);
spannung = ((spannung / 1023) * 20);
messbereich = 20;
}
if (spannung < 5) //4.99
{
digitalWrite(Fet20, LOW);
digitalWrite(Fet5, HIGH);
spannung = analogRead(A0);
spannung = ((spannung / 1023) * 5);
messbereich = 5;
}
lcd.setCursor(0, 0);
lcd.print(spannung, 1);
}
void Display() {
lcd.setCursor(4, 0);
lcd.print("Volt");
lcd.setCursor(0, 1 );
lcd.print("Messbereich");
lcd.setCursor(13, 1);
lcd.print(messbereich);
lcd.setCursor(15, 1);
lcd.print("V");
messbereich=50;
}
void loop() {
Spannungsmessung();
Display();
}