Skip to content

Commit

Permalink
change ultrasonic
Browse files Browse the repository at this point in the history
  • Loading branch information
tangjie133 committed Dec 25, 2020
1 parent b7f7c12 commit 578b9e9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
40 changes: 40 additions & 0 deletions NecIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,44 @@ int irCode(){
return data;
}

//%
int readPulseIn(int status){
uint32_t lasttime,nowtime,temp;
if(status == 1){//HIGH
lasttime = system_timer_current_time_us();
while(!uBit.io.P2.getDigitalValue()){
temp = system_timer_current_time_us();
if((temp - lasttime) > 70000){
//uBit.serial.printf("time out 0 %d\r\n",(temp-lasttime));
return -1;
}
}
lasttime = system_timer_current_time_us();
while(uBit.io.P2.getDigitalValue()){
if((system_timer_current_time_us() - lasttime) > 70000){
//uBit.serial.printf("time out 1");
return -1;
}
}
nowtime = system_timer_current_time_us();

}else{//LOW
while(uBit.io.P2.getDigitalValue()){
if((system_timer_current_time_us() - lasttime) > 70000){
//uBit.serial.printf("time out 3");
return -1;
}
}
lasttime = system_timer_current_time_us();
while(!uBit.io.P2.getDigitalValue()){
if((system_timer_current_time_us() - lasttime) > 70000){
//uBit.serial.printf("time out 4");
return -1;
}
}
nowtime = system_timer_current_time_us();
}
return (nowtime - lasttime);
}

}
9 changes: 7 additions & 2 deletions maqueen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ function valuotokeyConversion():number{
onPressEvent(IrPressEvent, maqueencb)
}

//% advanced=true shim=maqueenIRV2::readPulseIn
function readPulseIn(status:number): number {
return 0;
}
/**
* Read ultrasonic sensor.
*/
Expand All @@ -254,11 +258,11 @@ function valuotokeyConversion():number{
if (pins.digitalReadPin(DigitalPin.P2) == 0) {
pins.digitalWritePin(DigitalPin.P1, 1);
pins.digitalWritePin(DigitalPin.P1, 0);
d = pins.pulseIn(DigitalPin.P2, PulseValue.High, maxCmDistance * 58);
d = readPulseIn(1);//pins.pulseIn(DigitalPin.P2, PulseValue.High, maxCmDistance * 58);
} else {
pins.digitalWritePin(DigitalPin.P1, 0);
pins.digitalWritePin(DigitalPin.P1, 1);
d = pins.pulseIn(DigitalPin.P2, PulseValue.Low, maxCmDistance * 58);
d = readPulseIn(0);//pins.pulseIn(DigitalPin.P2, PulseValue.Low, maxCmDistance * 58);
}
let x = d / 39;
if (x <= 0 || x > 500) {
Expand Down Expand Up @@ -432,4 +436,5 @@ function valuotokeyConversion():number{
basic.pause(50);
})


}
4 changes: 4 additions & 0 deletions pxt.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"test.ts"
],
"public": true,
"targetVersions": {
"target": "3.1.46",
"targetId": "microbit"
},
"supportedTargets": [
"microbit"
],
Expand Down

0 comments on commit 578b9e9

Please sign in to comment.