I wish you are in a good health at first. I wrote a code that commanded a motor stepper with Arduino, via Serial Monitor. The code in Arduino works very well, but I want to write a program that is interconnected with serial communication with Arduino.
To do this, I have chosen Python programming language. The program that I wrote in the python contains two buttons labeled Forward and Backward, and a entrybox where the user gives the number of rotations of the stepper motor. My problem is that the program is not sending the number of rotations that the user gives to the stepper motor. So if I write number 10 in the entrybox, and then if I press the Forward button, the motor stepper is not moving.
To be clear about the work I have done, this is the code I have written in Arduino:
void setup()
{
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available()>0)
{
serialData = Serial.read();
if(serialData == '1')
{
i = Serial.parseInt();
digitalWrite(dirPin, HIGH);
for(x = 0; x <= i*200; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
}else if(serialData == '0')
{
p = Serial.parseInt();
digitalWrite(dirPin, LOW);
for(x = 0; x <= p*200; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
}
}
}
While this is the code I wrote on python:
import serial
from tkinter import *
from tkinter import ttk
arduinoData = serial.Serial('com3', 9600)
def Forward(enans):
print(enans)
arduinoData.write(str.encode('1''enans'))
def Backward():
arduinoData.write(str.encode('0''10'))
window = Tk()
window.title("Controlling of the Stepper motor")
en = Entry(window)
en.grid(row = 0, column = 2)
enans = en.get()
try:
int(enans)
except ValueError:
pass
btn1 = Button(window, text = "Forward", command = Forward(enans))
btn1.grid(row = 0, column = 0)
btn2 = Button(window, text = "Backward", command = Backward)
btn2.grid(row = 1, column = 0)
window.mainloop()