Here are some examples that we decided to base our idea off of.
![]() |
Rubber band gun example 1 http://www.pirate4x4.com/forum/general-chit-chat/845049-show-me-your-rubber-band-guns.html |
![]() |
Rubber band gun Example 2 http://www.doobybrain.com/2008/03/08/classic-rubber-band-launcher-on-dealextreme/ |
We wired up the Arduino Uno board with a servo motor, push button, LCD screen, and LED. We made the preliminary code featured below, but we plan to edit it so that the LCD screen, and possibly the LED, perform more advanced tasks. The push button will cause the servo motor to slightly rotate, which will in turn, rotate the gear causing the release of a rubber band. So far, the LCD screen and LED have constant displays, only to indicate that the mechanism is working properly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Code used to control servo motor with push button | |
and display LCD and LED at all times | |
*/ | |
#include <Servo.h> | |
#include <LiquidCrystal.h> | |
Servo Servo1; | |
LiquidCrystal lcd(12,11,5,4,3,2); | |
// global variables | |
const int buttonPin = 8; | |
boolean currentState = LOW; | |
boolean lastState = LOW; | |
boolean stateChange = false; | |
int currentButton = 0; | |
int lastButton = 4; | |
int pos = 0; | |
int led = 13; | |
// LED and LCD constant display | |
// setup | |
void setup() { | |
pinMode(buttonPin, INPUT); | |
Serial.begin(9600); | |
Servo1.attach(6); | |
while (pos == lastButton);{ | |
pinMode(led, OUTPUT); | |
lcd.begin(16, 2); | |
lcd.clear(); | |
lcd.print("RBG"); | |
} | |
} | |
// set up push button | |
// main loop | |
void loop(){ | |
currentState = debounceButton(); | |
stateChange = checkForChange(currentState, lastState); | |
currentButton = getButtonNumber(lastButton, currentState, stateChange); | |
servoControl(currentButton); | |
lastState = currentState; | |
lastButton = currentButton; | |
digitalWrite(led, HIGH); | |
} | |
// function debounceButton | |
boolean debounceButton() | |
{ | |
boolean firstCheck = LOW; | |
boolean secondCheck = LOW; | |
boolean current = LOW; | |
firstCheck = digitalRead(buttonPin); | |
delay(50); | |
secondCheck = digitalRead(buttonPin); | |
if (firstCheck == secondCheck){ | |
current = firstCheck; | |
} | |
return current; | |
} | |
// function checkForChange | |
boolean checkForChange(boolean current, boolean last) | |
{ | |
boolean change; | |
if (current != last){ | |
change = true; | |
} | |
else { | |
change = false; | |
} | |
return change; | |
} | |
// function getButtonNumber | |
int getButtonNumber(int button, boolean state, boolean change) | |
{ | |
if (change == true && state == LOW){ | |
button++; | |
if (button > 4){ | |
button = 0; | |
} | |
Serial.println(button); | |
} | |
return button; | |
} | |
// Base servo position off of button state | |
// function servoControl | |
void servoControl(int button) | |
{ | |
if(button == 0) {Servo1.write(10);} | |
if(button == 1) {Servo1.write(30);} | |
if(button == 2) {Servo1.write(50);} | |
if(button == 3) {Servo1.write(70);} | |
if(button == 4) {Servo1.write(90);} | |
} |
The the push button brings the servo motor through five different positions. The LED remains on and the LCD constantly displays "RBG" for Rubber Band Gunners. Here is a video of the current code in action:
No comments:
Post a Comment