[136] | 1 | #include "rca.h"
|
---|
| 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
|
---|
| 12 |
|
---|
| 13 | #ifdef BLINK
|
---|
| 14 | /*
|
---|
| 15 | Blink
|
---|
| 16 | Turns on an LED on for one second, then off for one second, repeatedly.
|
---|
| 17 |
|
---|
| 18 | Most Arduinos have an on-board LED you can control. On the Uno and
|
---|
| 19 | Leonardo, it is attached to digital pin 13. If you're unsure what
|
---|
| 20 | pin the on-board LED is connected to on your Arduino model, check
|
---|
| 21 | the documentation at http://arduino.cc
|
---|
| 22 |
|
---|
| 23 | This example code is in the public domain.
|
---|
| 24 |
|
---|
| 25 | modified 8 May 2014
|
---|
| 26 | by Scott Fitzgerald
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | // the setup function runs once when you press reset or power the board
|
---|
| 30 | void setup() {
|
---|
| 31 | // initialize digital pin 13 as an output.
|
---|
| 32 | pinMode(13, OUTPUT);
|
---|
| 33 | Serial.begin(115200);
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | // the loop function runs over and over again forever
|
---|
| 37 | void loop() {
|
---|
| 38 | digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
|
---|
| 39 | Serial.println("HIGH");
|
---|
| 40 | delay(1000); // wait for a second
|
---|
| 41 | digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
|
---|
| 42 | Serial.println("LOW");
|
---|
| 43 | delay(1000); // wait for a second
|
---|
| 44 | }
|
---|
| 45 | #endif /* BLINK */
|
---|
| 46 |
|
---|
| 47 | #ifdef toneMelody
|
---|
| 48 | /*
|
---|
| 49 | Melody
|
---|
| 50 |
|
---|
| 51 | Plays a melody
|
---|
| 52 |
|
---|
| 53 | circuit:
|
---|
| 54 | * 8-ohm speaker on digital pin 8
|
---|
| 55 |
|
---|
| 56 | created 21 Jan 2010
|
---|
| 57 | modified 30 Aug 2011
|
---|
| 58 | by Tom Igoe
|
---|
| 59 |
|
---|
| 60 | This example code is in the public domain.
|
---|
| 61 |
|
---|
| 62 | http://arduino.cc/en/Tutorial/Tone
|
---|
| 63 |
|
---|
| 64 | */
|
---|
| 65 | #include "pitches.h"
|
---|
| 66 |
|
---|
| 67 | // notes in the melody:
|
---|
| 68 | int melody[] = {
|
---|
| 69 | NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
|
---|
| 70 | };
|
---|
| 71 |
|
---|
| 72 | // note durations: 4 = quarter note, 8 = eighth note, etc.:
|
---|
| 73 | int noteDurations[] = {
|
---|
| 74 | 4, 8, 8, 4, 4, 4, 4, 4
|
---|
| 75 | };
|
---|
| 76 |
|
---|
| 77 | void setup() {
|
---|
| 78 | // no need to repeat the melody.
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | void loop() {
|
---|
| 82 | // iterate over the notes of the melody:
|
---|
| 83 | for (int thisNote = 0; thisNote < 8; thisNote++) {
|
---|
| 84 |
|
---|
| 85 | // to calculate the note duration, take one second
|
---|
| 86 | // divided by the note type.
|
---|
| 87 | //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
|
---|
| 88 | int noteDuration = 1000 / noteDurations[thisNote];
|
---|
| 89 | tone(8, melody[thisNote], noteDuration);
|
---|
| 90 |
|
---|
| 91 | // to distinguish the notes, set a minimum time between them.
|
---|
| 92 | // the note's duration + 30% seems to work well:
|
---|
| 93 | int pauseBetweenNotes = noteDuration * 1.30;
|
---|
| 94 | delay(pauseBetweenNotes);
|
---|
| 95 | // stop the tone playing:
|
---|
| 96 | noTone(8);
|
---|
| 97 | }
|
---|
| 98 | delay(2000);
|
---|
| 99 | }
|
---|
| 100 | #endif /* toneMelody */
|
---|
| 101 |
|
---|
| 102 | #ifdef SERIALUSB
|
---|
| 103 |
|
---|
| 104 | void setup() {
|
---|
| 105 | SerialUSB.begin(115200);
|
---|
| 106 | while(!SerialUSB){ ; }
|
---|
| 107 | SerialUSB.println("SerialUSB start!");
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | int val;
|
---|
| 111 | void loop() {
|
---|
| 112 | SerialUSB.println("arrive!");
|
---|
| 113 | delay(1000);
|
---|
| 114 | }
|
---|
| 115 | #endif /* SERIALUSB */
|
---|
| 116 |
|
---|
| 117 | #ifdef SERIAL5
|
---|
| 118 |
|
---|
| 119 | void setup() {
|
---|
| 120 | Serial5.begin(115200);
|
---|
| 121 | Serial5.println("Serial5 start!");
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | int val;
|
---|
| 125 | void loop() {
|
---|
| 126 | Serial5.println("arrive");
|
---|
| 127 | delay(1000);
|
---|
| 128 | }
|
---|
| 129 | #endif /* SERIAL5 */
|
---|
| 130 |
|
---|
| 131 | #ifdef ATTACHINTERRUPT
|
---|
| 132 | const int buttonPin = 7; // the number of the pushbutton pin
|
---|
| 133 | const int ledPin = 13; // the number of the LED pin
|
---|
| 134 |
|
---|
| 135 | // variables will change:
|
---|
| 136 | int buttonState = 0; // variable for reading the pushbutton status
|
---|
| 137 |
|
---|
| 138 | extern void blink(void);
|
---|
| 139 |
|
---|
| 140 | void setup() {
|
---|
| 141 | // initialize the LED pin as an output:
|
---|
| 142 | pinMode(ledPin, OUTPUT);
|
---|
| 143 | // initialize the pushbutton pin as an input:
|
---|
| 144 | pinMode(buttonPin, INPUT);
|
---|
| 145 | Serial.begin(115200);
|
---|
| 146 | attachInterrupt(buttonPin, blink, CHANGE);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | int interrupt;
|
---|
| 150 | /* ToDo */
|
---|
| 151 | void
|
---|
| 152 | blink(void){
|
---|
| 153 | // Serial.println("interrupt!");
|
---|
| 154 | syslog(LOG_NOTICE, "data = %d", interrupt++);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | void loop() {
|
---|
| 158 | // read the state of the pushbutton value:
|
---|
| 159 | buttonState = digitalRead(buttonPin);
|
---|
| 160 |
|
---|
| 161 | // check if the pushbutton is pressed.
|
---|
| 162 | // if it is, the buttonState is HIGH:
|
---|
| 163 | if (buttonState == HIGH) {
|
---|
| 164 | // turn LED on:
|
---|
| 165 | digitalWrite(ledPin, HIGH);
|
---|
| 166 | }
|
---|
| 167 | else {
|
---|
| 168 | // turn LED off:
|
---|
| 169 | digitalWrite(ledPin, LOW);
|
---|
| 170 | }
|
---|
| 171 | delay(10);
|
---|
| 172 | }
|
---|
| 173 | #endif /* ATTACHINTERRUPT */
|
---|
| 174 |
|
---|
| 175 | #ifdef ANALOGWRITE
|
---|
| 176 |
|
---|
| 177 | const int ledPin = 13;
|
---|
| 178 |
|
---|
| 179 | void setup() {
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | void loop() {
|
---|
| 183 | int i;
|
---|
| 184 | while(1){
|
---|
| 185 | for(i= 0; i<255; i++){
|
---|
| 186 | analogWrite(ledPin,i);
|
---|
| 187 | delay(10);
|
---|
| 188 | }
|
---|
| 189 | for(i= 255; i>0; i--){
|
---|
| 190 | analogWrite(ledPin,i);
|
---|
| 191 | delay(10);
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 | #endif /* ANALOGWRITE */
|
---|
| 196 |
|
---|
| 197 | #ifdef ANALOGREAD
|
---|
| 198 | /*
|
---|
| 199 | Analog Input
|
---|
| 200 | Demonstrates analog input by reading an analog sensor on analog pin 0 and
|
---|
| 201 | turning on and off a light emitting diode(LED) connected to digital pin 13.
|
---|
| 202 | The amount of time the LED will be on and off depends on
|
---|
| 203 | the value obtained by analogRead().
|
---|
| 204 |
|
---|
| 205 | The circuit:
|
---|
| 206 | * Potentiometer attached to analog input 0
|
---|
| 207 | * center pin of the potentiometer to the analog pin
|
---|
| 208 | * one side pin (either one) to ground
|
---|
| 209 | * the other side pin to +5V
|
---|
| 210 | * LED anode (long leg) attached to digital output 13
|
---|
| 211 | * LED cathode (short leg) attached to ground
|
---|
| 212 |
|
---|
| 213 | * Note: because most Arduinos have a built-in LED attached
|
---|
| 214 | to pin 13 on the board, the LED is optional.
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 | Created by David Cuartielles
|
---|
| 218 | modified 30 Aug 2011
|
---|
| 219 | By Tom Igoe
|
---|
| 220 |
|
---|
| 221 | This example code is in the public domain.
|
---|
| 222 |
|
---|
| 223 | http://arduino.cc/en/Tutorial/AnalogInput
|
---|
| 224 |
|
---|
| 225 | */
|
---|
| 226 | int sensorPin = A0; // select the input pin for the potentiometer
|
---|
| 227 | int ledPin = 13; // select the pin for the LED
|
---|
| 228 | int sensorValue = 0; // variable to store the value coming from the sensor
|
---|
| 229 |
|
---|
| 230 | void setup() {
|
---|
| 231 | // declare the ledPin as an OUTPUT:
|
---|
| 232 | pinMode(ledPin, OUTPUT);
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | int i = 0;
|
---|
| 236 | void loop() {
|
---|
| 237 | // read the value from the sensor:
|
---|
| 238 | sensorValue = analogRead(sensorPin);
|
---|
| 239 | analogWrite(ledPin,sensorValue/4);
|
---|
| 240 | delay(10);
|
---|
| 241 | if(i++ == 100){
|
---|
| 242 | i = 0;
|
---|
| 243 | syslog(LOG_NOTICE, "hoge = %d", sensorValue);
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 | #endif /* ANALOGREAD */
|
---|
| 247 |
|
---|
| 248 | #ifdef RTC_ALARM
|
---|
| 249 | /*****************************************************************************************************************************************************************************
|
---|
| 250 | * This sketch demonstrate how to use alarm in interrupt mode.
|
---|
| 251 | This mode is more conveniently because you use processor for other tasks and when alarm match occurs interrupt routine is executed.
|
---|
| 252 | In this way, alarm flag checking is indipendent from main program flow.
|
---|
| 253 | ******************************************************************************************************************************************************************************/
|
---|
| 254 |
|
---|
| 255 | #include <RTCInt.h>
|
---|
| 256 |
|
---|
| 257 | RTCInt rtc;
|
---|
| 258 |
|
---|
| 259 | extern void alarm_int(void);
|
---|
| 260 |
|
---|
| 261 | void setup()
|
---|
| 262 | {
|
---|
| 263 | Serial.begin(115200); //serial communication initializing
|
---|
| 264 | pinMode(13,OUTPUT);
|
---|
| 265 | rtc.begin(TIME_H24); //RTC initializing with 24 hour representation mode
|
---|
| 266 | rtc.setTime(17,0,5,0); //setting time (hour minute and second)
|
---|
| 267 | rtc.setDate(13,8,15); //setting date
|
---|
| 268 | rtc.enableAlarm(SEC,ALARM_INTERRUPT,alarm_int); //enabling alarm in polled mode and match on second
|
---|
| 269 | rtc.local_time.hour=17;
|
---|
| 270 | rtc.local_time.minute=5;
|
---|
| 271 | rtc.local_time.second=10; //setting second to match
|
---|
| 272 | rtc.setAlarm(); //write second in alarm register
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | void loop()
|
---|
| 276 | {
|
---|
| 277 | // digitalWrite(13,HIGH); //main program code
|
---|
| 278 | Serial.println("HIGH!");
|
---|
| 279 | delay(1000);
|
---|
| 280 | // digitalWrite(13,LOW);
|
---|
| 281 | Serial.println("LOW!");
|
---|
| 282 | delay(1000);
|
---|
| 283 |
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 |
|
---|
| 287 | /*************** Interrupt routine for alarm ******************************/
|
---|
| 288 | void alarm_int(void)
|
---|
| 289 | {
|
---|
| 290 | Serial.println("Alarm match!");
|
---|
| 291 | // RTC->MODE2.INTFLAG.bit.ALARM0=1; //clearing alarm0 flag
|
---|
| 292 | rtc.getDate(); //getting date in local structure (local_date)
|
---|
| 293 | rtc.getTime(); //getting time in local structure(local_time)
|
---|
| 294 |
|
---|
| 295 | //printing date in format YYYY/MM/DD
|
---|
| 296 | Serial.print(rtc.local_date.year+2000); // year
|
---|
| 297 | Serial.print('/');
|
---|
| 298 | Serial.print(rtc.local_date.month); // month
|
---|
| 299 | Serial.print('/');
|
---|
| 300 | Serial.print(rtc.local_date.day); // day
|
---|
| 301 | Serial.print(' ');
|
---|
| 302 |
|
---|
| 303 | //printing time
|
---|
| 304 | Serial.print(rtc.local_time.hour); //hour
|
---|
| 305 | Serial.print(':');
|
---|
| 306 | Serial.print(rtc.local_time.minute); //minute
|
---|
| 307 | Serial.print(':');
|
---|
| 308 | Serial.println(rtc.local_time.second); //second
|
---|
| 309 | }
|
---|
| 310 | #endif /* RTC_ALARM */
|
---|