Hello! I'm pretty new to Arduino, and I've been going through a tutorial series on it. I decided to try to branch off, however, and try my hand at a small project. Unfortunately, I haven't been completely successful.
Here's the idea: I want to make a joystick controlled DC motor with a toggle action, so if I push the joystick either all the way up, all the way to the side, or at a 45 degree angle positively, the motor will operate at 255 - full power. At the equilibrium joystick position, the motor should be at half speed, and by pushing the joystick, the motor will turn on and off.
Here's the hardware: The L293D motor controller and a 3-6V DC motor from the Elegoo "The Most Complete Starter Kit."
Here's the reasoning to my most recent approach: I hooked up everything using an external power supply and the L293D as well as the motor. My reasoning was to assign a "switchposold" and read a "switchposnew" and if the switchposold was 0 and the switchpos new was 1, that means the button is pushed. If the button is pushed, then set the motorstate to 0 if it's 1 and 1 if it's 0. Then I could use if statements to manipulate the motorspeed with the joystick given that the motorstate is 1. I've worked out an equation to get motorspeed from the joystick.
Here's my code;
int speedpin = 5;//this is pin 1 on the L293D
int dirpin1 = 4;//this is pin 2 on the L293D
int dirpin2 = 3;//this is pin 7 on the L293D
int motorspeed;//variable to control the speed
int xpin = A0;//the pin reading the x value of the joystick
int ypin = A1;//the pin reading the y value of the joystick
int xpos;// this is the x position variable
int ypos;//this is the y position variable
int switchposnew;//the value of the switch that is being read
int switchposold = 1; // the stagnant value of the button
int switchpin = 2;//the pin that the pushbutton of the joystick is connected to
int motorstate;// a number 1 or 0 to tell if the motor is on or off
void setup() {
// put your setup code here, to run once:
pinMode(speedpin, OUTPUT);
pinMode(dirpin1, OUTPUT);
pinMode(dirpin2, OUTPUT);
Serial.begin(2000000);
pinMode(xpin, INPUT);
pinMode(ypin, INPUT);
pinMode(switchpin, INPUT);
digitalWrite(switchpin, HIGH);//So that an external pullup resistor isn't necessary
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(dirpin1, LOW);
digitalWrite(dirpin2, HIGH);
xpos = 1023.- analogRead(xpin);//makes the joystick better for my preferences
ypos = analogRead(ypin);
switchposnew = digitalRead(switchpin);
if(switchposold == 0 && switchposnew == 1){//Basically, if the button has been released from being pushed
if(motorstate==0){
motorstate = 1;
}
else{
motorstate =0;
}
}// these commands simplify the motor into being on or off depending on the toggle of the pushbutton switch
if (motorstate == 0){
motorspeed = 0;
}
switchposnew = digitalRead(switchpin);//reads the state of the switch for the next loop
while(motorstate == 1 && switchposnew !=0){//as long as the motor is spinning and the button isn't pushed, you can controll the speed with the following equation
motorspeed = (255./1023.)*(xpos+ypos) - 127.7;//control's the speed of the motor using the joystick
switchposnew = digitalRead(switchpin);// updates the position of the button
}
if (motorspeed<0){
motorspeed = 0;//prevents the motor speed from going negative
}
if (motorspeed>255){
motorspeed = 255;//prevents the motor speed from going higher than full
}
switchposold = switchposnew;//refreshes the old switch position so the toggle function works.
Serial.println(motorspeed);
}
The motor isn't working as I would like. I haven't told the motor to spin up in the code, yet, but the motorstate doesn't cleanly alternate between 0 and 1 when I push the switch.
Does anyone have any advice for this project or the way I'm going about it?
Thanks!!!