Visible to members of this folder
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13; //第13個腳位請改成12,因為第13個腳位為測試用腳位可能會有訊號不穩定的狀態
// the setup routine runs once when you press reset:
void setup() { //setcup內的程式是執行一次之後即完成設定不需要以loop的方式重覆執行
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == 1) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
int ledPin = 9; // LED connected to digital pin 9
// nothing happens in setup
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
//fadeValue = fadeValue + 5;
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
//將第9個腳位當成類比輸出PWM~
// wait for 30 milliseconds to see the dimming effect
delay(30);
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
int potPin = 3; // select the input pin for the potentiometer
int ledPin = 9; // select the pin for the LED
//這個部分請注意要如何使用Serial Monitor
Serial.begin(9600);
int sensorValue = analogRead(potPin);
Serial.println(sensorValue, DEC);
//sensorValue = sensorValue/4;
sensorValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(ledPin, sensorValue);
delay(150);
#define led_pin 11
#define flex_pin A0
void setup()
{
Serial.begin(115200);
pinMode(led_pin, OUTPUT);
void loop()
int flex_value = analogRead(flex_pin);
int led_value = map(flex_value, 0, 1023, 0, 255);
led_value = led_value * 2;
analogWrite(led_pin, led_value);
Serial.println(flex_value);
Serial.println(led_value);
Serial.println("-------------");
delay(500);
int inByte = 0;
int inByte_state = 0;
// initialize serial communication:
// initialize the LED pins:
for (int thisPin = 2; thisPin < 7; thisPin++) {
pinMode(thisPin, OUTPUT);
// read the sensor:
//if (Serial.available() > 0) {
//int inByte = Serial.read();
if(inByte_state == 0){
inByte = 2;
if(inByte_state == 1){
inByte = 0;
switch (inByte) {
case 0:
digitalWrite(2, HIGH);
delay(200);
inByte_state = 0;
break;
case 1:
digitalWrite(3, HIGH);
case 2:
digitalWrite(2, LOW);
inByte_state = 1;
case 3:
digitalWrite(3, LOW);
// default:
// turn all the LEDs off:
// for (int thisPin = 2; thisPin < 7; thisPin++) {
// digitalWrite(thisPin, LOW);
//}
int brightness = 0;
void loop() { // read the sensor:
if (Serial.available() > 0) {
int inByte = Serial.read();
case 'a':
// if (brightness+20 <= 255) {
// brightness = brightness+20;
// analogWrite(3, brightness);
// Serial.println(inByte);
// } else {
// inByte = 98;
// }
case 'b':
analogWrite(3, 0);
Serial.println(inByte);
case 'c':
analogWrite(3, 100);
case 'd':
analogWrite(3, 150);
case 'e':
analogWrite(3, 200);
// Serial.println("hi");
#include <Keyboard.h>
int key_enable = 1;
pinMode( 2, INPUT_PULLUP);
Keyboard.begin();
if (digitalRead(2) == LOW) {
key_enable = 0;
if (key_enable == 0) {
Keyboard.print("TEST");
key_enable = 1;
int buttonPin_1 = 3;
int ledPin_1 = 10;
int buttonPin_2 = 4;
int ledPin_2 = 11;
int pinRead_1;
int pinRead_2;
int range;
int brightness;
int brightness1;
int brightness2;
int delayDuration=30;
pinMode(ledPin_1, OUTPUT);
pinMode(buttonPin_1, INPUT);
pinMode(ledPin_2, OUTPUT);
pinMode(buttonPin_2, INPUT);
pinRead_1 = digitalRead(buttonPin_1);
pinRead_2 = digitalRead(buttonPin_2);
if(pinRead_1==0 && pinRead_2==1){
range=0;
if(pinRead_1==1 && pinRead_2==0){
range=1;
if(pinRead_1==1 && pinRead_2==1){
range=2;
if(pinRead_1==0 && pinRead_2==0){
range=3;
switch (range) {
//switch case的做法是讓程式去選擇底下要執行的區塊,區塊的內容為case到break之間
case 0:
analogWrite(ledPin_1,brightness1);
brightness1 = brightness1 - 5 ;
if (brightness1 <= 0) {
brightness1 = 0 ;
delay(delayDuration);
analogWrite(ledPin_2,brightness2);
brightness2 = brightness2 + 5 ;
if (brightness2 >= 255) {
brightness2 = 255 ;
case 1:
brightness1 = brightness1 + 5 ;
if (brightness1 >= 255) {
brightness1 = 255 ;
brightness2 = brightness2 - 5 ;
if (brightness2 <= 0) {
brightness2 = 0 ;
if (brightness2 != 0 ){
analogWrite(ledPin_1,brightness1=0);
else {
analogWrite(ledPin_1,brightness1=255);
analogWrite(ledPin_1,brightness);
analogWrite(ledPin_2,brightness);
brightness = brightness - 5 ;
if (brightness <= 0) {
brightness = 0 ;
short ledU=3,ledD=5,ledM=11,GSenserU=7,GSenserD=8;
short MAX_BRI=170;
boolean b,gu,gd;
int bri;
pinMode(ledU, OUTPUT);
pinMode(ledD, OUTPUT);
pinMode(GSenserU,INPUT);
pinMode(GSenserD,INPUT);
digitalWrite(ledD, HIGH);
gu = digitalRead(GSenserU);
gd = digitalRead(GSenserD);
if(gu && !gd){
bri+=2;
ledPwm();
else if(!gu && gd){
bri-=2;
delay(6);
void ledPwm(){
if(bri>MAX_BRI) bri=MAX_BRI;
if(bri<0) bri=0;
if(bri!=MAX_BRI && bri!=0 && bri%3==0)
analogWrite(ledM, 30);
else
digitalWrite(ledM, LOW);
analogWrite(ledU, bri);
analogWrite(ledD, MAX_BRI-bri);
// the setup function runs once when you press reset or power the board
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
// the loop function runs over and over again forever
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
// create servo object to control a servo
#include <Servo.h>
Servo myservo_01;
Servo myservo_02;
int potpin_01 = 0;
int potpin_02 = 1;
// analog pin used to connect the potentiometer
int val_01;
int val_02;
// variable to read the value from the analog pin
myservo_01.attach(9);
myservo_02.attach(10);
// attaches the servo on pin 9 to the servo object
val_01 = analogRead(potpin_01);
val_02 = analogRead(potpin_02);
// reads the value of the potentiometer (value between 0 and 1023)
val_01 = map(val, 0, 1023, 0, 179);
val_02 = map(val, 0, 1023, 0, 179);
//map 前面兩組數字到後面兩組範圍內
// scale it to use it with the servo (value between 0 and 180)
myservo_01.write(val_01);
myservo_02.write(val_02);
// sets the servo position according to the scaled value
delay(15);
// waits for the servo to get there
課程軟體包
課堂習作(1):LED燈
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13; //第13個腳位請改成12,因為第13個腳位為測試用腳位可能會有訊號不穩定的狀態
// the setup routine runs once when you press reset:
void setup() { //setcup內的程式是執行一次之後即完成設定不需要以loop的方式重覆執行
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
課堂習作(2):開關
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == 1) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
課堂習作(3):可調式電阻 呼吸燈
int ledPin = 9; // LED connected to digital pin 9
void setup() {
// nothing happens in setup
}
void loop() {
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
//fadeValue = fadeValue + 5;
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
//將第9個腳位當成類比輸出PWM~
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
int potPin = 3; // select the input pin for the potentiometer
int ledPin = 9; // select the pin for the LED
//這個部分請注意要如何使用Serial Monitor
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(potPin);
Serial.println(sensorValue, DEC);
//sensorValue = sensorValue/4;
sensorValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(ledPin, sensorValue);
delay(150);
}
彎曲感測器
#define led_pin 11
#define flex_pin A0
void setup()
{
Serial.begin(115200);
pinMode(led_pin, OUTPUT);
}
void loop()
{
int flex_value = analogRead(flex_pin);
int led_value = map(flex_value, 0, 1023, 0, 255);
led_value = led_value * 2;
analogWrite(led_pin, led_value);
Serial.println(flex_value);
Serial.println(led_value);
Serial.println("-------------");
delay(500);
}
課堂習作(4):switch case
int inByte = 0;
int inByte_state = 0;
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pins:
for (int thisPin = 2; thisPin < 7; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// read the sensor:
//if (Serial.available() > 0) {
//int inByte = Serial.read();
if(inByte_state == 0){
inByte = 2;
}
if(inByte_state == 1){
inByte = 0;
}
switch (inByte) {
case 0:
digitalWrite(2, HIGH);
delay(200);
inByte_state = 0;
break;
case 1:
digitalWrite(3, HIGH);
break;
case 2:
digitalWrite(2, LOW);
delay(200);
inByte_state = 1;
break;
case 3:
digitalWrite(3, LOW);
break;
// default:
// turn all the LEDs off:
// for (int thisPin = 2; thisPin < 7; thisPin++) {
// digitalWrite(thisPin, LOW);
//}
//}
}
}
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pins:
for (int thisPin = 2; thisPin < 7; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
int brightness = 0;
void loop() { // read the sensor:
if (Serial.available() > 0) {
int inByte = Serial.read();
switch (inByte) {
case 'a':
// if (brightness+20 <= 255) {
// brightness = brightness+20;
// analogWrite(3, brightness);
// Serial.println(inByte);
// } else {
// inByte = 98;
// }
break;
case 'b':
analogWrite(3, 0);
Serial.println(inByte);
break;
case 'c':
analogWrite(3, 100);
Serial.println(inByte);
break;
case 'd':
analogWrite(3, 150);
Serial.println(inByte);
break;
case 'e':
analogWrite(3, 200);
Serial.println(inByte);
break;
// default:
// turn all the LEDs off:
// for (int thisPin = 2; thisPin < 7; thisPin++) {
// digitalWrite(thisPin, LOW);
// }
// Serial.println("hi");
}
}
}
#include <Keyboard.h>
int key_enable = 1;
void setup() {
pinMode( 2, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
if (digitalRead(2) == LOW) {
key_enable = 0;
} else {
if (key_enable == 0) {
Keyboard.print("TEST");
key_enable = 1;
}
}
}
課堂習作(5):倒水燈
int buttonPin_1 = 3;
int ledPin_1 = 10;
int buttonPin_2 = 4;
int ledPin_2 = 11;
int pinRead_1;
int pinRead_2;
int range;
int brightness;
int brightness1;
int brightness2;
int delayDuration=30;
void setup() {
pinMode(ledPin_1, OUTPUT);
pinMode(buttonPin_1, INPUT);
pinMode(ledPin_2, OUTPUT);
pinMode(buttonPin_2, INPUT);
}
void loop() {
pinRead_1 = digitalRead(buttonPin_1);
pinRead_2 = digitalRead(buttonPin_2);
if(pinRead_1==0 && pinRead_2==1){
range=0;
}
if(pinRead_1==1 && pinRead_2==0){
range=1;
}
if(pinRead_1==1 && pinRead_2==1){
range=2;
}
if(pinRead_1==0 && pinRead_2==0){
range=3;
}
switch (range) {
//switch case的做法是讓程式去選擇底下要執行的區塊,區塊的內容為case到break之間
case 0:
analogWrite(ledPin_1,brightness1);
brightness1 = brightness1 - 5 ;
if (brightness1 <= 0) {
brightness1 = 0 ;
}
delay(delayDuration);
analogWrite(ledPin_2,brightness2);
brightness2 = brightness2 + 5 ;
if (brightness2 >= 255) {
brightness2 = 255 ;
}
delay(delayDuration);
break;
case 1:
analogWrite(ledPin_1,brightness1);
brightness1 = brightness1 + 5 ;
if (brightness1 >= 255) {
brightness1 = 255 ;
}
delay(delayDuration);
analogWrite(ledPin_2,brightness2);
brightness2 = brightness2 - 5 ;
if (brightness2 <= 0) {
brightness2 = 0 ;
}
delay(delayDuration);
break;
case 2:
if (brightness2 != 0 ){
analogWrite(ledPin_1,brightness1=0);
}
else {
analogWrite(ledPin_1,brightness1=255);
}
break;
case 3:
analogWrite(ledPin_1,brightness);
analogWrite(ledPin_2,brightness);
brightness = brightness - 5 ;
if (brightness <= 0) {
brightness = 0 ;
}
delay(delayDuration);
break;
}
}
short ledU=3,ledD=5,ledM=11,GSenserU=7,GSenserD=8;
short MAX_BRI=170;
boolean b,gu,gd;
int bri;
void setup() {
pinMode(ledU, OUTPUT);
pinMode(ledD, OUTPUT);
pinMode(GSenserU,INPUT);
pinMode(GSenserD,INPUT);
digitalWrite(ledD, HIGH);
}
void loop() {
gu = digitalRead(GSenserU);
gd = digitalRead(GSenserD);
if(gu && !gd){
bri+=2;
ledPwm();
}
else if(!gu && gd){
bri-=2;
ledPwm();
}
delay(6);
}
void ledPwm(){
if(bri>MAX_BRI) bri=MAX_BRI;
if(bri<0) bri=0;
if(bri!=MAX_BRI && bri!=0 && bri%3==0)
analogWrite(ledM, 30);
else
digitalWrite(ledM, LOW);
analogWrite(ledU, bri);
analogWrite(ledD, MAX_BRI-bri);
}
課堂習作(6):繼電器應用
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
課堂習作(7):伺服馬達
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
// create servo object to control a servo
#include <Servo.h>
Servo myservo_01;
Servo myservo_02;
int potpin_01 = 0;
int potpin_02 = 1;
// analog pin used to connect the potentiometer
int val_01;
int val_02;
// variable to read the value from the analog pin
void setup() {
myservo_01.attach(9);
myservo_02.attach(10);
// attaches the servo on pin 9 to the servo object
}
void loop()
{
val_01 = analogRead(potpin_01);
val_02 = analogRead(potpin_02);
// reads the value of the potentiometer (value between 0 and 1023)
val_01 = map(val, 0, 1023, 0, 179);
val_02 = map(val, 0, 1023, 0, 179);
//map 前面兩組數字到後面兩組範圍內
// scale it to use it with the servo (value between 0 and 180)
myservo_01.write(val_01);
myservo_02.write(val_02);
// sets the servo position according to the scaled value
delay(15);
// waits for the servo to get there
}