Week 7: Discussions for the Final Design
For this week, we gathered for our group meeting before getting separated into our Full Scales Team. Dr. Furman and Ron explained to us how our second presentation was coming up in two weeks. After the brief meeting, we dispersed into our sub-teams and began discussing about our future designs. Cheuk and I discussed about our progress with our initial designs and how we could improve upon it. After being successfully able to control the the direction and speed of the DC motor using a joystick, we expanded further on how to improve it. We talked about how we could get the joystick to be wireless so that it would be more user-friendly when operating the podcar for application purposes. For the rest of the class period, we researched the different forms of wireless communications and what would be the most probable and beneficial for us to implement. Posted below is the code for controlling the DC motor using a joystick:
-----------------------------------------------------
//Set pin numbers:
const byte joyStickPin = A1;
const byte motorSpeedPin = 5;
const byte motorDirPin = 4;
//variables
//Joystick input variables
int joyValue = 0;
int joyValueMax = 1023;
int joyValueMin = 0;
int joyValueMid = 512;
int joyValueMidUpper = joyValueMid + 20;
int joyValueMidLower = joyValueMid - 20;
//DC motor variables
byte motorSpeed = 0;
byte motorSpeedMax = 255;
byte motorSpeedMin = 90; //set to smallest value that make motor move (default 0)
// DC motor that I use start to move at 90 pwm value
void setup()
{
pinMode(joyStickPin, INPUT);
pinMode(motorSpeedPin, OUTPUT);
pinMode(motorDirPin, OUTPUT);
}
void loop()
{
joyValue = analogRead(joyStickPin);
if(joyValue > joyValueMidUpper) //Forward
{
motorSpeed = map(joyValue, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
MotorForward(motorSpeed);
}
else if(joyValue < joyValueMidLower) //Backward
{
motorSpeed = map(joyValue, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
MotorBackward(motorSpeed);
}
//joyValue Between joyValueMidLower - joyValueMidUpper.
//Need some range here, because joystick sometime not in exact center.
else
{
MotorStop();
}
}
void MotorForward( byte Spd)
{
digitalWrite(motorDirPin, HIGH);
analogWrite(motorSpeedPin, Spd);
}
void MotorBackward( byte Spd)
{
digitalWrite(motorDirPin, LOW);
analogWrite(motorSpeedPin, Spd);
}
void MotorStop()
{
analogWrite(motorSpeedPin, 0);
}
----------------------------------------------------
Comments
Post a Comment