#include "mbed.h"
//Interface with the PC with UART
Serial pc(USBTX, USBRX); // tx, rx
//Valve control
DigitalOut ThValve1(p15); //Port 15
DigitalOut ThValve2(p16); // Port 16
DigitalOut AbdValve1(p17); // Port 17
DigitalOut VacDivValve1(p18); // Port 18
DigitalOut VacDivValve2(p19); // Port 19
DigitalOut VentValve1(p20); // Port 20
AnalogOut speed(p21); //Port 21
DigitalOut Th1_Led(LED1);
DigitalOut Th2_Led(LED2);
DigitalOut Abd_Led(LED3);
DigitalOut VD1_Led(LED4);
char c[20];
char temp;
bool start =0;
bool dataReady =0;
int digitCounter;
int receivedNumber;
int main() {
//set the baudrate
pc.baud(9600);
// initit
Th1_Led = 0;
Th2_Led = 0;
Abd_Led = 0;
VD1_Led = 0;
ThValve1 = 0;
ThValve2 = 0;
AbdValve1 = 0;
VacDivValve1 = 0;
VacDivValve2 = 0;
VentValve1 = 0;
Th1_Led = 1;
wait(1);
Th1_Led = 0;
while(1) {
// This checks if PC sent any data to uC. This is a flag that indicates data were received
if(pc.readable()) {
// This reads the data that was sent
temp = pc.getc();
// Control ThValve1
if (temp =='A') { ThValve1 = 1; Th1_Led=1;}
if (temp =='B') { ThValve1 = 0; Th1_Led=0;}
// Control ThValve2
if (temp =='C') { ThValve2 = 1; Th2_Led=1;}
if (temp =='D') { ThValve2 = 0; Th2_Led=0;}
// Control AbdValve1
if (temp =='E') { AbdValve1 = 1; Abd_Led=1;}
if (temp =='F') { AbdValve1 = 0; Abd_Led=0;}
// Control VacDivValve1
if (temp =='G') { VacDivValve1 = 1; VD1_Led=1;}
if (temp =='H') { VacDivValve1 = 0; VD1_Led=0;}
// Control VacDivValve2
if (temp =='I') { VacDivValve2 = 1;}
if (temp =='J') { VacDivValve2 = 0;}
// Control VentValve1
if (temp =='K') { VentValve1 = 1;}
if (temp =='L') { VentValve1 = 0;}
//-------------SPEED CONTROL--------------------------------------------------
//----------------------------------------------------------------------------
if (start) {
if (temp =='S') {
start= 0;
// This changes the c array into a number - +=m indicates an increment
for (int j=0; j<10; j++) {
//receivedNumber +=(c[j]-48)* pow(10.0,(digitCounter -(j+1)));
receivedNumber +=(c[j]-48)* pow(10.0,digitCounter -(j+1));
}
speed = (receivedNumber+4)/100.0;
pc.printf(" %f", (receivedNumber+4)/100.0);
// Two digits received indicate a command has been sent
//if (digitCounter > 0) {
// This is for data greater than 2 digits (3 digits) and indicates speed
//speed =receivedNumber/600.0;
//pc.printf(" speed %.2f",receivedNumber/600.0);
//pc.printf(" speed %d",receivedNumber);
//}
// Not A and not B indicates data to build a number, by inputting numbers into c array with indexes 1-3
}
else {
c[digitCounter]=temp;
digitCounter++;
}
}
if (temp =='T') {
dataReady = 0;
start = 1;
receivedNumber = 0;
digitCounter=0;
for (int j=1; j< 20; j++)
c[j] = 0;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
}
}
}
To get proper formatting could you please add a line with three backtick characters (like these ```) at the begin and end of your program (edit your post above)? That will make a copy&paste
to my online compiler easier.
Done.
This code was working before then I got the warning and the code is not working when I upload it on the LPC1768.
There is no AnalogOut available on LPC1768 at pin p21
. Try pin p18
instead:
//AnalogOut speed(p21); //Port 21
AnalogOut speed(p18); //Port 18
But as I see, pin p18
is already used as DigitalOut
. So you have to select a different pin for the VacDivValve1
.
However, PwmOut is available at pin p21
. Maybe you worked recently with Arduino where PWM output is called “Analog” (analogWrite()
) and you used that in Mbed by mistake. In this case try:
//AnalogOut speed(p21); //Port 21
PwmOut speed(p21); //Port 21
Hi,
it works! Thank you very much for your help!