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
// Code create by mark aruda for Mecanical Design Lab 1 at Umass Lowell | |
// Code moves 2 servos, the first moves based on the potentiometer value, | |
//the second servo moves between horizontal and vertical based on a pushbutton | |
#include <Servo.h> //includes servo library | |
Servo servo1; //servo control object | |
Servo servo2; | |
const int buttonPin = 2; //labels button pin | |
const int potpin = A3; //labels analog read pin for potentiometer | |
int potposition; //creates value for potientiometer value | |
int servo1position; //creates variable for servo position | |
boolean currentState = LOW; | |
boolean lastState = LOW; | |
boolean stateChange = false; | |
int currentButton = 0;// used for button count | |
int lastButton = 2; | |
// setup | |
void setup() { | |
servo1.attach(9); //servo1 attached pin 9 | |
servo2.attach(10); //servo2 attached pin 10 | |
pinMode(buttonPin, INPUT); //makes the button pin an input | |
} | |
// main loop | |
void loop(){ | |
potposition= analogRead(potpin); //reads the potentiometer value and gives a value between 0-1023 | |
servo1position= map(potposition,0,1023,0,180); //maps the value from the potentiometer so it can work for a servo range | |
servo1.write(servo1position); //writes the servo to that value | |
currentState = debounceButton(); | |
stateChange = checkForChange(currentState, lastState); | |
currentButton = getButtonNumber(lastButton, currentState, stateChange); | |
indicatorLight(currentButton); | |
lastState = currentState; | |
lastButton = 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 > 2){ | |
button = 0; | |
} | |
Serial.println(button); | |
} | |
return button; | |
} | |
// function indicatorLight | |
void indicatorLight(int button) //function that acts according to the button number that the get button number function gives out | |
{ | |
if (button==0){ //if the button is 0 it makes the servo go to 90 degrees | |
servo2.write(90); | |
} | |
if (button == 1){ | |
//do nothing | |
} | |
if (button ==2) { // if the button number is 2 it makes the servo go to 180 degrees | |
servo2.write(0); | |
} | |
} | |
//things to add in the future lcd screen and indicator lights |
https://gist.github.com/maruda95/7552561
No comments:
Post a Comment