Help with code, just checking if it works

  /* 
(Joystick)
left  pin A1
right pin A0
up    pin A3
down  pin A2

(Left Motor)
pin 13 
pin 12
pin 11
pin 10

*/

int leftpin = A1;
int rightpin = A0;
int uppin = A3;
int downpin = A2;

int leftvalue = 0;
int rightvalue = 0;
int upvalue = 0;
int downvalue = 0;

#include <Stepper.h>
const int stepsPerRevolution = 150;
Stepper leftStepper(stepsPerRevolution, 13, 12, 11, 10);

void setup() {
pinMode(leftpin,INPUT_PULLUP);
pinMode(rightpin,INPUT_PULLUP);
pinMode(uppin,INPUT_PULLUP);
pinMode(downpin,INPUT_PULLUP);

leftStepper.setSpeed(120);

Serial.begin(9600);
}
void setStepperIdle() { 
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);

}

void loop() {
leftvalue = digitalRead(leftpin);
rightvalue = digitalRead(rightpin);
upvalue = digitalRead(uppin);
downvalue = digitalRead(downpin);


if(leftvalue == LOW){
  leftStepper.step(-stepsPerRevolution);
  setStepperIdle();
  Serial.print("left");
  delay(10);}
if(rightvalue == LOW){
  leftStepper.step(stepsPerRevolution);
  setStepperIdle();
  Serial.print("right");
  delay(10);}
if(upvalue == LOW){
  leftStepper.step(stepsPerRevolution);
  setStepperIdle();
  Serial.print("up");
  delay(10);}
if(downvalue == LOW){
  leftStepper.step(-stepsPerRevolution);
  setStepperIdle();
  Serial.print("down");
  delay(10);}


}


I am currently building a motorized wheelchair, this is the code for the left motor, there is also one for the right motor (can upload if u guys can help with that too), Could someone please check this code and see if it is compatible with arduino uno rev3, l298 boards, and stepper motors? thank you

It looks plausible.

You could build it in the wokwi simulator

and see for yourself.

If you are really aiming for a wheel chair control system, such a sketch might need to be somewhat more elaborate. Just sayin'.

Why do you have a delay(10) after each revolution?

What is the gearing for the wheels? Is moving in increments of one revolution at a time sufficient control over the motion?

Are steppers the right choice of motor type?

a7

Do you understand what each line of the code that you have written does and have you planned for failure circumstances?

A motorized wheelchair could be an extremely dangerous thing if not properly coded and wired, like if it couldn't be stopped, or accelerated suddenly without input.

I am something of a mad scientist and I don't normally ask these things but vehicles are different.

For example, do you understand if your motor drivers will fail closed or open, or understand their various failure modes? Is there any plan for acceleration limiting and in what way? Is there anything to prevent it driving off a step, like a height detector at the deck? etc?

Looking forward to the first test ride.

I didn't write the code, I sourced it from a project (element 14 episode 550), for anyone wondering, so I don't really understand any of it. It goes very slow so stopping isnt an issue, and steppers are the only motor I have (also, what does delay10 mean?) thank you for your help, ill keep you guys posted on testing progress

I'm sorry, but we're in a bit of a bad situation. We signed up for a science fair, it is due in 3 days and we have almost no experiences with this, we built it off a guide and thought things would be easy to do if we followed the instructions. That is why I am asking such questions.

OK if you can post a link to that, it may be possible to focus your learning enough to succeed.

What is the purpose of the project, that is to say what were you expected to bring to this, and how much about what are you expected to add to your skillz?

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.