Advertisement
sgccarey

HelloADK arduino Sketch

Oct 27th, 2011
2,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. #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.
  2. #include <Max3421e.h> // USB host controller
  3. #include <Usb.h> // USB library
  4. #include <AndroidAccessory.h> // Needed for Android communication
  5.  
  6. // #include <Servo.h> // http://www.arduino.cc/playground/ComponentLib/Servo // don't think is needed
  7.  
  8. #define LED_GREEN 12 // Links pin 12 to a variable name; LED_GREEN. Could just use '13' through, but this makes it clearer
  9.  
  10.  
  11.  
  12. // identifies the accessory to the android device
  13. AndroidAccessory acc("SaltApps", // manufacturer
  14. "HelloADK", // Model
  15. "Demo of Arduino Board", // description
  16. "1.0", // version
  17. "http://www.android.com", // URL
  18. "0000000012345678"); // serial #
  19.  
  20.  
  21. void setup(); // calls function
  22. void loop(); // calls function
  23.  
  24.  
  25. void init_leds()
  26. {
  27. pinMode(LED_GREEN, OUTPUT); // initializes pin to behave as an output. Pins are INPUT by default
  28. digitalWrite(LED_GREEN, 1); // initialises pin to value 1 of 255 (on, basically).
  29. // The lower the value the brighter the light
  30. // should maybe be: analogWrite(LEDPIN, 1);?
  31.  
  32. /*
  33. 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.
  34. Writing LOW will disable the pullup. The pullup resistor acts as a known value when no other input is present (such as when
  35. pressing the button). Later you can use digitalRead() to check the value to determine if the button has been pressed or not.
  36. */
  37.  
  38. Serial.print("\r\n init_leds() ran"); // prints message to tools/serial monitor
  39.  
  40. } // init_leds()
  41.  
  42.  
  43. void setup() // begins serial communications with the device and then calls all the initialization methods you saw earlier.
  44. {
  45. Serial.begin(115200); // baud rate of serial monitor
  46. Serial.print("\r\nStart - Setup ran"); // prints message to tools/serial monitor. Set to 115,200 baud
  47. init_leds(); // calls init_leds() function
  48. acc.powerOn(); // switches accessory power on
  49. Serial.print("\r\nStart - accessory power on"); // prints message to tools/serial monitor
  50.  
  51. } // setup()
  52.  
  53.  
  54. void loop() // The loop() function is the heart beat of the Arduino Base Board. After creating and executing the setup() function,
  55. // which is responsible for initializing the initial values, the loop begins its magic and loops consecutively, allowing
  56. // the sketch to analyze and respond accordingly.
  57. {
  58. byte err;
  59. byte idle;
  60. static byte count = 0;
  61. byte msg[3];
  62. long touchcount;
  63.  
  64. if (acc.isConnected()) { // if accessory is connected
  65. Serial.print("\r\nStart - accessory connected"); // prints message to tools/serial monitor
  66. int len = acc.read(msg, sizeof(msg), 1); // gets length of message from accessory?
  67. int i;
  68. byte b;
  69. uint16_t val;
  70. int x, y;
  71. char c0;
  72.  
  73. if (len > 0) { // If the length of the input buffer from the attached Android device is greater than zero,
  74. // the loop process the input command to determine what it should do. Assumes only one command per packet
  75.  
  76. if (msg[0] == 0x2) { // 'msg[0] == 2' indicates that LEDs are the target
  77. if (msg[1] == 0x0) // 'msg[1] == VALUE' indicates the pin to update
  78. analogWrite(LED_GREEN, 255 - msg[2]); // value is set using 'msg[2]' as value
  79. // digitalWrite(LED_GREEN, HIGH); // set the LED on
  80.  
  81. /* 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).
  82. 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
  83. value for “bright”
  84. */
  85.  
  86. } // if 3
  87. } // if 2
  88.  
  89. } else {
  90. // reset outputs to default values on disconnect
  91. analogWrite(LED_GREEN, 255); // turns the LED off. Lower the value the brighter the light
  92. // digitalWrite(13, LOW); // set the LED off
  93. Serial.print("\r\nStart - accessory not connected"); // prints message to tools/serial monitor
  94.  
  95. } // if/else
  96.  
  97. delay(10); // Then the loop ends by delaying for a few milliseconds.
  98.  
  99. } // loop()
  100.  
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement