stay in the same value of analog input give output one time

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.

please , can you give me an example on signal edge detection or any link explain it

MostafaHamdy:
please , can you give me an example on signal edge detection or any link explain it

Look at the StateChangeDetection example.

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) 
    {
      Serial.print("x");
      Serial.println();
      flagged = 1; 
    }
   else flagged =0;
  }
 
   delay (1000);
}

@ retrolefty : this code give me the same result when i stay in this value (1023) 'x' repeated

MostafaHamdy:
@ retrolefty : this code give me the same result when i stay in this value (1023) 'x' repeated

Well that stumps me, maybe I should stick with hardware. :smiley:

Lefty

Ah, I placed the else in the wrong scope. Try:

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);
}

yes that i need :smiley: :smiley: :smiley: , please can you add line comment to help me to understand the code
thanks :smiley: :smiley:

MostafaHamdy:
yes that i need :smiley: :smiley: :smiley: , please can you add line comment to help me to understand the code
thanks :smiley: :smiley:

added comments to code/

Lefty

please Lefty , i have problem when i add if condition x not print

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.

const int sensor = A0; // analog input use variable resistance
const int x = 2;
int flagged = 0; // create a flag variable

void setup ()
{
pinMode(x,INPUT);
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
{
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

delay (1000);
}

do you add if condition if (x==1)

Your "x" never equals 1, because there is no input for it to change to 1 and at the top you have it as 2.

What are you trying to do with the code, what is its purpose?

The original code was to print out "x" only ONCE, if it equaled 1023 and if the input changed to a new number and then back to 1023.

HazardsMind:
Your "x" never equals 1, because there is no input for it to change to 1 and at the top you have it as 2.

What are you trying to do with the code, what is its purpose?

The original code was to print out "x" only ONCE, if it equaled 1023 and if the input changed to a new number and then back to 1023.

ok i need add if condition before print "x" to the original code ( print " x " when if condition is valid )

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;
 }

delay (1000);
}

i need to add if condition as i shown in code

Oh, then change the ELSE statement to print whatever you want it to say.

"else flagged =0;" change this to,

else { Serial.println("not valid");
         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;

please check this i need

How many inputs do you need? This only looks at 1 input. And to get a different printout instead of "x", change the ELSE statement to what I gave you.