Problem with coding for push button to executing programs

Hi guys, let’s say, hypothetically speaking the hard-wiring is done correctly and everything is where its suppose to be connected. I am using an Arduino Uno with a nema-17 stepper motor and a L298N motor controller. I am currently trying to use 3 push buttons so that when I program it and hit each button, it will go a certain distance and come back to the starting point. I was wondering based on my program, would it potentially work? If not, what did I do wrong? Thanks for your time.

Here’s my program:

#include <Stepper.h>

int stepsPerRevolution1 = 7500;  
int stepsPerRevolution2 = 4150; 
int stepsPerRevolution3 = 1800;


Stepper myStepper1(stepsPerRevolution1, 8, 9, 10, 11);
Stepper myStepper2(stepsPerRevolution2, 8, 9, 10, 11);
Stepper myStepper3(stepsPerRevolution3, 8, 9, 10, 11);

void setup() {
  
    myStepper1.setSpeed(10);
    myStepper2.setSpeed(18);
    myStepper3.setSpeed(40);
    int a = 0;
    int b = 0;
    int c = 0;
    Serial.begin(9600);
}

void loop() {
  int a = analogRead(A0);
  if (a==1){
    Serial.println("clockwise1");
  myStepper1.step(stepsPerRevolution1);
  delay(500);

  Serial.println("counterclockwise1");
  myStepper1.step(-stepsPerRevolution1);
  delay(500);
  }
  else if (a==0){
    myStepper1.step(stepsPerRevolution1);
  }
   int b = analogRead(A1);
  if (b==1){
    Serial.println("clockwise2");
  myStepper2.step(stepsPerRevolution2);
  delay(500);

  Serial.println("counterclockwise2");
  myStepper2.step(-stepsPerRevolution2);
  delay(500);
  }
  else if (b==0){
    myStepper2.step(stepsPerRevolution2);
  }
   int c = analogRead(A2);
  if (c==1){
    Serial.println("clockwise3");
  myStepper3.step(stepsPerRevolution3);
  delay(500);

  Serial.println("counterclockwise3");
  myStepper3.step(-stepsPerRevolution3);
  delay(500);
  }
  else if (c==0){
    myStepper3.step(stepsPerRevolution3);
  }
}
    int a = 0;
    int b = 0;
    int c = 0;

Not sure what that's doing there.
Have you read about the concept of scope in C programming?

  int a = analogRead(A0);
  if (a==1)

That’s a fairly unusual construct. The effects of noise mean that it is unlikely that analogRead will return 1 consistently.

AWOL:

    int a = 0;

int b = 0;
    int c = 0;


Not sure what that's doing there.
Have you read about the concept of scope in C programming?

I am completely new to programming so since I didn't know where to start for this, I looked on youtube on ways to start my program and I found a video of a sample code and they had this in their code for their program and it worked so I assumed these were the declaration of each button's state to be off or 0. But now that you pointed it out and gave me direction and did some research, I suppose those meant nothing.

AWOL:

  int a = analogRead(A0);

if (a==1)


That's a fairly unusual construct. The effects of noise mean that it is unlikely that analogRead will return 1 consistently.

As for this part, I'm not quite sure what you mean the "effects of noise"?

OK, let's simplify; why are you using analogRead to read switches, which are, I hope you'll agree, essentially digital devices?

zi984:
As for this part, I’m not quite sure what you mean the “effects of noise”?

void setup()
{
Serial.begin(9600);
}

void loop()
{
Serial.println(A0);
delay(500);
}

Hook A0 to the 3.3V of the uno.
How consistent are the readings?

AWOL:
OK, let's simplify; why are you using analogRead to read switches, which are, I hope you'll agree, essentially digital devices?

I do agree but the video which I molded my code from used analogRead and he got his code to work and when I tried to use it for myself, the code worked for a bit but then it stopped working, and now it doesnt work at all, except the opposite effect is happening in which, the programs are running consistently executing but when I hit each push buttons, it would stop the programs. I don't suppose I could post the youtube link here and you can see what I'm talking about?

sterretje:

void setup()

{
Serial.begin(9600);
}

void loop()
{
Serial.println(A0);
delay(500);
}



Hook A0 to the 3.3V of the uno.
How consistent are the readings?

I don't have my project with me as it is at my friend's house and it's 1:12AM here, so I will have to get back to you tomorrow on this.

I don't particularly want to watch an unrelated YouTube video, I'd rather you recognised the pitfalls of what you're trying to do, and how to make it work.

Start with analogRead - what range of values does it return?

AWOL:
I don't particularly want to watch an unrelated YouTube video, I'd rather you recognised the pitfalls of what you're trying to do, and how to make it work.

Start with analogRead - what range of values does it return?

I'm not sure what you mean by that, I don't see any values returning unless I'm looking at the wrong place?

Sorry, the line in loop () should have been

Serial.println(analogRead(A0));

OK,, back another step.
analogRead returns values in the range 0 to 1023, depending on the voltage applied to the analogue input pin.
If you are testing to see if that value is 1, what does that tell you about the applied voltage?
Is that greatly different to zero?

Zi984,

Maybe you need to step back even further… make sure you understand the difference between analogRead and digitalRead.

sterretje:
Sorry, the line in loop () should have been

Serial.println(analogRead(A0));

Oh okay...now I'm starting to see what I did wrong...

AWOL:
OK,, back another step.
analogRead returns values in the range 0 to 1023, depending on the voltage applied to the analogue input pin.
If you are testing to see if that value is 1, what does that tell you about the applied voltage?
Is that greatly different to zero?

Oh okay, after some more research I'm starting to see what I did wrong, for some reason I was under the impression that analogRead was just a command so that the board knows which analog pin I was using for my push button and for whatever was next that it'll execute the program through that specific pin. But after looking it up, analogRead is only a reading (I'm sorry for the incompetency as I was thrown this part of my project to program with very little time and I have zero knowledge on it so I appreciate the patience you're giving me). So safe to say the program I came up with was complete waste.

patduino:
Zi984,

Maybe you need to step back even further... make sure you understand the difference between analogRead and digitalRead.

Yep, just did the research so now I know. Sorry.

Hi zi984,

Before you throw it all away (as complete waste), look into digitalRead. It might actually be what you wanted in the first place. But instead of comparing to 1 and 0, compare it to HIGH and LOW.

patduino:
But instead of comparing to 1 and 0, compare it to HIGH and LOW.

...which a simply posh names for the same values.

true... or should I say 1 :slight_smile:

...but I try to champion good programming habits such as readability, modularity, and type-awareness, etc. whenever I can.

Pat.

patduino:
true... or should I say 1 :slight_smile:

No, you should say !false

Ha. Nobody else gets computer / logic / engineering jokes. My wife and family just look at me like...

... well, like an engineer.

patduino:
Hi zi984,

Before you throw it all away (as complete waste), look into digitalRead. It might actually be what you wanted in the first place. But instead of comparing to 1 and 0, compare it to HIGH and LOW.

Okay, I will try digitalRead, and will update with my code when I get it done, thanks all for the help!

Okay so I updated my code and here it is. Would it hypothetically work if all my wiring is done correctly? I molded this code off of the Arduino push button tutorial. Thanks.

#include <Stepper.h>

int button1 = 2;
int button2 = 3;
int button3 = 4;
int val1 = 0;
int val2 = 0;
int val3 = 0;
int stepsPerRevolution1 = 900; 
int stepsPerRevolution2 = 2150;
int stepsPerRevolution3 = 5000;

Stepper myStepper1(stepsPerRevolution1, 8, 9, 10, 11);
Stepper myStepper2(stepsPerRevolution2, 8, 9, 10, 11);
Stepper myStepper3(stepsPerRevolution3, 8, 9, 10, 11);

  void setup() {
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(stepsPerRevolution1, OUTPUT);
  pinMode(stepsPerRevolution2, OUTPUT);
  pinMode(stepsPerRevolution3, OUTPUT);
}

void loop() {
  val1 = digitalRead(button1);
  if (val1 == HIGH)
  {
  digitalWrite(stepsPerRevolution1, HIGH);
    }else{
      digitalWrite(stepsPerRevolution1, LOW);
    }
   val2 = digitalRead(button2);
   if (val2 == HIGH)
  {
  digitalWrite(stepsPerRevolution2, HIGH);
  }else{
    digitalWrite(stepsPerRevolution2, LOW);
  }
  val3 = digitalRead(button3);
  if (val3 == HIGH)
  {
    digitalWrite(stepsPerRevolution3, HIGH);
  }else{
    digitalWrite(stepsPerRevolution3, LOW);
  }
}