const int sensor = A0; // analog input use variable resistance
void setup ()
{
Serial.begin (9600);
}
void loop ()
{
if(analogRead(sensor)==1023)//when variable resistance give value =1023 print 'x'
{
Serial.print("x");
Serial.println();
}
delay (1000);
}
// my proplem : when i stay in this value (1023) it repeated print 'x' until i change this value, i need when i stay in (1023) print x one time and repeat it when i change the value of analug input and back to (1023)
Keep track of the last value read and only print it when the value reaches 1023 and it wasn't 1023 on the last reading; This is known as signal edge detection.
const int sensor = A0; // analog input use variable resistance
int flagged = 0; // create a flag variable
void setup ()
{
Serial.begin (9600);
}
void loop ()
{
if(analogRead(sensor)==1023)//when variable resistance give value =1023 print 'x'
{
if(!flagged) // if already printed out an x, don't repeat unless a less then 1023 reading has occured
{
Serial.print("x");
Serial.println();
flagged = 1; // set flag to note the we have printed an x on first new reading of a 1023 value
}
}
else flagged =0; // we read a value less then 1023 so clear flag so next time a 1023 will print an x
delay (1000);
}
Whats the problem? I tested his code myself and it works. Is it not printing out fast enough? You may need to change the delay time from 1000 to 500 or less.
void loop ()
{
if(analogRead(sensor)==1023)//when variable resistance give value =1023 print 'x'
{
if(!flagged) // if already printed out an x, don't repeat unless a less then 1023 reading has occured
{
if (x==1)
{
Serial.print("x");
Serial.println();
}
flagged = 1; // set flag to note the we have printed an x on first new reading of a 1023 value
}
}
else flagged =0; // we read a value less then 1023 so clear flag so next time a 1023 will print an x
retrolefty:
You just need to keep track of past reading through the main loop:
const int sensor = A0; // analog input use variable resistance
int flagged = 0;
void setup ()
{
Serial.begin (9600);
}
void loop ()
{
if(analogRead(sensor)==1023)//when variable resistance give value =1023 print 'x'
{
if(!flagged)
{ // i need to add if condition here and when it valid print "X" , my problem: when i add if condition not print "x"
Serial.print("x");
Serial.println();
flagged = 1;
}
else flagged =0;
}
if(analogRead(sensor)==1023)
{
if(!flagged)
{ if (inputsignal == 1) // i need print "x" when analogRead(sensor)==1023 and when inputsignal == 1
{
Serial.print("x");
Serial.println();
flagged = 1;
}
}
}
else flagged =0;