| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

How to show Arduino analog values on the Amber

Page history last edited by mike 11 years, 9 months ago

Use the following code to read and write analog values from the Arduino on the Amber.

 

Parts:

 

1. Amber 

2. Arduino Duemilanove (FTDI based)

 

Instructions

 

Use Eclipse IDE for Java for compiling and uploading Android code.

Use the Arduino IDE for compiling and uploading Arduino code.

 

Import the Android code. First, download a zip or clone the git repo:

 

Source: https://github.com/liquidware/amber-serial

 

The notable bits of the code show a background task that sends a msg over serial when a button is pressed:

 

Notice the transmit code (Amber>>Arduino)

protected Boolean doInBackground(String... cmd) { //execute this method in the background
            mActiveCmd = cmd[0]; // get the commands and stored in the mActiveCmd variable
            if (mActiveCmd.equals("a")) { // if the Command equals "a", i.e. turning on the LED
                send("a"); // send the character "a" to the Arduino
                setTimeout(3000); // set the Timeout to 3000ms in case the serial port does not respond
            } else if (mActiveCmd.equals("b")) { // if the Command equals "b", i.e. turning off the LED
                send("b"); // send the character "b" to the Arduino
                setTimeout(3000); // set the Timeout to 3000ms in case the serial port does not respond
            } else {
                return false; // return false, i.e. failure
            }
            return true; // return true, i.e. success
        }

 

Notice the receive code (Amber<<Arduino)

    // this method is called when a new data byte array is received
    protected void onDataReceived(final byte[] buffer, final int size) {
        runOnUiThread(new Runnable() {
            public void run() {
                mProgressBar1.setProgress(Integer.parseInt(new String(buffer, 0, size))); // set the progress bar to the new received data
                mAnalogLightValue.setText("Analog Light Value: " + (new String(buffer, 0, size))); // update the text displayed on the screen
            }
        });
    }

 

Compile and upload the Arduino code in the Arduino IDE:

 

/**
* AmberTest
* Arduino example by Huang Yu, Mike Gionfriddo, and Chris Ladden
*
* This example demonstrates sending analog
* values to the serial port. This sketch
* is simply broadcasting an analog value
* as a packet.
*
* Notes:
* 1.) Set the baud rate
* 2.) Read some analog value
* 3.) Print the value to the serial port
*/


void setup()
{
    Serial.begin(115200);//setup a serial port with a baud of 115200
    
    pinMode(18, OUTPUT);// set analog pin4 as a digital output
    pinMode(19, OUTPUT);// set analog pin5 as a digital output
    digitalWrite(18, HIGH);// set analog pin4 as a digital high (+5V)
    digitalWrite(19, LOW);// set analog pin5 as a digital ground (0V)
    pinMode(13, OUTPUT); // set pin13 as a digital output
}



void loop()
{
    int value = analogRead(3);//Check analog pin 3 to update  
    byte in;// define a byte called, "in"

// sprintf(out, "arduino #%d\n", 0);//prep the out file
    in = Serial.read(); // read from the serial port and save it as "in"

    if (in == 97) { //if an "a" is received via serial, do the following
      digitalWrite(13, HIGH);// write pin 13 to the high (LED On)
    }
    else if (in == 98) { //if a "b" is received via serial, do the following
      digitalWrite(13, LOW);// write pin 13 to the off (LED Off)
    }
    else {}// do nothing
    
    Serial.print(value);//print the analog value to the serial
    delay(1000);//wait for 1000ms
}

Comments (0)

You don't have permission to comment on this page.