1

この距離計を購入しましたhttp://www.dfrobot.com/wiki/index.php/URM37_V3.2_Ultrasonic_Sensor_(SKU:SEN0001)

そして今、nodeJSで動作させるのに苦労しています。私のセットアップは、「firmata」ライブラリを使用したArduino UNOとnodeJです。

試したこと: 1. Rangefinder をネイティブの 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);

}
  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);
    

    });

みんな私を助けてください!

4

0 に答える 0