Problem with Arduino Nano Coding

Thread Starter

28sya

Joined Jun 13, 2020
20
i'm doing voltage indicator but the input voltage is different from output voltage show on lcd display. can someone help me how to solve the problem?


Code:
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, A0, A1, A2, A3);

int analogPin = A4;
float vout = 0.0;
float vin = 0.0;
float R1 = 100000.0; // resistance of R1 (100K)
float R2 = 10000.0; // resistance of R2 (10K)
int value = 0;
int a[5] = {};

int greenLed = 10;
int blueLed = 11;
int yellowLed = 12;
int redLed = 13;
int sound = 8;

void setup()
{
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(yellowLed,OUTPUT);
  pinMode(redLed,OUTPUT);
 
  pinMode(A0,OUTPUT);
  pinMode(A1,OUTPUT);
  pinMode(A2,OUTPUT);
  pinMode(A3,OUTPUT);
 
  pinMode(A4,INPUT);
 
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.setCursor(1, 0);
  lcd.print("VOLT INDICATOR");
  lcd.setCursor(6, 1);
  lcd.print("BLIS");
  delay(3000);
  lcd.clear();
}

void loop()
{
 // LCD DISPLAY
 
 lcd.print("Voltage Level");
 // read the value at analog input
 value = analogRead(A4);
 vout = (value * 5) / 1024.0;
 vin = vout / (R2 / (R1 + R2));
 Serial.println (vin);
 
 if (vin < 0.09)
 {
  vin = 0.0;
 }
 
 lcd.setCursor(0, 1);
 lcd.print("Voltage V :");
 lcd.print(vin);
 delay(3000);
 //lcd.clear();
 
// LED & BUZZER
 
if(vin <= 11.3)
{
 digitalWrite(redLed, HIGH);
 //delay (1000);
 tone(sound, 1000, 250);             
 delay(1000);                             
 digitalWrite(redLed, LOW);
 //delay (1000);
 tone(sound, 2000, 250);             
 delay(1000);                               
}
 
  if(vin <= 11.60 && vin > 11.3)
{
 digitalWrite(greenLed,LOW);
 digitalWrite(blueLed,LOW);
 digitalWrite(yellowLed,HIGH);
 digitalWrite(redLed,HIGH);
}
 
  if(vin <= 12.1 && vin > 11.6)
{
  digitalWrite(greenLed,LOW);
  digitalWrite(blueLed,HIGH);
  digitalWrite(yellowLed,HIGH);
  digitalWrite(redLed,HIGH);
}

if(vin > 12.1)
{
  digitalWrite(greenLed,HIGH);
  digitalWrite(blueLed,HIGH);
  digitalWrite(yellowLed,HIGH);
  digitalWrite(redLed,HIGH);
}
}
 

ci139

Joined Jul 11, 2016
1,898
Code:
. . .
value = analogRead(A4);
// donno if theres any help but you can try
vout = 2.5/512;
vout = vout*value;
// vout = (value * 5) / 1024.0;
// vin = vout / (R2 / (R1 + R2));
vin = vout * (R1/R2 + 1);
Serial.println (vin); // ? sending floating point value over serial . . . good question is -- what is been sent ?
. . .
lcd.setCursor(0, 1);
lcd.print("Voltage V :");
lcd.print(vin); // ? does the lcd.print() detect the float and convert it to anything reasonable display value . . . ?
. . .
 
Top