Gear to be fabricated, with adjusted thickness and material volume. |
The Arduino code was also updated so that the LCD screen displays "RELOAD" when the servo motor is in its initial position. In this position, rubber bands can be loaded onto the mechanism, so that they can be fired as the servo motor changes positions. The code was also adjusted so that after it is uploaded, the servo motor moves directly to its initial position. Here is the code:
This file contains 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
/* | |
Arduino code - Code is used to: | |
* control servo motor with push button | |
* display LED at all times | |
* display LCD at servo's initial position | |
*/ | |
// introduce servo motor and LCD | |
#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; | |
// setup button, sevo, LED, and LCD | |
void setup() { | |
pinMode(buttonPin, INPUT); | |
Serial.begin(9600); | |
Servo1.attach(9); | |
while (pos==lastButton);{ | |
pinMode(led, OUTPUT); | |
lcd.begin(16, 2); | |
lcd.clear(); | |
} | |
} | |
// main loop | |
// call functions and keep LED on at all times | |
void loop(){ | |
currentState = debounceButton(); | |
stateChange = checkForChange(currentState, lastState); | |
currentButton = getButtonNumber(lastButton, currentState, stateChange); | |
servoControl(currentButton); | |
lastState = currentState; | |
lastButton = currentButton; | |
digitalWrite(led, HIGH); | |
printfunction(currentButton); | |
} | |
// 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; | |
} | |
// function servoControl | |
// base servo position off of button state | |
// make sure that first position is intended initial position | |
void servoControl(int button) | |
{ | |
if(button == 4) {Servo1.write(10);} | |
if(button == 0) {Servo1.write(30);} | |
if(button == 1) {Servo1.write(50);} | |
if(button == 2) {Servo1.write(70);} | |
if(button == 3) {Servo1.write(90);} | |
} | |
// function printfunction | |
// base LCD display off of button state | |
void printfunction (int button) | |
{ | |
if(button == 4) { | |
lcd.setCursor(0,0); | |
lcd.print ("!!!! RELOAD !!!!!"); | |
} | |
else{lcd.clear(); | |
} | |
} |
No comments:
Post a Comment