programming II

Using Analog inputs
http://arduino.cc/en/Tutorial/AnalogInputPins
int i = analogRead(0);        // return value from 0 to 1023

map ()
map (reading,minvalue,maxvalue,0,255) ;
map (reading,0,1023,0,255) ;    // map 0-1023 to 0-255

Fade();
Smoothing();
constrain();

Serial

Serial.begin(9600); 
Serial.print(“aaa”);
Serial.println(“aaa”);

Using interrupt
http://www.dave-auld.net/index.php?option=com_content&view=article&id=107:a
rduino-interrupts&catid=53:arduino-input-output-basics&Itemid=107

int pbIn = 0;                              // Interrupt 0 is on DIGITAL PIN 2!
int ledOut = 4;                         // The output LED pin
volatile int state = LOW;      // The input state toggle

void setup()
{
 // Set up the digital pin 2 to an Interrupt and Pin 4 to an Output
 pinMode(ledOut, OUTPUT);

 //Attach the interrupt to the input pin and monitor for ANY Change
 attachInterrupt(pbIn, stateChange, CHANGE);
}

void loop()
{
 //Simulate a long running process or complex task
 for (int i = 0; i < 100; i++)
 {
 delay(10);
 }
}

void stateChange()
{
 state = !state;
 digitalWrite(ledOut, state);
}

Frequency Control
There are 6 PWM channels available. The instruction to produce PWM output is 
analogWrite(pin,Duty), where pin must be 5,6,9,10,11,or 3,and Duty is the duty cycle, entered 
as 0-255 corresponding to 0-100%. The default PWM frequency is 490 Hz. To change the frequency 
an additional instruction is required. The PWM frequency is controlled by three internal 
timers: timer0, timer1,and timer2. The instruction to change the PWM frequency is 
TCCRnB = (TCCRnB & 0xF8) | i where i is chosen depending on which timer is associated with the 
PWM pin whose frequency you want to change. Note that the PWM pins are associated in pairs with 
the same timer. Thus if you change the PWM frequency for pin 9, you will get the same frequency 
for PWM on pin 10. However the duty cycles can be different on the two pins. The table below 
summarizes the options available.

PWM-frequency

Example program :

void setup()
{
 pinMode (3,OUTPUT);                  // make pin 3 an output
 TCCR2B = ( TCCR2B & 0xF8 ) | 2;      // set PWM frequency to 3906 Hz for pin 3 (and 11)
}

void loop()
{
 analogWrite(3,127);               //   do 50% PWM on pin 3 at the frequency set in TCCR2B
}                                  //   127/255.     255 (8bits)  -> 100% duty cycle

.

.

.

.

.

.

.

.

.

.

.

.

 

Leave a comment