KL46Z code to use in conjunction with a PyQt GUI over a serial communication.

Dependencies:   mbed

Here is the Python code for the PyQt Gui...

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
import serial
  
def printNumber(number):
    print number
    ser = serial.Serial('COM5',9600)   
    #state which port the KL46Z is connected to and the baud rate

    ser.write(unichr(number).encode()) 
    #encode number from slider and send the encoded character to KL46Z
  
if __name__=="__main__":
    app=QApplication(sys.argv)
    slider=QSlider(Qt.Horizontal)
    slider.setRange(1,127)
    slider.setFixedWidth(400)
    slider.setSliderPosition(64)
    #create a QApplication and QSlider
    
    QObject.connect(slider,SIGNAL("valueChanged(int)"),printNumber)
    #call the printNumber funciton each time the value on the slider changes 
  
    slider.show()
    #start the loop
    sys.exit(app.exec_())

Motor.cpp

Committer:
covrigs
Date:
2016-12-09
Revision:
0:f2c6afc8e527

File content as of revision 0:f2c6afc8e527:

#include "mbed.h"
#include "Motor.h"

Motor::Motor(PinName Positive, PinName Negative): _positive(Positive), _negative(Negative){
     _positive.period(0.03f);      // .03 second period
     _positive.write(0.25f);       // initial duty cycle
     _negative.period(0.03f);      // .03 second period
    _negative.write(0.25f);        // initial duty cycle
     
}   


void Motor::Spin(float move) {
    
    if(move >= .51){                //direction and speed when percentage is above .51
     _positive = (move-.5);
     _negative = 0;
     }
     
    else if(move <= .49){           //direction and speed when percentae is below .49
     _positive = 0;
     _negative = (.5-move);
     }
     
    else if(move > .49 && move < .51){      //stop motor when precentage is about .50
     _positive = 0;
     _negative = 0;
     }
     
}