Posts

Showing posts with the label Code example

Implement Timer Interrupt for Arduino Due

Image
The code implement Timer Interrupt for Arduino Due, to toggle LED every second and send the duration in millisecond to PC via Serial port. int led = 13; volatile boolean ledon; volatile unsigned long lasttime; volatile unsigned long now; int FREQ_1Hz = 1; void TC3_Handler(){ TC_GetStatus(TC1, 0); now = millis(); digitalWrite(led, ledon = !ledon); Serial.println(now - lasttime); lasttime = now; } void startTimer(Tc *tc, uint32_t channel, IRQn_Type irq, uint32_t frequency){ //Enable or disable write protect of PMC registers. pmc_set_writeprotect(false); //Enable the specified peripheral clock. pmc_enable_periph_clk((uint32_t)irq); TC_Configure(tc, channel, TC_CMR_WAVE|TC_CMR_WAVSEL_UP_RC|TC_CMR_TCCLKS_TIMER_CLOCK4); uint32_t rc = VARIANT_MCK/128/frequency; TC_SetRA(tc, channel, rc/2); TC_SetRC(tc, channel, rc); TC_Start(tc, channel); tc->TC_CHANNEL[channel].TC_IER = TC_IER_CPCS; tc->TC_CH...

Android code sending command to Arduino Due to turn LED On/Off

Image
It's the coding in Android side to send command to Arduino Due to control LED On/Off. Modify AndroidManifest.xml to add <uses-feature> of "android.hardware.usb.accessory", <intent-filter> of "android.hardware.usb.action.USB_ACCESSORY_ATTACHED", and <meta-data> of resource="@xml/myfilter". <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.androidadkled" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="12" android:targetSdkVersion="17" /> <uses-feature android:name="android.hardware.usb.accessory"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" ...

Read byte from Serial port and set pin

Image
This code run on Arduino Due, read byte from Serial port, and set LED pin HIGH/LOW according. int led = 13; int incomingByte = 0; void setup() { pinMode(led, OUTPUT); //Setup Serial Port with baud rate of 115200 Serial.begin(115200); Serial.println("Press H to turn LED ON"); Serial.println("Press L to turn LED OFF"); } void loop() { if (Serial.available() > 0) { // read the incoming byte: incomingByte = Serial.read(); if(incomingByte == 'H'){ digitalWrite(led, HIGH); Serial.println("LED ON"); }else if(incomingByte == 'L'){ digitalWrite(led, LOW); Serial.println("LED OFF"); }else{ Serial.println("invalid!"); } } }

Hello World ADK: Communication between Arduino Due and Android device

Image
The former post demonstrate half " Pre-Hello World for ADK ". It's further implemented to include communication between Arduino Due and Android Device. In Arduino Due side: it monitor any incoming data from accessory, and send it back. In Android side, it will send out the text when Send button clicked. And display the text received from USB. It implement the function of bi-direction communication between Arduino Due and Android Device. The code in Arduino side is listed here: #include "variant.h" #include <stdio.h> #include <adk.h> // Accessory descriptor. It's how Arduino identifies itself to Android. char applicationName[] = "HelloADK"; // 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/si...