我买了这个测距仪http://www.dfrobot.com/wiki/index.php/URM37_V3.2_Ultrasonic_Sensor_(SKU:SEN0001)
现在我很难让它在 nodeJS 上运行。我的设置是使用“firmata”库的 Arduino UNO 和 nodeJs。
我尝试了什么: 1. 我用原生 Arduino IDE 测试了测距仪,它工作正常。2. 然后我从 Arduino IDE 将数据写入 EEPROM(代码示例如下):
int cmmd1[]={
0x44,0x00,0x10,0x54};//low byte stored in the sensor for the distance threshold.
int cmmd2[]={
0x44,0x01,0x00,0x45};//high byte, write 0x0010 into address 0x01 and 0x00,so the threshold is set to 16cm
int cmmd3[]={
0x44,0x02,0xaa,0xf0};// Autonomous mode. write 0xaa into address 0x02
//int cmmd3[]={
// 0x44,0x02,0xbb,0x01}; // PWM mode. write 0xbb into address 0x02
int i;
void setup(){
Serial.begin(9600); // Sets the baud rate to 9600
A_Mode_Setup(); //PWM mode setup function
}
void loop()
{
}
void A_Mode_Setup(){
//write the data into the URM37 EEPROM
for (i=0;i<4;i++)
Serial.write(cmmd3[i]);
delay(200);
for (i=0;i<4;i++)
Serial.write(cmmd1[i]);
delay(200);
for (i=0;i<4;i++)
Serial.write(cmmd2[i]);
delay(200);
}
我试图制作与手册中相同的代码以使其工作但在nodeJS中,但我失败了:
var board = new firmata.Board('/dev/cu.usbmodemfd121', function() { var command = [0x44, 0x02, 0xbb, 0x01]; board.pinMode(5, board.MODES.OUTPUT); board.digitalWrite(5, board.HIGH); board.pinMode(3, board.MODES.INPUT); board.sp.write(command, function(err, results) { console.log(arguments) }); var callback = function() { console.log(arguments); //console.log(this) } board.on('pulse-in-3', callback); board.on('pin-state-3', callback); board.pulseIn({ pin: 3, value: board.LOW, timeout: 50 }, callback); setInterval(function() { board.digitalWrite(5, board.LOW); board.digitalWrite(5, board.HIGH); }, 100);
});
请各位大侠帮帮我!