Hello everyone
since a little while ago I'm in the arduino world,
but you do not know me because I make my topics in the Spanish section.
but for some time I have not found a solution to my problem in the Spanish forum so I try in English hoping someone can help me
I have this code that works me very well controlling a DC motor
/*
The code is written by Farshid Jafari Harandi,
(C) Spekel, Spekel.se y modificado por cuchara con ayuda del foro arduino
*/
int Revers =5; // boton de cambi de direccion
int ledPin = 13; // LED connected to digital pin 13
int pwmPin = 11; // PWM Motor driver (/D2 pin pin on MC338870 motor board)
int motorPin1 = 7; // Motor pin 1 (IN1 on MC338870 motor board)
int motorPin2 = 8; // Motor pin 2 (IN2 on MC338870 motor board)
int pot = 0; // Potentiometer pin on analog input 0
//Leave D1 and /FS and /FB unconnceted
//Connect the EN to +5V on Arduino board
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
int val;
// The setup() method runs once, when the sketch starts
void setup() {
pinMode(Revers, INPUT);
pinMode(ledPin, OUTPUT); // initialize the digital pin as an output:
pinMode(pwmPin, OUTPUT); // initialize the digital pin as an output:
pinMode(motorPin1, OUTPUT); // initialize the digital pin as an output:
pinMode(motorPin2, OUTPUT); // initialize the digital pin as an output:
digitalWrite(Revers,HIGH);
Serial.begin(9600);
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
reading = digitalRead(Revers);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
digitalWrite(ledPin, state);
previous = reading;
val = analogRead(pot);
val = map(val,0,1023,-255,255);
Serial.println(val);
if (state){
if (val >= 0)
{
digitalWrite(ledPin,HIGH);
digitalWrite(motorPin1,LOW);
digitalWrite(motorPin2,HIGH);
analogWrite(pwmPin,val);
}
else
{
val *= -1;
digitalWrite(ledPin,LOW);
digitalWrite(motorPin1,HIGH);
digitalWrite(motorPin2,LOW);
analogWrite(pwmPin,val);
}
}
if (!state) {
if (val >= 0) {
digitalWrite(ledPin,HIGH);
digitalWrite(motorPin1,HIGH);
digitalWrite(motorPin2,LOW);
analogWrite(pwmPin,val);
}
else {
val *= -1;
digitalWrite(ledPin,LOW);
digitalWrite(motorPin1,LOW);
digitalWrite(motorPin2,HIGH);
analogWrite(pwmPin,val);
}
}
delay(100);
}
here is a demonstration video:
I connect it like that
What I am looking for and I do not get is to get the same result by controlling a stepper motor nema17
I tried many modifications to the code but I do not think it will be useful because nothing happens
I hope you can help me
Thanks