Introduction
This tutorial is part of the Narcan first aid-kit project . When the driver got notified that there is an emergency, the driver will hit a button and open the Narcan first aid-kit. This tutorial will demonstrate how to open the Narcan first aid-kit with a buttom.
Parts List
- Jumper wires
- Push button
- Servo
The Circuit
- Servo has three wires attached to it: brown, red and yellow wire. Connect the brown wire to Ground(GND); connect the red wire to Pin 5V; connect the yellow wire to Pin 9
- Connect the push button to Ground(GND) and Pin 2
- Glue the servo to the container. Glue a plastic belt to the servo the the lid of the container.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
#include <Servo.h> Servo myservo; // create servo object to control a servo #define MIN_ANGLE 53 #define MAX_ANGLE 98 #define SPEED 15 #define OPEN_value 15 float OnOffvalue = 0; float get_button_state(); const int buttonPin = 2; // the number of the pushbutton pin void setup(){ Serial.begin(9600); digitalWrite(buttonPin, HIGH); OnOffvalue = 20; myservo.attach(9); // attaches the servo on pin 9 to the servo object myservo.write(MIN_ANGLE); } char flag_open = 0; void loop() { float state = get_button_state(); if(state < OPEN_value && flag_open == 0 ){ flag_open = 1; for(int i = MIN_ANGLE; i < MAX_ANGLE; i++){ myservo.write(i); delay(SPEED); } }else if( state > OPEN_value && flag_open == 1 ){ flag_open = 0; for(int i = MAX_ANGLE; i > MIN_ANGLE ; i--){ myservo.write(i); delay(SPEED); } } Serial.print("STATE:"); Serial.print(state); delay(20); } float get_button_state(){ if (digitalRead(buttonPin) == LOW) { delay(500); if(digitalRead(buttonPin) == LOW){ if(OnOffvalue == 20){OnOffvalue = 0;} else OnOffvalue = 20; while( digitalRead(buttonPin) == LOW ); } } return OnOffvalue; } |