LinMinquan's Blog

Experience technology to change life

Arduino 线性磁力霍尔传感器

同事弄的,我记录下笔记。环境:Windows 10,Auduino 1.6.6,线性磁力霍尔传感器,磁铁。

传感器购买链接磁铁购买链接

#define  M0  4
#define  M1  5
#define  M2  6
#define  M3  7

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(300);
  pinMode(M0 ,INPUT);
  pinMode(M1 , INPUT);
  pinMode(M2 ,INPUT);
  pinMode(M3 ,INPUT);
  Serial.println("AOK");
}

int m=0,m1= 0,m2 = 0,m3 = 0,m4 = 0;
void loop() {
  // put your main code here, to run repeatedly:
  if(readPin(M0) == HIGH)
    {
       //Serial.println("1001");
        m1 = 1;
    }
    else 
    {
         m1 = 0;
    }
    
     if(readPin(M1) == HIGH)
    {
     //  Serial.println("1010");
         m2 = 1;
    }
     else 
     {
          m2 = 0;
     }
     
     if(readPin(M2) == HIGH)
    {
      // Serial.println("1100");
         m3 = 1;
    }
     else 
     {
          m3 = 0;
     }
     
     if(readPin(M3) == HIGH)
    {
     //  Serial.println("1000");
         m4 = 1;
    }
     else 
     {
          m4 = 0;
     }
     
    m = m1*1000+m2*100+m3*10+m4;
    Serial.println(m);
    delay(500);
}

int readPin(int inPin)
 {
   return(digitalRead(inPin));
 }

以上是代码。

P70828 182920

P70828 182746

P70828 182824

蓝色的是Arduino Nano

在Arduino软件的菜单中,Tools –> Board 得选择Arduino Nano

还得选择对应的Com接口,Tools –> Port。

 


Share