The code I wrote today is the following:
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
#include <Servo.h> | |
Servo cup; | |
Servo cart; | |
int potpin = 0; | |
int val; | |
int inPinR = 4; | |
int inPinL = 3; | |
int ButtonR = 0; | |
int ButtonL = 0; | |
void setup() | |
{ | |
cup.attach(9); | |
cart.attach(6); | |
pinMode(inPinR, INPUT); | |
pinMode(inPinL, INPUT); | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
ButtonR = digitalRead(inPinR); | |
ButtonL = digitalRead(inPinL); | |
if ((ButtonR == LOW)&&(ButtonL == HIGH)) | |
{ cart.write(0); // if the left button is pressed the cart will move | |
delay(2100);} // to the left for 2100 ms | |
if ((ButtonL == LOW) && (ButtonR == HIGH)) | |
{ cart.write(180); // if the right button is pressed the cart will | |
delay(2100);} // move to the left for 2100 ms | |
if ((ButtonL == LOW) && (ButtonR ==LOW)) | |
{ cart.write(94);} // cart wont run if both buttons pressed | |
if ((ButtonL == HIGH) && (ButtonR == HIGH)) | |
{ cart.write(94);} // cart wont run if neiter button is pressed | |
val = analogRead(potpin); | |
val = map(val, 0, 1023, 0, 179); | |
if (val <= 70) // if the scaled pot value is less than or | |
// equal to 70 the servo cup will assigned the value 0 | |
{ | |
cup.write(0); // when assigned 0 the cup goes down | |
} | |
if ((val >70) && (val < 110)) // if the value is between 71 and | |
// 109 the cup will be assigned the value 94 | |
// 90 should stop the motor however it would continue to go down | |
// at a very slow rate | |
{ | |
cup.write(94); // cup stops | |
} | |
if (val >= 110) // if the value is greater than or equal to 110 | |
// the servo cup will be set to 180 | |
{ | |
cup.write(180); | |
} | |
Serial.print(val); | |
Serial.print("\n"); | |
delay(15); | |
} |
-Mike Law