Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h> // This library allows you to communicate with I2C / TWI devices. On most Arduino boards, SDA (data line) is on analog input pin 4, and SCL (clock line) is on analog input pin 5. On the Arduino Mega, SDA is digital pin 20 and SCL is 21.
- #include <Max3421e.h> // USB host controller
- #include <Usb.h> // USB library
- #include <AndroidAccessory.h> // Needed for Android communication
- // #include <Servo.h> // http://www.arduino.cc/playground/ComponentLib/Servo // don't think is needed
- #define LED_GREEN 12 // Links pin 12 to a variable name; LED_GREEN. Could just use '13' through, but this makes it clearer
- // identifies the accessory to the android device
- AndroidAccessory acc("SaltApps", // manufacturer
- "HelloADK", // Model
- "Demo of Arduino Board", // description
- "1.0", // version
- "http://www.android.com", // URL
- "0000000012345678"); // serial #
- void setup(); // calls function
- void loop(); // calls function
- void init_leds()
- {
- pinMode(LED_GREEN, OUTPUT); // initializes pin to behave as an output. Pins are INPUT by default
- digitalWrite(LED_GREEN, 1); // initialises pin to value 1 of 255 (on, basically).
- // The lower the value the brighter the light
- // should maybe be: analogWrite(LEDPIN, 1);?
- /*
- a pin configured as an INPUT can have a HIGH or LOW value. Writing a HIGH value with will enable an internal 20K pullup resistor.
- Writing LOW will disable the pullup. The pullup resistor acts as a known value when no other input is present (such as when
- pressing the button). Later you can use digitalRead() to check the value to determine if the button has been pressed or not.
- */
- Serial.print("\r\n init_leds() ran"); // prints message to tools/serial monitor
- } // init_leds()
- void setup() // begins serial communications with the device and then calls all the initialization methods you saw earlier.
- {
- Serial.begin(115200); // baud rate of serial monitor
- Serial.print("\r\nStart - Setup ran"); // prints message to tools/serial monitor. Set to 115,200 baud
- init_leds(); // calls init_leds() function
- acc.powerOn(); // switches accessory power on
- Serial.print("\r\nStart - accessory power on"); // prints message to tools/serial monitor
- } // setup()
- void loop() // The loop() function is the heart beat of the Arduino Base Board. After creating and executing the setup() function,
- // which is responsible for initializing the initial values, the loop begins its magic and loops consecutively, allowing
- // the sketch to analyze and respond accordingly.
- {
- byte err;
- byte idle;
- static byte count = 0;
- byte msg[3];
- long touchcount;
- if (acc.isConnected()) { // if accessory is connected
- Serial.print("\r\nStart - accessory connected"); // prints message to tools/serial monitor
- int len = acc.read(msg, sizeof(msg), 1); // gets length of message from accessory?
- int i;
- byte b;
- uint16_t val;
- int x, y;
- char c0;
- if (len > 0) { // If the length of the input buffer from the attached Android device is greater than zero,
- // the loop process the input command to determine what it should do. Assumes only one command per packet
- if (msg[0] == 0x2) { // 'msg[0] == 2' indicates that LEDs are the target
- if (msg[1] == 0x0) // 'msg[1] == VALUE' indicates the pin to update
- analogWrite(LED_GREEN, 255 - msg[2]); // value is set using 'msg[2]' as value
- // digitalWrite(LED_GREEN, HIGH); // set the LED on
- /* In the case of the value for a LED, the lower the value the brighter the light (or the higher the value the more black and darker the light).
- The DemoKit app on the Android device sends 255 when it wants a full bright light so the value is subtracted from 255 to set the appropriate
- value for “bright”
- */
- } // if 3
- } // if 2
- } else {
- // reset outputs to default values on disconnect
- analogWrite(LED_GREEN, 255); // turns the LED off. Lower the value the brighter the light
- // digitalWrite(13, LOW); // set the LED off
- Serial.print("\r\nStart - accessory not connected"); // prints message to tools/serial monitor
- } // if/else
- delay(10); // Then the loop ends by delaying for a few milliseconds.
- } // loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement