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_())
Committer:
covrigs
Date:
Fri Dec 09 19:58:50 2016 +0000
Revision:
0:f2c6afc8e527
For ECE 111, Lab 4. KL46Z receives serial data from a python GUI and controls a motor via an L293D.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
covrigs 0:f2c6afc8e527 1 #include "mbed.h"
covrigs 0:f2c6afc8e527 2 #include "Motor.h"
covrigs 0:f2c6afc8e527 3
covrigs 0:f2c6afc8e527 4 Motor::Motor(PinName Positive, PinName Negative): _positive(Positive), _negative(Negative){
covrigs 0:f2c6afc8e527 5 _positive.period(0.03f); // .03 second period
covrigs 0:f2c6afc8e527 6 _positive.write(0.25f); // initial duty cycle
covrigs 0:f2c6afc8e527 7 _negative.period(0.03f); // .03 second period
covrigs 0:f2c6afc8e527 8 _negative.write(0.25f); // initial duty cycle
covrigs 0:f2c6afc8e527 9
covrigs 0:f2c6afc8e527 10 }
covrigs 0:f2c6afc8e527 11
covrigs 0:f2c6afc8e527 12
covrigs 0:f2c6afc8e527 13 void Motor::Spin(float move) {
covrigs 0:f2c6afc8e527 14
covrigs 0:f2c6afc8e527 15 if(move >= .51){ //direction and speed when percentage is above .51
covrigs 0:f2c6afc8e527 16 _positive = (move-.5);
covrigs 0:f2c6afc8e527 17 _negative = 0;
covrigs 0:f2c6afc8e527 18 }
covrigs 0:f2c6afc8e527 19
covrigs 0:f2c6afc8e527 20 else if(move <= .49){ //direction and speed when percentae is below .49
covrigs 0:f2c6afc8e527 21 _positive = 0;
covrigs 0:f2c6afc8e527 22 _negative = (.5-move);
covrigs 0:f2c6afc8e527 23 }
covrigs 0:f2c6afc8e527 24
covrigs 0:f2c6afc8e527 25 else if(move > .49 && move < .51){ //stop motor when precentage is about .50
covrigs 0:f2c6afc8e527 26 _positive = 0;
covrigs 0:f2c6afc8e527 27 _negative = 0;
covrigs 0:f2c6afc8e527 28 }
covrigs 0:f2c6afc8e527 29
covrigs 0:f2c6afc8e527 30 }