source: rtos_arduino/trunk/examples/basic/rca_app.cpp@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 7.6 KB
Line 
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
30void 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
37void 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
60This 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:
68int 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.:
73int noteDurations[] = {
74 4, 8, 8, 4, 4, 4, 4, 4
75};
76
77void setup() {
78 // no need to repeat the melody.
79}
80
81void 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
104void setup() {
105 SerialUSB.begin(115200);
106 while(!SerialUSB){ ; }
107 SerialUSB.println("SerialUSB start!");
108}
109
110int val;
111void loop() {
112 SerialUSB.println("arrive!");
113 delay(1000);
114}
115#endif /* SERIALUSB */
116
117#ifdef SERIAL5
118
119void setup() {
120 Serial5.begin(115200);
121 Serial5.println("Serial5 start!");
122}
123
124int val;
125void loop() {
126 Serial5.println("arrive");
127 delay(1000);
128}
129#endif /* SERIAL5 */
130
131#ifdef ATTACHINTERRUPT
132const int buttonPin = 7; // the number of the pushbutton pin
133const int ledPin = 13; // the number of the LED pin
134
135// variables will change:
136int buttonState = 0; // variable for reading the pushbutton status
137
138extern void blink(void);
139
140void 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
149int interrupt;
150/* ToDo */
151void
152blink(void){
153// Serial.println("interrupt!");
154 syslog(LOG_NOTICE, "data = %d", interrupt++);
155}
156
157void 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
177const int ledPin = 13;
178
179void setup() {
180}
181
182void 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 */
226int sensorPin = A0; // select the input pin for the potentiometer
227int ledPin = 13; // select the pin for the LED
228int sensorValue = 0; // variable to store the value coming from the sensor
229
230void setup() {
231 // declare the ledPin as an OUTPUT:
232 pinMode(ledPin, OUTPUT);
233}
234
235int i = 0;
236void 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.
251This mode is more conveniently because you use processor for other tasks and when alarm match occurs interrupt routine is executed.
252In this way, alarm flag checking is indipendent from main program flow.
253******************************************************************************************************************************************************************************/
254
255#include <RTCInt.h>
256
257RTCInt rtc;
258
259extern void alarm_int(void);
260
261void 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
275void 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 ******************************/
288void 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 */
Note: See TracBrowser for help on using the repository browser.