neteng
Posts: 24
Joined: Wed May 11, 2016 3:56 am

Moving a Motor

Wed Oct 12, 2016 2:17 am

Below is a code that moves my motor through connection to the arduino. My arduino is serially connected to the pi for which transmission of data is handled through the nanpy software....my motor drivers are connected to my arduino...

Code: Select all

from nanpy import (ArduinoApi, SerialManager)
from time import sleep

directionPin1 = 38
directionPin2 = 40
pwmPin = 5
speedVal = 100
stopVal = 0

try:
    connection = SeriaManager()
    a = ArduinoApi(connection = connection)
except:
    print¨Failed to connect to arduino¨

#setup the pinmodes as if we were in ARDUINO IDE
a.pinMode(directionPin1, a.OUTPUT)
a.pinMode(directionPin2,a.OUTPUT)
a.pinMode(pwmPin,a.OUTPUT)

try:
    a.digitalWrite(directionPin1, a.LOW)
    a.digitalWrite(directionPin2, a.HIGH)
    a.analogWrite(pwmPin, speedVal)
    sleep(2)

    a.analogWrite(pwmPin, stopVal)
    sleep(2)

    a.digitalWrite(directionPin1, a.HIGH)
    a.digitalWrite(directionPin2, a.LOW)
    a.analogWrite(pwmPin, speedVal)
    sleep(2)

    a.analogWrite(pwmPin, stopVal)
    sleep(2)

except KeyboardInterrupt:
 
When you use the nanpy firmware, its as if you are wrting on the arduino ide....with a ArduinoApi connection prefix attached in front of the arduino syntaxes....

My question is such that how will i write this code to be a function so that i can call it or execute it when a particular parameter of my main code changes....

Thanks,
neteng

theenggprojects
Posts: 9
Joined: Thu Nov 19, 2015 2:13 am
Contact: Website

Re: Moving a Motor

Wed Oct 12, 2016 4:08 am

Have you solved this issue. I am working on a similar project and is unable to do the communication between arduino and raspberry Pi 3 so if someone knows the solution please share it here. I am using the Visual Studio to get the data from Arduino and I am following this tutorial How to use Serial Port in VB2010.

You should also check The Engineering Projects because they have posted a lot of Arduino Projects and PIC Microcontroller Projects and have also designed many new Proteus Tutorials and C# Tutorial and MATLAB Projects and LabView Projects and Engineering Projects . I hope you guys are gonna like them.
Last edited by theenggprojects on Tue Jan 10, 2017 12:08 am, edited 1 time in total.

123mike
Posts: 1
Joined: Fri Oct 28, 2016 9:12 am

Re: Moving a Motor

Fri Oct 28, 2016 9:25 am

This is a site that help me a lot when trying to use nanny: https://autottblog.wordpress.com/raspbe ... ith-nanpy/. The project is quite advanced so I am guessing you will find you answer it you look through there code (or perhaps just there website). The link to there code from here: https://autottblog.wordpress.com/programming-the-car/.

Hope this helps.

Return to “Beginners”