Analog read , problems

Hi ,

I have made the next sketch:

int ledPin = 13; 
int val = 0;

void setup(){
 Serial.begin(9600); 
 pinMode(ledPin,OUTPUT);

}
 
 void loop()
{
 Serial.print(val);
  
  val = analogRead(0); 
 val /=4;  // converter 0-1023 naar 0-255 


 if (val < 74 )
 {
 Serial.println("  Value < 74" );   [color=red]<<<will not work [/color]
   digitalWrite(ledPin,LOW);
 
 }

if (val > 74 )
 {
  Serial.println("  Value > 74" );    [color=red]<<<will not work [/color]
   digitalWrite(ledPin,HIGH);

  
  if (val >119 )
  { 
 // Serial.println("  Value < 119" );     [color=red]<<<will not work [/color]
    digitalWrite(ledPin,LOW);

 }
if (val > 139 )
 { 
  //Serial.println("  Value > 139" );              [color=red]<<<will not work [/color]
   digitalWrite(ledPin,HIGH); 

 }

  }

}

but it’s not working correct , and i can’t find the problem.
When i turn the potentiometer from 0 - 74 the LED is off , and will on at 75.
When i turn the potentiometer from 74 - 119 the LED will OFF after reach 119.
When i turn the potentiometer from 119 - 139 , after reach 139 the LED wil ON

This is working well,
But now i will put some text in this sketch , it will show the status at hyperterminal
But unfortunately this won’t work :~

the LED will ON at 75 and will not turn OFF ( when i have insert te text in this sketch ,

Please some advice , what iám doing wrong,

Thanks in advance ,

Patrick

how is it supposed to work ?

int ledPin = 13; 
int val = 0;

void setup(){
  Serial.begin(9600); 
  pinMode(ledPin,OUTPUT);

}

void loop()
{
  Serial.print(val);

  val = analogRead(0)>>2; 
  if (val <= 74 )
  {
    Serial.println("  Value <= 74" );   
    digitalWrite(ledPin,LOW);
  }
  else if (val > 139 )
  {
    Serial.println("  Value > 139" );    
    digitalWrite(ledPin,HIGH);
  }
  else if (val >119 )
  { 
    Serial.println("  Value > 119" ); 
    digitalWrite(ledPin,LOW);
  }
  else  
  { 
    Serial.println("  Value > 74" ); 
    digitalWrite(ledPin,HIGH); 
  }
}

When i turn the potentiometer from 0 - 74 the LED is off

That’s not what your code says.

Hi ,

if (val < 74 )
{
Serial.println(" Value < 74" );
digitalWrite(ledPin,LOW);

between 0 and 74 the LED should be OFF , this is working !!
}

if (val > 74 )
{
Serial.println(" Value > 74" ); <<<will not work
digitalWrite(ledPin,HIGH);

when the value is 75 , the LED will ON , this is working !!!

if (val >119 )
{
// Serial.println(" Value < 119" ); <<<will not work
digitalWrite(ledPin,LOW);

when the value is less than 119 , the LED will OFF , this is working !!

But it won’t work when i insert the text ???

I hope inform you well

Regards,

Patrick

 Serial.print(val);
  
  val = analogRead(0); 
 val /=4;  // converter 0-1023 naar 0-255

Print the value. Then read the value. How the hell is that supposed to work?

tested code from reply 1 ?

Hi , NY

This exact what i mean ,

What does this part of the sketch mean :

val = analogRead(0)>>2;

please explain it to me,

Many thanks !!

Patrick

Divide by four.

Ahaa...

The 1023 / 4 = 255

Thanks !!

Regards ,

Patrick

Hi ,

I have look for the divide marker >> in the Arduino Cookbook but can’t find no information from this.
either the combination else … if is not explain in the Cookbook ( only if … else )

Where can i find those information ??

Thanks in advance ,

Patrick.

Here

Perfect !!

now my questions are completed .

Many thanks,

Regards,

Patrick