🛠 Flux de capteur analogique vers Android

Arduino Mega ADK (Rev3) · intermediaire · Schéma : IMG-mega-adk-intermediaire-2-mega-adk-flux-capteur-android.svg — Potentiomètre sur A0, trames envoyées au téléphone Android via le port USB-A host, jauge de mesure à l'écran

Objectives

Continuously send the value of an analog sensor (potentiometer on A0) to a mini Android dashboard: the board sends compact binary frames, the application decodes them and displays a gauge. This exercises the 16 10-bit analog inputs of the ATmega2560 and the two bulk endpoints of the AOA protocol. Frame format choice: 3 bytes [0x80][high byte][low byte] per measurement, simpler to decode on the Java side than an ASCII stream without delimiters.

Hardware

Full Code

Board side (sending frames at 10 Hz):

// mega-adk-flux-capteur-android.ino — Board: "Arduino Mega ADK"
#include <Usb.h>
#include <usbhub.h>
#include <adk.h>

USB Usb;
USBHub hub0(&Usb);
ADK adk(&Usb, "AtelierADK", "FluxCapteur", "Trames capteur A0",
        "1.0", "http://example.org", "SN000002");

const byte CANAL = A0;

void setup {
  Serial.begin(115200);
  if (Usb.Init == -1) { Serial.println(F("OSCOKIRQ failed to assert")); while (1); }
}

void loop {
  Usb.Task;
  if (!adk.isReady) return;

  int v = analogRead(CANAL);                 // 0..1023
  uint8_t trame[3] = { 0x80, (uint8_t)(v >> 8), (uint8_t)(v & 0xFF) };
  if (adk.SndData(3, trame) != 0) {          // échec d'envoi : téléphone parti
    Serial.println(F("Telephone debranche"));
  }
  delay(100);                                // 10 Hz
}

Android side (decoding + gauge):

// FluxActivity.java (extrait) — lit les trames [0x80][hi][lo]
private void boucleFlux {
    byte[] entete = new byte[1];
    byte[] corps  = new byte[2];
    try {
        while (in.read(entete) == 1) {
            if (entete[0] != (byte) 0x80) continue;   // resynchronisation
            if (in.read(corps) != 2) break;
            final int valeur = ((corps[0] & 0xFF) << 8) | (corps[1] & 0xFF);
            runOnUiThread( -> {
                jauge.setProgress(valeur);            // ProgressBar max=1023
                texte.setText("Capteur A0 : " + valeur + " / 1023");
            });
        }
    } catch (IOException deconnexion) { /* câble retiré */ }
}
// En cas de trame coupée (téléphone en veille), on resynchronise sur 0x80 :
// un en-tête unique 0x80 est rare dans les données 0-1023 (hi vaut 0 ou 3 max).

Expected Verification

The Android gauge follows the potentiometer in near real-time (10 measurements/s). When unplugging the cable, the application does not crash: the read operation throws an IOException and the interface switches to the "disconnected" state. The displayed value remains consistent with the serial console if you add a Serial.println for control.

Common Errors

  1. Sending ASCII integers without a delimiter: the receiver cannot find frame boundaries — prefer fixed-length binary or terminate each line with \n.
  2. Forgetting Usb.Task between two SndData calls: USB transfers stop progressing, and the gauge freezes.
  3. Bombarding without control: beyond the useful throughput, sends fail silently; check the return value of SndData.
  4. Phone in deep sleep cutting USB power: the board must detect the disconnection (isReady false) and return to waiting state without restarting.
  5. Power budget exceeded: without an external power supply ≥ 1.5 A, the load of the phone + board saturates the 500 mA available on USB, and the link drops intermittently.
  6. The 500 mA polyfuse on the host port trips if an external device is short-circuited: the port remains dead for a few seconds, then resets itself.

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials