Go Down

Topic: What am I doing wrong? (Read 1 time) previous topic - next topic

vios

I am trying to use analog data to activate some function at certain level of  analog value but the result is only go to blink2 function even though the analog  value is higher than 400.
Code: [Select]
/*
 Conditionals - If statement

This example demonstrates the use of if() statements.
It reads the state of a potentiometer (an analog input) and turns on an LED
only if the LED goes above a certain threshold level. It prints the analog value
regardless of the level.

The circuit:
* potentiometer connected to analog pin 0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital pin 13 to ground

* Note: On most Arduino boards, there is already an LED on the board
connected to pin 13, so you don't need any extra components for this example.

created 17 Jan 2009
by Tom Igoe

 http://arduino.cc/en/Tutorial/

*/

// These constants won't change:
const int analogPin = 0;     // pin that the sensor is attached to
const int ledPin = 13;       // pin that the LED is attached to
const int threshold = 200;   // an arbitrary threshold level that's in the range of the analog input
const int medium = 400;

void setup() {
 // initialize the LED pin as an output:
 pinMode(ledPin, OUTPUT);
 // initialize serial communications:
 Serial.begin(9600);
}

void loop() {
 // read the value of the potentiometer:
 int analogValue = analogRead(analogPin);

 // if the analog value is high enough, turn on the LED:
 if (analogValue > threshold) {
    //Serial.println(analogValue, DEC)
    blink2 ();
 
 }

 else if (analogValue > medium ) {
   blink3 ();
 }

 else {
   digitalWrite(ledPin,LOW);
 }

 // print the analog value:
 Serial.println(analogValue, DEC);

}

void blink2() {
 digitalWrite(ledPin,HIGH);
 delay (500);
 digitalWrite(ledPin,LOW);
 delay (500);
 digitalWrite(ledPin,HIGH);
 delay (500);
 digitalWrite(ledPin,LOW);
 delay (500);
}

void blink3() {
 digitalWrite(ledPin,HIGH);
 delay (100);
 digitalWrite(ledPin,LOW);
 delay (100);
 digitalWrite(ledPin,HIGH);
 delay (100);
 digitalWrite(ledPin,LOW);
 delay (100);
 digitalWrite(ledPin,HIGH);
 delay (100);
 digitalWrite(ledPin,LOW);
 delay (1000);
}


And for the blink2 and blink3 function how can I make it shorter?

Pls help.

Thanks.

mem

Make sure you are connecting to analog 0 pin, not digital 0.
The pin you want is the one at the bottom of this picture


The duration of the delay is determined by the value in the delay function. Reducting this shortens the delay.  

 

vios

Yes I am connecting to analog pin 0. Is there anything wrong with the code?

raron

This sounds like some kind of tutorial excersize, so I don't want to just say the answer (of which there might be several).
But have a look at the if - else construct: http://arduino.cc/en/Reference/Else
Ok
Maybe a strong hint then:
Quote
else can proceed another if test, so that multiple, mutually exclusive tests can be run at the same time.

;)
As for the length of the blink()2 and 3, no hint, but using the old grey stuff: Think, what is the point of it and why do it like that?

mmi

Your code is not ok.
If analogValue is greater than medium then it's also always greater than threshold. Your else statement will never be reached!

Ask first for analogValue > medium and then for analogValue > threshold.

greets, mmi

vios

Hi mmi.

I have tried several ways of using operators without success. It's look like your giving some light to me. I will try it this evening and let you know the outcome.

Go Up