[260] | 1 | #include "r2ca.h"
|
---|
[136] | 2 |
|
---|
| 3 | #define BLINK
|
---|
| 4 | //#define toneMelody
|
---|
| 5 | //#define USBUART
|
---|
| 6 | //#define SERIALUSB
|
---|
| 7 | //#define SERIAL5
|
---|
| 8 | //#define ATTACHINTERRUPT
|
---|
| 9 | //#define ANALOGWRITE
|
---|
| 10 | //#define ANALOGREAD
|
---|
| 11 | //#define RTC_ALARM
|
---|
[197] | 12 | //#define SD_CARD
|
---|
[136] | 13 |
|
---|
| 14 | #ifdef BLINK
|
---|
| 15 | /*
|
---|
| 16 | Blink
|
---|
| 17 | Turns on an LED on for one second, then off for one second, repeatedly.
|
---|
| 18 |
|
---|
| 19 | Most Arduinos have an on-board LED you can control. On the Uno and
|
---|
| 20 | Leonardo, it is attached to digital pin 13. If you're unsure what
|
---|
| 21 | pin the on-board LED is connected to on your Arduino model, check
|
---|
| 22 | the documentation at http://arduino.cc
|
---|
| 23 |
|
---|
| 24 | This example code is in the public domain.
|
---|
| 25 |
|
---|
| 26 | modified 8 May 2014
|
---|
| 27 | by Scott Fitzgerald
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | // the setup function runs once when you press reset or power the board
|
---|
| 31 | void setup() {
|
---|
| 32 | // initialize digital pin 13 as an output.
|
---|
| 33 | pinMode(13, OUTPUT);
|
---|
| 34 | Serial.begin(115200);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | // the loop function runs over and over again forever
|
---|
| 38 | void loop() {
|
---|
| 39 | digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
|
---|
| 40 | Serial.println("HIGH");
|
---|
| 41 | delay(1000); // wait for a second
|
---|
| 42 | digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
|
---|
| 43 | Serial.println("LOW");
|
---|
| 44 | delay(1000); // wait for a second
|
---|
| 45 | }
|
---|
| 46 | #endif /* BLINK */
|
---|
| 47 |
|
---|
| 48 | #ifdef toneMelody
|
---|
| 49 | /*
|
---|
| 50 | Melody
|
---|
| 51 |
|
---|
| 52 | Plays a melody
|
---|
| 53 |
|
---|
| 54 | circuit:
|
---|
| 55 | * 8-ohm speaker on digital pin 8
|
---|
| 56 |
|
---|
| 57 | created 21 Jan 2010
|
---|
| 58 | modified 30 Aug 2011
|
---|
| 59 | by Tom Igoe
|
---|
| 60 |
|
---|
| 61 | This example code is in the public domain.
|
---|
| 62 |
|
---|
| 63 | http://arduino.cc/en/Tutorial/Tone
|
---|
| 64 |
|
---|
| 65 | */
|
---|
| 66 | #include "pitches.h"
|
---|
| 67 |
|
---|
| 68 | // notes in the melody:
|
---|
| 69 | int melody[] = {
|
---|
| 70 | NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
|
---|
| 71 | };
|
---|
| 72 |
|
---|
| 73 | // note durations: 4 = quarter note, 8 = eighth note, etc.:
|
---|
| 74 | int noteDurations[] = {
|
---|
| 75 | 4, 8, 8, 4, 4, 4, 4, 4
|
---|
| 76 | };
|
---|
| 77 |
|
---|
| 78 | void setup() {
|
---|
| 79 | // no need to repeat the melody.
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | void loop() {
|
---|
| 83 | // iterate over the notes of the melody:
|
---|
| 84 | for (int thisNote = 0; thisNote < 8; thisNote++) {
|
---|
| 85 |
|
---|
| 86 | // to calculate the note duration, take one second
|
---|
| 87 | // divided by the note type.
|
---|
| 88 | //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
|
---|
| 89 | int noteDuration = 1000 / noteDurations[thisNote];
|
---|
| 90 | tone(8, melody[thisNote], noteDuration);
|
---|
| 91 |
|
---|
| 92 | // to distinguish the notes, set a minimum time between them.
|
---|
| 93 | // the note's duration + 30% seems to work well:
|
---|
| 94 | int pauseBetweenNotes = noteDuration * 1.30;
|
---|
| 95 | delay(pauseBetweenNotes);
|
---|
| 96 | // stop the tone playing:
|
---|
| 97 | noTone(8);
|
---|
| 98 | }
|
---|
| 99 | delay(2000);
|
---|
| 100 | }
|
---|
| 101 | #endif /* toneMelody */
|
---|
| 102 |
|
---|
| 103 | #ifdef SERIALUSB
|
---|
| 104 |
|
---|
| 105 | void setup() {
|
---|
| 106 | SerialUSB.begin(115200);
|
---|
| 107 | while(!SerialUSB){ ; }
|
---|
| 108 | SerialUSB.println("SerialUSB start!");
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | int val;
|
---|
| 112 | void loop() {
|
---|
| 113 | SerialUSB.println("arrive!");
|
---|
| 114 | delay(1000);
|
---|
| 115 | }
|
---|
| 116 | #endif /* SERIALUSB */
|
---|
| 117 |
|
---|
| 118 | #ifdef SERIAL5
|
---|
| 119 |
|
---|
| 120 | void setup() {
|
---|
| 121 | Serial5.begin(115200);
|
---|
| 122 | Serial5.println("Serial5 start!");
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | int val;
|
---|
| 126 | void loop() {
|
---|
| 127 | Serial5.println("arrive");
|
---|
| 128 | delay(1000);
|
---|
| 129 | }
|
---|
| 130 | #endif /* SERIAL5 */
|
---|
| 131 |
|
---|
| 132 | #ifdef ATTACHINTERRUPT
|
---|
| 133 | const int buttonPin = 7; // the number of the pushbutton pin
|
---|
| 134 | const int ledPin = 13; // the number of the LED pin
|
---|
| 135 |
|
---|
| 136 | // variables will change:
|
---|
| 137 | int buttonState = 0; // variable for reading the pushbutton status
|
---|
| 138 |
|
---|
| 139 | extern void blink(void);
|
---|
| 140 |
|
---|
| 141 | void setup() {
|
---|
| 142 | // initialize the LED pin as an output:
|
---|
| 143 | pinMode(ledPin, OUTPUT);
|
---|
| 144 | // initialize the pushbutton pin as an input:
|
---|
| 145 | pinMode(buttonPin, INPUT);
|
---|
| 146 | Serial.begin(115200);
|
---|
| 147 | attachInterrupt(buttonPin, blink, CHANGE);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | int interrupt;
|
---|
| 151 | /* ToDo */
|
---|
| 152 | void
|
---|
| 153 | blink(void){
|
---|
| 154 | // Serial.println("interrupt!");
|
---|
| 155 | syslog(LOG_NOTICE, "data = %d", interrupt++);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | void loop() {
|
---|
| 159 | // read the state of the pushbutton value:
|
---|
| 160 | buttonState = digitalRead(buttonPin);
|
---|
| 161 |
|
---|
| 162 | // check if the pushbutton is pressed.
|
---|
| 163 | // if it is, the buttonState is HIGH:
|
---|
| 164 | if (buttonState == HIGH) {
|
---|
| 165 | // turn LED on:
|
---|
| 166 | digitalWrite(ledPin, HIGH);
|
---|
| 167 | }
|
---|
| 168 | else {
|
---|
| 169 | // turn LED off:
|
---|
| 170 | digitalWrite(ledPin, LOW);
|
---|
| 171 | }
|
---|
| 172 | delay(10);
|
---|
| 173 | }
|
---|
| 174 | #endif /* ATTACHINTERRUPT */
|
---|
| 175 |
|
---|
| 176 | #ifdef ANALOGWRITE
|
---|
| 177 |
|
---|
| 178 | const int ledPin = 13;
|
---|
| 179 |
|
---|
| 180 | void setup() {
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | void loop() {
|
---|
| 184 | int i;
|
---|
| 185 | while(1){
|
---|
| 186 | for(i= 0; i<255; i++){
|
---|
| 187 | analogWrite(ledPin,i);
|
---|
| 188 | delay(10);
|
---|
| 189 | }
|
---|
| 190 | for(i= 255; i>0; i--){
|
---|
| 191 | analogWrite(ledPin,i);
|
---|
| 192 | delay(10);
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 | #endif /* ANALOGWRITE */
|
---|
| 197 |
|
---|
| 198 | #ifdef ANALOGREAD
|
---|
| 199 | /*
|
---|
| 200 | Analog Input
|
---|
| 201 | Demonstrates analog input by reading an analog sensor on analog pin 0 and
|
---|
| 202 | turning on and off a light emitting diode(LED) connected to digital pin 13.
|
---|
| 203 | The amount of time the LED will be on and off depends on
|
---|
| 204 | the value obtained by analogRead().
|
---|
| 205 |
|
---|
| 206 | The circuit:
|
---|
| 207 | * Potentiometer attached to analog input 0
|
---|
| 208 | * center pin of the potentiometer to the analog pin
|
---|
| 209 | * one side pin (either one) to ground
|
---|
| 210 | * the other side pin to +5V
|
---|
| 211 | * LED anode (long leg) attached to digital output 13
|
---|
| 212 | * LED cathode (short leg) attached to ground
|
---|
| 213 |
|
---|
| 214 | * Note: because most Arduinos have a built-in LED attached
|
---|
| 215 | to pin 13 on the board, the LED is optional.
|
---|
| 216 |
|
---|
| 217 |
|
---|
| 218 | Created by David Cuartielles
|
---|
| 219 | modified 30 Aug 2011
|
---|
| 220 | By Tom Igoe
|
---|
| 221 |
|
---|
| 222 | This example code is in the public domain.
|
---|
| 223 |
|
---|
| 224 | http://arduino.cc/en/Tutorial/AnalogInput
|
---|
| 225 |
|
---|
| 226 | */
|
---|
| 227 | int sensorPin = A0; // select the input pin for the potentiometer
|
---|
| 228 | int ledPin = 13; // select the pin for the LED
|
---|
| 229 | int sensorValue = 0; // variable to store the value coming from the sensor
|
---|
| 230 |
|
---|
| 231 | void setup() {
|
---|
| 232 | // declare the ledPin as an OUTPUT:
|
---|
| 233 | pinMode(ledPin, OUTPUT);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | int i = 0;
|
---|
| 237 | void loop() {
|
---|
| 238 | // read the value from the sensor:
|
---|
| 239 | sensorValue = analogRead(sensorPin);
|
---|
| 240 | analogWrite(ledPin,sensorValue/4);
|
---|
| 241 | delay(10);
|
---|
| 242 | if(i++ == 100){
|
---|
| 243 | i = 0;
|
---|
| 244 | syslog(LOG_NOTICE, "hoge = %d", sensorValue);
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 | #endif /* ANALOGREAD */
|
---|
| 248 |
|
---|
| 249 | #ifdef RTC_ALARM
|
---|
| 250 | /*****************************************************************************************************************************************************************************
|
---|
| 251 | * This sketch demonstrate how to use alarm in interrupt mode.
|
---|
| 252 | This mode is more conveniently because you use processor for other tasks and when alarm match occurs interrupt routine is executed.
|
---|
| 253 | In this way, alarm flag checking is indipendent from main program flow.
|
---|
| 254 | ******************************************************************************************************************************************************************************/
|
---|
| 255 |
|
---|
| 256 | #include <RTCInt.h>
|
---|
| 257 |
|
---|
| 258 | RTCInt rtc;
|
---|
| 259 |
|
---|
| 260 | extern void alarm_int(void);
|
---|
| 261 |
|
---|
| 262 | void setup()
|
---|
| 263 | {
|
---|
| 264 | Serial.begin(115200); //serial communication initializing
|
---|
| 265 | pinMode(13,OUTPUT);
|
---|
| 266 | rtc.begin(TIME_H24); //RTC initializing with 24 hour representation mode
|
---|
| 267 | rtc.setTime(17,0,5,0); //setting time (hour minute and second)
|
---|
| 268 | rtc.setDate(13,8,15); //setting date
|
---|
| 269 | rtc.enableAlarm(SEC,ALARM_INTERRUPT,alarm_int); //enabling alarm in polled mode and match on second
|
---|
| 270 | rtc.local_time.hour=17;
|
---|
| 271 | rtc.local_time.minute=5;
|
---|
| 272 | rtc.local_time.second=10; //setting second to match
|
---|
| 273 | rtc.setAlarm(); //write second in alarm register
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | void loop()
|
---|
| 277 | {
|
---|
| 278 | // digitalWrite(13,HIGH); //main program code
|
---|
| 279 | Serial.println("HIGH!");
|
---|
| 280 | delay(1000);
|
---|
| 281 | // digitalWrite(13,LOW);
|
---|
| 282 | Serial.println("LOW!");
|
---|
| 283 | delay(1000);
|
---|
| 284 |
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 |
|
---|
| 288 | /*************** Interrupt routine for alarm ******************************/
|
---|
| 289 | void alarm_int(void)
|
---|
| 290 | {
|
---|
| 291 | Serial.println("Alarm match!");
|
---|
| 292 | // RTC->MODE2.INTFLAG.bit.ALARM0=1; //clearing alarm0 flag
|
---|
| 293 | rtc.getDate(); //getting date in local structure (local_date)
|
---|
| 294 | rtc.getTime(); //getting time in local structure(local_time)
|
---|
| 295 |
|
---|
| 296 | //printing date in format YYYY/MM/DD
|
---|
| 297 | Serial.print(rtc.local_date.year+2000); // year
|
---|
| 298 | Serial.print('/');
|
---|
| 299 | Serial.print(rtc.local_date.month); // month
|
---|
| 300 | Serial.print('/');
|
---|
| 301 | Serial.print(rtc.local_date.day); // day
|
---|
| 302 | Serial.print(' ');
|
---|
| 303 |
|
---|
| 304 | //printing time
|
---|
| 305 | Serial.print(rtc.local_time.hour); //hour
|
---|
| 306 | Serial.print(':');
|
---|
| 307 | Serial.print(rtc.local_time.minute); //minute
|
---|
| 308 | Serial.print(':');
|
---|
| 309 | Serial.println(rtc.local_time.second); //second
|
---|
| 310 | }
|
---|
| 311 | #endif /* RTC_ALARM */
|
---|
[197] | 312 |
|
---|
| 313 |
|
---|
| 314 | #ifdef SD_CARD
|
---|
| 315 | /*
|
---|
| 316 | SD card datalogger
|
---|
| 317 |
|
---|
| 318 | This example shows how to log data from three analog sensors
|
---|
| 319 | to an SD card using the SD library.
|
---|
| 320 |
|
---|
| 321 | The circuit:
|
---|
| 322 | * analog sensors on analog ins 0, 1, and 2
|
---|
| 323 | * SD card attached to SPI bus as follows:
|
---|
| 324 | ** MOSI - pin 11
|
---|
| 325 | ** MISO - pin 12
|
---|
| 326 | ** CLK - pin 13
|
---|
| 327 | ** CS - pin 4
|
---|
| 328 |
|
---|
| 329 | created 24 Nov 2010
|
---|
| 330 | modified 9 Apr 2012
|
---|
| 331 | by Tom Igoe
|
---|
| 332 |
|
---|
| 333 | This example code is in the public domain.
|
---|
| 334 |
|
---|
| 335 | */
|
---|
| 336 |
|
---|
| 337 | #include <SPI.h>
|
---|
| 338 | #include <SD.h>
|
---|
| 339 |
|
---|
| 340 | // On the Ethernet Shield, CS is pin 4. Note that even if it's not
|
---|
| 341 | // used as the CS pin, the hardware CS pin (10 on most Arduino boards,
|
---|
| 342 | // 53 on the Mega) must be left as an output or the SD library
|
---|
| 343 | // functions will not work.
|
---|
[223] | 344 | const int chipSelect = 10;
|
---|
[197] | 345 |
|
---|
| 346 | void setup()
|
---|
| 347 | {
|
---|
| 348 | // Open serial communications and wait for port to open:
|
---|
| 349 | Serial.begin(115200);
|
---|
| 350 | while (!Serial) {
|
---|
| 351 | ; // wait for serial port to connect. Needed for Leonardo only
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | Serial.print("Initializing SD card...");
|
---|
| 355 | // make sure that the default chip select pin is set to
|
---|
| 356 | // output, even if you don't use it:
|
---|
| 357 | // pinMode(10, OUTPUT);
|
---|
| 358 |
|
---|
| 359 | // see if the card is present and can be initialized:
|
---|
| 360 | if (!SD.begin(chipSelect)) {
|
---|
| 361 | Serial.println("Card failed, or not present");
|
---|
| 362 | // don't do anything more:
|
---|
| 363 | return;
|
---|
| 364 | }
|
---|
| 365 | Serial.println("card initialized.");
|
---|
| 366 |
|
---|
| 367 | if (SD.remove("datalog.txt")) {
|
---|
| 368 | Serial.println("Delete datalog.txt");
|
---|
| 369 | }
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | void loop()
|
---|
| 373 | {
|
---|
| 374 | // make a string for assembling the data to log:
|
---|
| 375 | String dataString = "";
|
---|
| 376 |
|
---|
| 377 | // read three sensors and append to the string:
|
---|
| 378 | for (int analogPin = 0; analogPin < 3; analogPin++) {
|
---|
| 379 | int sensor = analogRead(analogPin);
|
---|
| 380 | dataString += String(sensor);
|
---|
| 381 | if (analogPin < 2) {
|
---|
| 382 | dataString += ",";
|
---|
| 383 | }
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | File dataFile = SD.open("datalog.txt");
|
---|
| 387 | if (dataFile) {
|
---|
| 388 | Serial.println("=== data from file ===");
|
---|
| 389 | while (dataFile.available()) {
|
---|
| 390 | Serial.write(dataFile.read());
|
---|
| 391 | }
|
---|
| 392 | dataFile.close();
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | // open the file. note that only one file can be open at a time,
|
---|
| 396 | // so you have to close this one before opening another.
|
---|
| 397 | dataFile = SD.open("datalog.txt", FILE_WRITE);
|
---|
| 398 |
|
---|
| 399 | // if the file is available, write to it:
|
---|
| 400 | if (dataFile) {
|
---|
| 401 | dataFile.println(dataString);
|
---|
| 402 | dataFile.close();
|
---|
| 403 | // print to the serial port too:
|
---|
| 404 | Serial.println("=== new data to file ===");
|
---|
| 405 | Serial.println(dataString);
|
---|
| 406 | }
|
---|
| 407 | // if the file isn't open, pop up an error:
|
---|
| 408 | else {
|
---|
| 409 | Serial.println("error opening datalog.txt");
|
---|
| 410 | }
|
---|
| 411 | delay(4000);
|
---|
| 412 | }
|
---|
| 413 | #endif /* SD_CARD */
|
---|
| 414 |
|
---|
| 415 |
|
---|