arduino small experiment
It just happened that I used arduino to learn something recently, so I made the usual small experiment into an experiment, and friends who are interested can take a look.
1. Function realization
1. The mercury switch realizes the function of breathing light. When the mercury switch is closed, the light will go from "off->l brightness increase->brightness is kept for a maximum of 10ms and then off, and continuously circulates", when the mercury switch is turned on, the light will be off.
Diagram and introduction of mercury switch used
The following is the implementation procedure
Read whether the value of SensorPin has changed through the variable zhi. If it is 0 (that is, the mercury collision switch is triggered), enter the for loop and execute the corresponding command, otherwise it remains off.
int led=3; int SensorPin=2; int t; void setup() { pinMode(led, OUTPUT); pinMode(SensorPin, INPUT); } void loop() { int zhi=digitalRead(SensorPin); if(zhi==0){ for(int n=255;n>0;n--) { analogWrite(led,n); // Common anode connection, when pin 9 is high, the light is off delay(10); } } else{ analogWrite(led,255); } }
The hardware wiring diagram is as follows
2. Write some bank safe programs:
When the safe is collided (that is, after the mercury switch is triggered), the LED light keeps flashing, and the buzzer continues to alarm without interruption, the mercury switch fails, and the alarm cannot be stopped.
Buzzer icon and introduction
The following is the bug explanation (just kidding, the program is available)
The buzz function is well-defined. If you don't change it, you can use it without changing it. You can use it directly when you call it. The pro test is very loud. If you want to modify it, you can change the values of x and y.
To keep the mercury collision switch inactive after being triggered, I implemented it by using if judgment and for loop. When the mercury switch is triggered, the alarm and lights will enter the loop, and the mercury switch will be disabled before jumping out of the loop. (Theoretically, as long as you If there are enough loops, it will not come out. Of course, if you really want to use it, this place has to be modified).
The procedure is as follows
int Led=4;//Define Lamp Pins int SensorPin=2;//Shock sensor connection pins int buzzpin=3; int zhi; void buzz(int x,int y) { for(int i=0;i<x;i++) { digitalWrite(buzzpin, HIGH); delayMicroseconds(y); digitalWrite(buzzpin, LOW); delayMicroseconds(y); } } void setup() { pinMode(2,INPUT); pinMode(4,OUTPUT); pinMode(buzzpin, OUTPUT); buzz(200,500); buzz(400,250); } void loop() { int zhi=digitalRead(SensorPin); if(zhi==LOW) { for(int i=0;i<1000;i++) { digitalWrite(Led,LOW); delay(10); digitalWrite(Led,HIGH); delay(10); buzz(200,500); buzz(400,250); } } else { digitalWrite(Led,HIGH); digitalWrite(buzzpin,LOW); } }
The hardware wiring diagram is as follows
3. Complete safe function
When the safe is collided (that is, after the mercury switch is triggered), the LED light keeps flashing, and the buzzer continues to alarm without interruption, the mercury switch fails, and the alarm cannot be stopped. The end alarm function is realized through the terminal button, and the mercury switch is touched to continue to enter the alarm, which can be repeated repeatedly
The difference from the above program is that a button is added, so I embedded an if judgment in the for loop. If the button is pressed, the value of the for loop variable overflows, the for loop is jumped out, the alarm light and the alarm sound all stop and continue. Judging the status of the mercury switch, repeat the cycle, the procedure is as follows
int Led=4;//Define Lamp Pins int SensorPin=2;//Shock sensor connection pins int buzzpin=3; int zhi; int ting; int key=5; void buzz(int x,int y) { //buzzer for(int i=0;i<x;i++) { digitalWrite(buzzpin, HIGH); delayMicroseconds(y); digitalWrite(buzzpin, LOW); delayMicroseconds(y); } } void setup() //initialization function { pinMode(SensorPin,INPUT); pinMode(Led,OUTPUT); pinMode(key, INPUT); pinMode(buzzpin, OUTPUT); buzz(200,500); buzz(400,250); } void loop() //main program { int zhi=digitalRead(SensorPin); if(zhi==LOW) { for(int i=0;i<1000;i++) { digitalWrite(Led,LOW); delay(10); digitalWrite(Led,HIGH); delay(10); buzz(200,500); buzz(400,250); int ting=digitalRead(key); if(ting==LOW){ i=1000; } } } else { digitalWrite(Led,HIGH); digitalWrite(buzzpin,LOW); } }
The wiring diagram is as follows
Fourth, the following is a video demonstration
55059fce3ac7c39c86a5de51cb24f91a
4. Summary
The above are some study notes I made on the arduino simple safe. There is still a lot of room for optimization in the program. In the later stage, sensors can be added to realize more functions and make it more perfect.
The BUG written here is over, thank you for watching.