Beginners‎ > ‎

reading analog value


The goal of this example is to read an analog value. Based on the value read, a led will blink faster or slower.


WHAT DO YOU NEED




SCHEMATIC



BOARD

In the schema you need a variable resistor connected to pin 15 and to 5V and GND.

SOFTWARE

// test blink with Pinguino

#define PIC18F4550

// pin where the led is connected
#define LED 0

void setup()
{
pinMode(LED,OUTPUT);
}

void loop() T
{
digitalWrite(LED,HIGH);
delay(300+analogRead(15));
digitalWrite(LED,LOW);
delay(300+analogRead(15));
}

As this it's not the first pinguino example, we are going to suppose that you know what means the setup and loop functions, and why appears the #define PIC18F4550 directive.

In this example of new function we are going to use the analogRead.

analogRead will return a 10 bits value, based on the analog value on the specified pin. If the pin has 0V, the returned value will be 0, if the pin has 5V the returned value will be 1023.

Then in this example, we use the readed analog value to determine the value that we pass to the delay function. If the voltage is 0V, we will execute a delay(300), if the value is 5V, we will execute a delay(300+1023)

DO-IT-YOURSELF

- Instead of blinking the led, change it to dimmer



Comments