Set a variable 0 or 1 with an IF just once at the beginning

Hi,

I want to set one variable called “salida1” with a 0 or a 1 depending on the start value of another variable called “Limit01”. I tried to use the following code to change the variable “salida1” to 1 if “Limit01” is in HIGH position at the beginning.

  • I’m using a button to move a stepper up and down, the problem is that I have a limit switch and the bottom and IF the program runs in this position, pressing this limit switch (HIGH), I want my starting value of “salida1” = 1.

#include <AccelStepper.h>

AccelStepper stepper(1, 9, 8);

float recorridoSubida = 135.0;
float pasosPorVuelta = 1600.0;
float pasoUsillo = 1.0;
float recorridoMax = 140.0;

float pos1 = (pasosPorVuelta * recorridoMax) * pasoUsillo;
float pos2 = - (pasosPorVuelta * recorridoSubida) * pasoUsillo;

int estado1 = 0;
int estadoAnterior1 = 0;
int salida1 = 0;

#define Limit01 2 // Creamos el fin de carrera conectado en el pin 2

void setup() {

pinMode(10, INPUT);

pinMode(Limit01, INPUT);
digitalWrite(Limit01, LOW);

if (digitalRead(Limit01) == HIGH) { **
** digitalWrite(salida1,HIGH);

** }**

Serial.print(digitalRead(Limit01));
Serial.print("\t");
Serial.print(salida1);
Serial.print("\t");

stepper.setMaxSpeed(32000);
stepper.setSpeed(16000);
stepper.setAcceleration(1000);

}
void loop() {

float posicionActual = stepper.currentPosition();

estado1 = digitalRead(10);

if ((estado1 == HIGH) && (estadoAnterior1 == LOW)) {
salida1 = 1 - salida1;
delay(1000);
}

estadoAnterior1 = estado1;

if (salida1 == 1) {
if (digitalRead(Limit01) == LOW) {
stepper.moveTo(pos1);
stepper.run();

}

else {
stepper.setCurrentPosition(pos1);

}
}
if ((salida1 == 0) && (digitalRead(Limit01) == HIGH)){
stepper.move(pos2);
stepper.run();
}
else{
stepper.run();
}
}

Hi,

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

What pin do you have the limit switch connected to?

Tom… :slight_smile:

Hi,

I want to set one variable called “salida1” with a 0 or a 1 depending on the start value of another variable called “Limit01”. I tried to use the following code to change the variable “salida1” to 1 if “Limit01” is in HIGH position at the beginning.

  • I’m using a button to move a stepper up and down, the problem is that I have a limit switch and the bottom and IF the program runs in this position, pressing this limit switch (HIGH), I want my starting value of “salida1” = 1.

  • I have My limit switch connected to digitalpin 2, and it is called Limit01

#include <AccelStepper.h>

AccelStepper stepper(1, 9, 8); 

float recorridoSubida = 135.0; 
float pasosPorVuelta = 1600.0; 
float pasoUsillo = 1.0;
float recorridoMax = 140.0; 

float pos1 = (pasosPorVuelta * recorridoMax) * pasoUsillo; 
float pos2 = - (pasosPorVuelta * recorridoSubida) * pasoUsillo; 

int estado1 = 0; 
int estadoAnterior1 = 0;
int salida1 = 0;

#define Limit01 2  // Creamos el fin de carrera conectado en el pin 2

 
void setup() {
   
  
   pinMode(10, INPUT);  
  
   pinMode(Limit01, INPUT);  
   digitalWrite(Limit01, LOW);

   
   if (digitalRead(Limit01) == HIGH) {        
         digitalWrite(salida1,HIGH); 
   }
    
   Serial.print(digitalRead(Limit01));
   Serial.print("\t");
   Serial.print(salida1);
   Serial.print("\t");

  stepper.setMaxSpeed(32000); 
  stepper.setSpeed(16000); 
  stepper.setAcceleration(1000); 
 
}
void loop() {
  
  float posicionActual = stepper.currentPosition();

  

  estado1 = digitalRead(10);
 
  
  if ((estado1 == HIGH) && (estadoAnterior1 == LOW)) {
    salida1 = 1 - salida1;
    delay(1000);
  }

  estadoAnterior1 = estado1;

   
  if (salida1 == 1) {  
    if (digitalRead(Limit01) == LOW) {  
        stepper.moveTo(pos1);  
        stepper.run();  
          
       }  

    else {  
          stepper.setCurrentPosition(pos1); 
          
    }
  }
  if ((salida1 == 0) && (digitalRead(Limit01) == HIGH)){   
         stepper.move(pos2); 
         stepper.run();  
    }   
  else{      
         stepper.run();
  }
}

Hi,

 pinMode(Limit01, INPUT); 
   digitalWrite(Limit01, LOW);

Why do you digitalWrite LOW to the input?

How have you got the limit wired?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom… :slight_smile:

Hi,

Here you have my skematich, I dont use the same driver, the driver Im using is 2H Microstep driver DQ20MA, because of that I just connect two pins the direction and the step.

The digitalWrite to an INPUT was an error.

Thank you,

Hi,
OPs Fritzy
c7a0e2cc5ff27379022831de4eee7721141a15ae.png

Tom… :o

Hi, Do you have a DMM? Check the voltages around the edges of the protoboard, some boards have the long side conductors open circuit halfway along the length.

What are the spec/data on your stepper, I hope you are not using the UNO to provide current to it?

Tom.... :)

Hi Tom,

I am new in arduino and I dont know how to make the skematich with this program.

My stepper is already moving, my problem is with the code, as I told you in the first post I want to know how to includ a if in the setupvoid. I want to know how to torn my estado1 to 1 if the limit switch is HIGH at the beggining of the program.

Thank you

Hi, What does your monitor show ?

Try it with the limit switch in each OFF and ON position.

Tom... :)