#include <LiquidCrystal.h>
//VARIAVEIS PARA LER A TENSAO
const int analogPin = A3; // potentiometer wiper (middle terminal) connected to analog pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read
int led1 = 4;
int led2 = 5;
LiquidCrystal lcd(12,11,10,9,8,7);
const int buttonPin = 13; // the number of the pushbutton pin
const int relays = 12; // the number of the LED pin
int run;
int relayState = LOW; // the current state of relay
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
void setup() {
run = 0; //starts stopped
Serial.begin(9600); // setup serial
pinMode(buttonPin, INPUT_PULLUP);
pinMode(relays, OUTPUT);
lcd.begin(16, 2);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
digitalWrite(relays, LOW);
currentButtonState = digitalRead(buttonPin);
}
void loop() {
// read the state of the pushbutton value:
bool buttonState = digitalRead(buttonPin);
if(digitalRead(buttonPin) == LOW){ //funcitons based off of button pulling input pin LOW
lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(buttonPin); // read new state
if(lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println("The button is pressed");
// toggle state of relay
relayState = !relayState;
// control relay arccoding to the toggled state
digitalWrite(relays, relayState);
}
if(currentButtonState == HIGH){
exit(0);
}
//LER E ESCREVER A TENSAO
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
//FAZER A LEITURA DA CORRENTE DO CIRCUITO
unsigned int x=0;
float AcsValue=0.0,Samples=0.0,AvgAcs=0.0,AcsValueF=0.0;
for (int x = 0; x < 150; x++){ //Get 150 samples
AcsValue = analogRead(A0); //Read current sensor values
Samples = Samples + AcsValue; //Add samples together
delay (3); // let ADC settle before next sample 3ms
}
AvgAcs=Samples/150.0;//Taking Average of Samples
//((AvgAcs * (5.0 / 1024.0)) is converitng the read voltage in 0-5 volts
//2.5 is offset(I assumed that arduino is working on 5v so the viout at no current comes
//out to be 2.5 which is out offset. If your arduino is working on different voltage than
//you must change the offset according to the input voltage)
//0.185v(185mV) is rise in output voltage when 1A current flows at input
AcsValueF = (2.5 - (AvgAcs * (5.0 / 1024.0)) )/0.185;
Serial.print(AcsValueF);//Print the read current on Serial monitor
Serial.print("ma");
delay(50);
if (AcsValueF>=0 && AcsValueF<=200) {
lcd. print("Passou no teste\n");
digitalWrite(led1, HIGH);
delay(3000);
digitalWrite(led1, LOW);
delay(1000);
lcd.clear();
}
else{
lcd. print("Nao passou no teste\n");
digitalWrite(led2, HIGH);
delay(3000);
digitalWrite(led2, LOW);
delay(1000);
lcd.clear();
} //code you only run if button was pressed, stops running when button pressed again, so forth...
}
}