Toggle state

Hi there,

my arduino measures incoming dc on the analog inputs and if it matches a certain value, it prints a message. However, I don’t want the program to send the print message repeatedly. I need printing only one… I tried the Button example with no success. I tried every single toggle-examples but cannot apply to my code. Any help would be appreciated! Here’s the code:

int v1;
int v2;
int d=50;

void setup(){
Serial.begin(9600);
}
void loop(){
v1=analogRead(0);
//Serial.println(v1);
delay(100);
v2=analogRead(1);
// Serial.println(v2);
// delay(d);
if(v1<=389 & v1>=387){
Serial.println(“GET SOME!”);
}
if(v2<=389 & v2>=387){
Serial.println(“AWESOME”);
}
if(v1<=218 & v1>=215){
Serial.println(“BOOMIN!”);
}
if(v2<=218 & v2>=215){
Serial.println(“UBER”);
}
}

Put all your code in setup.

Or, add a boolean variable:
boolean beenThereDoneThat = false;

Then, in loop:
void loop()
{
if(!beenThereDoneThat)
{
v1=analogRead(0);
//Serial.println(v1);
delay(100);
v2=analogRead(1);
// Serial.println(v2);
// delay(d);
if(v1<=389 & v1>=387){
Serial.println(“GET SOME!”);
beenThereDoneThat = true;
}
if(v2<=389 & v2>=387){
Serial.println(“AWESOME”);
beenThereDoneThat = true;
}
if(v1<=218 & v1>=215){
Serial.println(“BOOMIN!”);
beenThereDoneThat = true;
}
if(v2<=218 & v2>=215){
Serial.println(“UBER”);
beenThereDoneThat = true;
}
}

// Do whatever needs to be done over and over…
}

Thanks for the answers, gents! Will try them!

The problem have been partly resolved, thanks! However after I disconnect the DC source from the analog input, the measured value remains the same. The idea of the circuit is that the board's analog inputs measure voltage of a dc source. When the source is disconnected, the voltage should change, I guess. Instead they remain the same... The inputs should exactly tell when a source is connected or disconnected.

alkopop79: The problem have been partly resolved, thanks! However after I disconnect the DC source from the analog input, the measured value remains the same. The idea of the circuit is that the board's analog inputs measure voltage of a dc source. When the source is disconnected, the voltage should change, I guess. Instead they remain the same... The inputs should exactly tell when a source is connected or disconnected.

When you disconnect your voltage source to the pin you will continue to read the residue voltage charge on the pin. What you can do it wire a 20k ohm resistor from the analog pin to ground and that should discharge the voltage back to zero.

Lefty

Brilliant! Thanks!

Quick update: a 1M Ohm resistor does the job well! Thanks again!