Arduino Serial.Print not working when I add while loop

I have commented out a lot of the code trying to isolate what's going on. The serial.print works until I add in the while loops in setup to home my stepper motors.

I just want to see the voltage at pin A4 Why would this be and how can I fix it? Thanks!

//CONSTANTS THAT DONT CHANGE.  ASSIGNING PIN NUMBERS
const int STEPPER01_DIR = A2;
const int STEPPER01_STEP = A3;
const int LANE1LED = A4;
const int LANE2LED = A5 ;
const int STEPPER02_DIR = 3;
const int STEPPER02_STEP = 4;
const int LIMITSWITCH1 = 5;
const int LIMITSWITCH2 = 6;
const int RELAY_RED = 7;
const int RELAY_GREEN = 8;
const int RELAY_SHREDDER = 9;
const int BUTTON_SHRED = 10;
const int BUTTON_RESET = 11;


//VARIABLES THAT WILL CHANGE.  SET UP DIGITAL INPUTS
int buttonstate_shred = 0;
int buttonstate_reset = 0;
int buttonstate_limit1 = 0;
int buttonstate_limit2 = 0;
int steps1;
int steps2;

//starting accelstepper
#include <AccelStepper.h>;
AccelStepper stepper1(1,STEPPER01_STEP,STEPPER01_DIR);
AccelStepper stepper2(1,STEPPER02_STEP,STEPPER02_DIR);

void setup() {
// put your setup code here, to run once:

//STATING WHAT EACH PIN DOES (INPUT OR OUTPUT)
pinMode(STEPPER01_DIR,OUTPUT);
pinMode(STEPPER01_STEP,OUTPUT);
pinMode(STEPPER02_DIR,OUTPUT);
pinMode(STEPPER02_STEP,OUTPUT);
pinMode(LIMITSWITCH1,INPUT_PULLUP);
pinMode(LIMITSWITCH2,INPUT_PULLUP);
pinMode(RELAY_RED,OUTPUT);
pinMode(RELAY_GREEN,OUTPUT);
pinMode(RELAY_SHREDDER,OUTPUT);
pinMode(BUTTON_SHRED,INPUT_PULLUP);
pinMode(BUTTON_RESET,INPUT_PULLUP);
//pinMode(LANE1LED,INPUT);
//pinMode(LANE2LED,INPUT);

stepper1.setMaxSpeed(750);
stepper2.setMaxSpeed(750);
stepper1.setAcceleration(1000);
stepper2.setAcceleration(1000);
stepper1.setCurrentPosition(0);
stepper2.setCurrentPosition(0);

  Serial.begin(9600);

//STEPPER HOMING
//stepper 1
while (digitalRead(LIMITSWITCH1))
{digitalWrite(STEPPER01_DIR, HIGH);
digitalWrite(STEPPER01_STEP, HIGH);
delay(5);
digitalWrite(STEPPER01_STEP, LOW);
delay(5);}

while (!digitalRead(LIMITSWITCH1))
{digitalWrite(STEPPER01_DIR, LOW);
digitalWrite(STEPPER01_STEP, HIGH);
delay(10);
digitalWrite(STEPPER01_STEP, LOW);
delay(10); }

steps1=0;

//stepper 2
while (digitalRead(LIMITSWITCH2))
{digitalWrite(STEPPER02_DIR, HIGH);
digitalWrite(STEPPER02_STEP, HIGH);
delay(5);
digitalWrite(STEPPER02_STEP, LOW);
delay(5);}

while (!digitalRead(LIMITSWITCH2))
{digitalWrite(STEPPER02_DIR, LOW);
digitalWrite(STEPPER02_STEP, HIGH);
delay(10);
digitalWrite(STEPPER02_STEP, LOW);
delay(10); }

steps2=0;


}
void loop() {
  // put your main code here, to run repeatedly:

//digitalWrite(RELAY_GREEN, HIGH);

//stepper tester
//stepper1.moveTo(400);
//stepper1.run();
//stepper2.moveTo(400);
//stepper2.run();

//INSERT CODE THAT DECIDES WHO WON HERE

  int sensorValue = analogRead(LANE1LED);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage);

//int LANE1 = analogRead (LANE1LED);
//Serial.println(LANE1);
//int LANE2 = analogRead (LANE2LED);
//Serial.println(LANE2);

//if lane one wins
//delay(1000);  

//digitalWrite(RELAY_GREEN, LOW);
//digitalWrite(RELAY_RED, HIGH);

//stepper1.moveTo(4000);
//stepper1.run();

////digitalWrite(RELAY_SHREDDER, HIGH);

//stepper1.moveTo(50);
//stepper1.run();


//turns on the shredder when you press the shredder button
//buttonstate_shred=digitalRead(BUTTON_SHRED);
//if (buttonstate_shred == LOW){
//digitalWrite(RELAY_SHREDDER, HIGH);}
//else{
//digitalWrite(RELAY_SHREDDER, LOW);}

}

In setup() there are 4 whiles:

while (digitalRead(LIMITSWITCH1)) {
while (!digitalRead(LIMITSWITCH1)) {
while (digitalRead(LIMITSWITCH2)) {
while (!digitalRead(LIMITSWITCH2)) {

If LIMITSWITCH1 is HIGH, the code stays at 1.o while
and if it is LOW it stays in the 2nd while.

To go through all whiles and print something,
it needs:
LIMITSWITCH1 LOW, then LIMITSWITCH2 LOW.

See the simulation here:

" Limits - Wokwi ESP32, STM32, Arduino Simulator

@thoughtbomb try @ruilviana's simulation. If you stab around on the buttons, you'll get through your logic maze, which seems to be slamming into the limit switch then backing off a bit, two times.

If you can't get your real version to do what you can do in the simulation, then worry if your switches are being read. Correctly or at all.

Some Serial.print()ing strewn about your setup() and in the while bodies might turn up that your switches aren't acting like you think.

You could also write a teeny sketch that echoed the switches on LEDs, even do one at a time and use the build in LED at 13 (on an UNO, what board are you using?).

HTH

a7

does the "homing" code you added work correctly?
does each motor rotate toward the limit switch, stop and take one step back?

add prints while homing

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