Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: main.cpp
- Revision:
- 1:f77859e74071
- Parent:
- 0:bdd449e55907
--- a/main.cpp Mon Feb 27 12:45:08 2017 +0000 +++ b/main.cpp Fri Mar 03 17:09:31 2017 +0000 @@ -4,45 +4,113 @@ I2CSlave slave(D14, D15); + +Serial pc(USBTX, USBRX); // tx, rx + +PwmOut pwmNO(D9); +DigitalOut dirNO(D7); +PwmOut pwmNE(D10); +DigitalOut dirNE(D8); + +float power_NO = 0.0; +float power_NE = 0.0; + int main() { DigitalOut myLED (LED2); + + pc.printf("Hello!\n"); + + pwmNO.period_us(1000); + pwmNE.period_us(1000); + dirNO=0; + dirNE=1; + + pwmNO.pulsewidth_us(0); + pwmNE.pulsewidth_us(0); - - - slave.address(0x14); - - - - char buf[10]; - - - + const int addr = 0x10; - while (1) { + slave.address(addr); + + char buf[1]; + + while (1) { int i = slave.receive(); + switch (i) { - // case I2CSlave::ReadAddressed: + // case I2CSlave::ReadAddressed: - // slave.write(5); // Includes null char + // slave.write(5); // Includes null char - // myLED=0; - // break; - //case I2CSlave::WriteGeneral: + // myLED=0; + // break; + // case I2CSlave::WriteGeneral: //to implement case I2CSlave::WriteAddressed: - slave.read(buf, 10); - - if (buf ==1){ - myLED=1; - } + slave.read(buf, 1); + char c= buf[0]+48; + + if((c == '1') && (power_NO < 0.8)) { + power_NO += 0.2; + pc.printf("Gauche Up!\n"); + } + if((c == '2') && (power_NO > -0.8)) { + power_NO -= 0.2; + pc.printf("Gauche Down!\n"); + } + if((c == '3') && (power_NE < 0.8)) { + power_NE += 0.2; + pc.printf("Droite Up!\n"); + } + if((c == '4') && (power_NE > -0.8)) { + power_NE -= 0.2; + pc.printf("Droite Down!\n"); + } + if((c == '5') && (power_NO < 0.8) && (power_NE < 0.8)) { + power_NO += 0.2; + power_NE += 0.2; + pc.printf("Gauche Droite Up!\n"); + } + if((c == '6') && (power_NO > -0.8)&& (power_NE > -0.8)) { + power_NO -= 0.2; + power_NE -= 0.2; + pc.printf("Gauche Droite Down!\n"); + } + if((c == '7')) { + power_NO = 0; + power_NE = 0; + pc.printf("Stop!\n"); + } + + + + + + if(power_NO>0){ + dirNO=0; + pwmNO.pulsewidth_us(1000*power_NO); + } else { + dirNO=1; + pwmNO.pulsewidth_us(-1000*power_NO); + } + if(power_NE>0){ + dirNE=1; + pwmNE.pulsewidth_us(1000*power_NE); + } else { + dirNE=0; + pwmNE.pulsewidth_us(-1000*power_NE); + } + + + wait(0.1); break; } - for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer + for(int i = 0; i < 1; i++) buf[i] = 0; // Clear buffer } }