# 消毒機 程式撰寫 ###### tags: `程式組教程` ## 電磁閥 ![](https://i.imgur.com/N0tCjtp.png) We are starting with the BareMinimum Sketch found in the IDE, it should look something like this: ```cpp= void setup() { // put your setup code here, to run once. } void loop() { // put your main code here, to run repeatedly. } ``` So first we will need a variable for the Arduino pin: ```cpp= int solenoidPin = 4; //This is the output pin on the Arduino we are using void setup() { // put your setup code here, to run once. } void loop() { // put your main code here, to run repeatedly. } ``` Next we need to set the Arduino pin to act as an output: ```cpp= int solenoidPin = 4; //This is the output pin on the Arduino we are using void setup() { // put your setup code here, to run once. pinMode(solenoidPin, OUTPUT); //Sets the pin as an output } void loop() { // put your main code here, to run repeatedly. } ``` Now that it is set as an output we can tell it what to do: ```cpp= int solenoidPin = 4; //This is the output pin on the Arduino we are using void setup() { // put your setup code here, to run once. pinMode(solenoidPin, OUTPUT); //Sets the pin as an output } void loop() { // put your main code here, to run repeatedly. digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON delay(1000); //Wait 1 Second digitalWrite(solenoidPin, LOW); //Switch Solenoid OFF delay(1000); //Wait 1 Second } ``` So if we want the solenoid to allow water to flow, set the pin high. When you want the water to stop flowing, set the pin low. In this case it will turn the water on for 1 second and then off for 1 second, looping forever (or at least until it is unplugged!) This solenoid valve could easily be used with the flow meter featured in our last tutorial to create a system that only allows a certain volume of water to flow before shutting off. ## 幫浦 [Bump sensor](https://forum.arduino.cc/t/bump-sensor/493166) ```cpp= const byte switchPin = 10; byte oldSwitchState = HIGH; // initial condition void setup () { Serial.begin (9600); pinMode (switchPin, INPUT); } // end of setup void loop () { // see if switch is open or closed byte switchState = digitalRead (switchPin); // has it changed since last time? if (switchState != oldSwitchState) { oldSwitchState = switchState; // remember for next time if (switchState == LOW) { Serial.println ("Switch closed."); } // end if switchState is LOW else { Serial.println ("Switch opened."); } // end if switchState is HIGH } // end of state change } ``` 網路上查到的造霧機arduino程式(可以參考) ```cpp= /* Demo code for grove atomization. Touch to start atomizing. Last modified by he by xiaohe */ // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin 13 as an output. pinMode(A5, OUTPUT);// Set A5 as OUTPUT pinMode(5, INPUT); // Use digital pin 5 as output port } // the loop function runs over and over again forever void loop() { int D2Sig = digitalRead(5);// read pin 5 signal if (D2Sig == 1) { /* code */ digitalWrite(A5, HIGH); // atomize delay(10000); // wait for 10 seconds digitalWrite(A5, LOW); // atomization stopped } } ``` ```cpp= /* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-controls-pump */ // constants won't change const int RELAY_PIN = A5; // the Arduino pin, which connects to the IN pin of relay // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin A5 as an output. pinMode(RELAY_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(RELAY_PIN, HIGH); // turn on pump 5 seconds delay(5000); digitalWrite(RELAY_PIN, LOW); // turn off pump 5 seconds delay(5000); } ``` ## 繼電器 [[Arduino 範例] 繼電器(Relay)的使用](https://blog.jmaker.com.tw/arduino-relay/ ) 據我所知好像沒有程式?? (仁祥 ```cpp= void setup() { // put your setup code here, to run once: pinMode(11,OUTPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(11,HIGH); delay(500); digitalWrite(11,LOW); } ``` ## 微動開關 ```cpp= const int buttonPin = 10; // the number of the pushbutton pin const int ledPin = 12; // 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 == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); } } ``` [Arduino 按壓開關與微動開關](/K2D_p--PRgW5B7SsmXAFRA) ## 馬達 (motor, encoder) [Talon Speed Controller](https://forum.arduino.cc/t/talon-speed-controller/146210) ```cpp= #include <Servo.h> void setup(){ } void loop() { servo.write(180); delay(1500); servo.write(0); delay(1500); } ``` [Control a Talon motor using PWM](https://mrmctavish.wordpress.com/2020/03/15/using-an-arduino-to-control-a-talon-or-spark-motor-controller-using-pwm/) ```cpp= #include <PWM.h> //use pin 11 on the Mega instead, otherwise there is a frequency cap at 31 Hz int motor = 9; // the pin that the LED is attached to int motorspeed = 1; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by int32_t frequency = 1000; //frequency (in Hz) bool success; void setup() { //initialize all timers except for 0, to save time keeping functions InitTimersSafe(); //sets the frequency for the specified pin success = SetPinFrequencySafe(motor, frequency); //if the pin frequency was set successfully, turn pin 13 on if(success) { pinMode(13, OUTPUT); digitalWrite(13, HIGH); } } void loop() { //use this functions instead of analogWrite on 'initialized' pins digitalWrite(13, HIGH); success = SetPinFrequencySafe(motor, 1000); pwmWrite(motor, motorspeed); delay(5000); digitalWrite(13, LOW); motorspeed=HIGH; pwmWrite(motor,motorspeed); delay(5000); success = SetPinFrequencySafe(motor, 1500); motorspeed = LOW; pwmWrite(motor, motorspeed); delay(5000); motorspeed=HIGH; pwmWrite(motor,motorspeed); delay(5000); success = SetPinFrequencySafe(motor, 2000); motorspeed = LOW; pwmWrite(motor, motorspeed); delay(5000); motorspeed=HIGH; pwmWrite(motor,motorspeed); delay(5000); } ``` [Knob](https://www.arduino.cc/en/Tutorial/Knob) ```cpp= #include <Servo.h> Servo myservo; // create servo object to control a servo int potpin = 0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180) myservo.write(val); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there } ``` ## 燈條 ## 紅外線 [紅外線](https://hackmd.io/@yqvHTthmTYehjxz4k9Hqrw/HJDggCjyt) ```cpp= int IR_SENSOR = 0; // 類比讀取腳設為A0 int intSensorResult = 0; //偵測結果 float fltSensorCalc = 0; //計算距離 void setup(){ Serial.begin(9600); //設定和電腦的連接去將結果顯示在序列埠監控視窗(Serial Monitor) } void loop(){ //從IR sensor讀取傳感值 intSensorResult = analogRead(IR_SENSOR); //從類比讀腳(A0)獲取傳感值 fltSensorCalc = (6787.0 / (intSensorResult - 3.0)) - 4.0; //以公分為單位計算距離 Serial.print(fltSensorCalc); //傳送距離到電腦中 Serial.println("cm"); //將"cm"加到顯示結果中 delay(200); //延遲0.2秒 } ``` ## 超音波 ```cpp= // ---------------------------------------------------------------- // // Arduino Ultrasoninc Sensor HC-SR04 // Re-writed by Arbi Abdul Jabbaar // Using Arduino IDE 1.8.7 // Using HC-SR04 Module // Tested on 17 September 2019 // ---------------------------------------------------------------- // #define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04 #define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04 // defines variables long duration; // variable for the duration of sound wave travel int distance; // variable for the distance measurement void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor Serial.println("with Arduino UNO R3"); } void loop() { // Clears the trigPin condition digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin HIGH (ACTIVE) for 10 microseconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back) // Displays the distance on the Serial Monitor Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm"); } ``` ```cpp= int trigPin = 發射端腳位; int echoPin = 接收端腳位; void setup(){ Serial.begin (9600); //這東東不一定要加,要在電腦上看到數據要加 pinMode(trigPin,OUTPUT); //宣告發射端為輸出 pinMode(echoPin,INPUT); //宣告接收端為輸入 } void loop(){ digitalWrite(trigPin, LOW); //先將發射端斷電,訊號清空 delayMicroseconds(5); //讓訊號完整清空 digitalWrite(trigPin, HIGH); //將發射端通電,射出訊號 delayMicroseconds(10); //讓上面動作完整執行,為啥是10秒,這是網路上的數據 digitalWrite(trigPin, LOW); //將發射端斷電 duration = pulseIn(echoPin, HIGH); //讀取接收端的數據 cm = (duration/2) / 29.4; //從時間轉換成距離 // 1000000 microseconds / 340 * 100 = 29.4 microseconds(氣溫15度時) //上面這些就是測距的程式 ```