Page 1 of 1

Nanpy- using raspberry pi as the master and arduino as slave

Posted: Thu Mar 26, 2020 7:50 pm
by ejm2107
As a part of my project I have to control my arduino using raspberry pi. The arduino controls the stepper motors. All my wiring is right but project is not working. I am a newbie to raspberry pi. Would u mind helping out where the code might have gone wrong.

Code: Select all

import urllib
from nanpy import(ArduinoApi, SerialManager)
from time import sleep
dirPin1=3
stepsPin1=4
steps=40
dirPin2=10
stepsPin2=11
try:
    connection = SerialManager()
    a=ArduinoApi(connection = connection)
except:
    print("Failure")
a.pinMode(dirPin1,a.OUTPUT)
a.pinMode(stepsPin1,a.OUTPUT)
a.pinMode(stepsPin2,a.OUTPUT)
a.pinMode(dirPin2,a.OUTPUT)
try:
          a.digitalWrite(dirPin1,a.HIGH)
          a.digitalWrite(dirPin2, a.HIGH)
          z=0
          while (z<steps):
             a.digitalWrite(stepsPin1, a.HIGH)
             a.digitalWrite(stepsPin2, a.HIGH)
             sleep(2)
             a.digitalWrite(stepsPin1, a.LOW)
             a.digitalWrite(stepsPin2, a.LOW)
             sleep(2)
             z=z+1
          sleep(1)
          a.digitalWrite(dirPin1,a.LOW);
          a.digitalWrite(dirPin2,a.LOW);
          z=0
          while (z<steps):
             a.digitalWrite(stepsPin1, a.HIGH)
             a.digitalWrite(stepsPin2, a.HIGH)
             sleep(2)
             a.digitalWrite(stepsPin1, a.LOW)
             a.digitalWrite(stepsPin2, a.LOW)
             sleep(2)
             z=z+1
          sleep(1)
except:
        a.digitalWrite(stepsPin1, a.LOW)
        a.digitalWrite(stepsPin2, a.LOW)