Monday, April 1, 2013

Arduino Due + Android + ADK: change LED brightness by calling analogWrite()

In this example, the LED brightness on Arduino Due can be controlled by sliding a SeekBar on Android device. A Byte of data between 0x00 and 0xFF will be sent to Arduino Due board from Android device via ADK interface, In Arduino side, it call analogWrite() function to change the brightness of the LED.


#include "variant.h"
#include <stdio.h>
#include <adk.h>
#include <stdlib.h>

 
// Accessory descriptor. It's how Arduino identifies itself to Android.
char applicationName[] = "DueAdkAnalogLed"; // the app on your phone
char accessoryName[] = "Arduino Due"; // your Arduino board
char companyName[] = "Arduino-er";
 
// Make up anything you want for these
char versionNumber[] = "0.1";
char serialNumber[] = "1";
char url[] = "https://sites.google.com/site/arduinosite/exercise/androidadkanalogled/AndroidADKAnalogLED_0.1.apk";
 
USBHost Usb;
ADK adk(&Usb, companyName, applicationName, accessoryName,versionNumber,url,serialNumber);
 
// Pin 13 has an LED connected on most Arduino boards.
int led = 13;
 
void setup() {
    Serial.begin(9600);
    cpu_irq_enable();
   
    pinMode(led, OUTPUT);
    //Indicate start of program
    digitalWrite(led, LOW);
    delay(2000);
    digitalWrite(led, HIGH);
    for(int i = 0; i <= 2; i++){
        digitalWrite(led, HIGH);
        delay(250);
        digitalWrite(led, LOW);
        delay(250);
    }
}
 
#define RCVSIZE 128
 
void loop() {

    uint8_t buf[RCVSIZE];
    uint32_t nbread = 0;
   
    Usb.Task();
     
    if (adk.isReady()){
      
        adk.read(&nbread, RCVSIZE, buf);
        if (nbread > 0){
          
            uint8_t asciiChar[2];
          
            //Convert received uint8_t to human readable ASCII
            uint8_t lowNibble = buf[0] & 0x0F;
            uint8_t highNibble = (buf[0] & 0xF0) >> 4;
            
            if(lowNibble <= 0x09){
                asciiChar[1] = '0' + lowNibble;
            }else{
                asciiChar[1] = 'A' + lowNibble - 0x0A;
            }
            
            if(highNibble <= 0x09){
                asciiChar[0] = '0' + highNibble;
            }else{
                asciiChar[0] = 'A' + highNibble - 0x0A;
            }
            
            adk.write(2, asciiChar);
            
            uint8_t ledBrightness = buf[0];
            analogWrite(led, ledBrightness);
        }
         
    }
     
}

Download>>



Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin). The frequency of the PWM signal is approximately 490 Hz.

On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 through 13. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.

The Arduino Due supports analogWrite() on pins 2 through 13, plus pins DAC0 and DAC1. Unlike the PWM pins, DAC0 and DAC1 are Digital to Analog converters, and act as true analog outputs.

You do not need to call pinMode() to set the pin as an output before calling analogWrite().

The analogWrite function has nothing to do with the analog pins or the analogRead function.

source: http://arduino.cc/en/Reference/AnalogWrite

Android side code refer the post "Android code sending command to Arduino to change LED brightness".

1 comment:

  1. Hola, me he preguntado últimamente si la conexión para este tipo de proyectos es exclusiva par el arduino DUE ya que he empezado a investigar para hacer el control con android pero cuento con un arduino Mega ADK por lo que no se si pueda usarla de igual manera y tenga el soporte para hacerlo así como saber si cambiarían las librerías, de todos modos te agradezco tu tiempo en leer esto saludos.

    ReplyDelete