Thursday, November 28, 2013

Modified Code

         Today I worked on the code that I uploaded yesterday.  There are still a few things that I would like to fix such as not allowing the user to press the same button sequentially.   I would like to add this feature because I don't want the carriage to be told to move in a direction when it's already at the edge.  Otherwise the carriage will be pushing against the wall.  
          The code I wrote today is the following:

#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);
}
view raw gistfile1.txt hosted with ❤ by GitHub
Here is a video of the code working.  At the beginning i'm showing that the servo is attached to pin 6 and I lay the deodorant stick down and press the buttons.  This is what the cold will be doing to the carriage the only difference is that the carriage is on a track and will go straight.   The second part of the video is me showing the servo is attached to pin 9 and how using a potentiometer one can raise and lower the cup.

-Mike Law                                                                                                                                



No comments:

Post a Comment