如何将URM37(测距仪)连接到Arduino并通过nodeJS读取数据

时间:2013-08-25 11:12:46

标签: node.js arduino firmata

我买了这个测距仪http://www.dfrobot.com/wiki/index.php/URM37_V3.2_Ultrasonic_Sensor_(SKU:SEN0001)

现在我很难让它在nodeJS上运行。 我的设置是Arduino UNO和nodeJs使用“firmata”库。

我尝试了什么: 1.我使用原生Arduino IDE测试了Rangefinder并且它工作正常。 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);

}
  1. 我尝试使用与手册中相同的代码使其工作但是在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);
    

    });

  2. 请大家帮助我!

0 个答案:

没有答案