Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
This is my code
#include "mbed.h" Serial pc(USBTX, USBRX); // tx, rx PwmOut motor1(p21); PwmOut motor2(p22); PwmOut led1(LED1); PwmOut led2(LED2); float speed = 0.0; void motorcontrol(float speed); int main() { pc.printf("Press 'u' to turn motor clockwise, 'd' to turn it counter clockwise\n"); motor1.period(0.01); motor2.period(0.01); while(1) { pc.putc(speed); char c = pc.getc(); if((c == 'u') && (speed < 1)) { speed += 0.05; motorcontrol(speed); } if((c == 'd') && (speed > -1)) { speed -= 0.05; motorcontrol(speed); } } error("Loop unexpectedly terminated"); return 0; } void motorcontrol(float speed) { if (speed>0) //clockwise { motor1=1; motor2=1-speed; led1=speed; led2=0; } if (speed<0) //ccw { motor1=1+speed; motor2=1; led1=0; led2=-speed; } if (speed==0) //brake { motor1=1; motor2=1; led1=0; led2=0; } }When I run this, the LED brightness changes as expected, but when I measure the pin outputs using an oscilloscope, I get weird readings. Does anyone know what could be causing this problem?
Edit Also, would something like following work?
PwmOut pin (p21) int main(){ pin=0.5; }In other words, does the pin=0.5 need to be placed in a infinite loop for it to work?