Arduino UNO 学习 4×4薄膜按键9G舵机超声波传感器人体传感器1位数码管四位数码管

Posted heze

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Arduino UNO 学习 4×4薄膜按键9G舵机超声波传感器人体传感器1位数码管四位数码管相关的知识,希望对你有一定的参考价值。

(一) 4×4薄膜按键

代码如下:

 1 const int numRows = 4;
 2 const int numCols = 4;
 3 const int debounceTime = 20;
 4 const char keymap[numRows][numCols] = {
 5   {1,2,3,A},
 6   {4,5,6,B},
 7   {7,8,9,C},
 8   {*,0,#,D}
 9 };
10 const int rowPins[numRows] = {4,5,6,7};
11 const int colPins[numCols] = {8,9,10,11};
12 
13 void setup() { // all keymap level = HIGH
14   // put your setup code here, to run once:
15   Serial.begin(9600);
16   for(int row = 0; row < numRows; row++){
17     pinMode(rowPins[row], INPUT);  //输入
18     digitalWrite(rowPins[row], HIGH);  //上拉电阻,
19   }
20   for(int column = 0; column < numCols; column++){
21     pinMode(colPins[column], OUTPUT);  //写出,只有列能写(8,9,10,11)口
22     digitalWrite(colPins[column], HIGH);
23   }
24 }
25 
26 void loop() {
27   // put your main code here, to run repeatedly:
28   char key = getkey();
29   if(key != 0){
30     Serial.print("Got key ");
31     Serial.println(key);
32   }
33 }
34 
35 char getkey(){
36   char key = 0;
37   for(int column = 0; column < numCols; column++){  //
38     digitalWrite(colPins[column], LOW);
39     for(int row = 0; row < numRows; row++){
40       if(digitalRead(rowPins[row]) == LOW){  // touch keyboard ?
41         delay(debounceTime);
42         while(digitalRead(rowPins[row]) == LOW){
43           key = keymap[row][column];
44         }
45       }
46     }
47     digitalWrite(colPins[column], HIGH);
48   }
49   return key;
50 }

技术图片       技术图片     技术图片

 

(二) 9G舵机

代码

#include <Servo.h>
Servo myservo;

void setup() {
  // put your setup code here, to run once:
  myservo.attach(9);
}

void loop() {
  // put your main code here, to run repeatedly:
  for(int pos = 0; pos < 180; pos++){
    myservo.write(pos);
    delay(15);
  }
  for(int pos = 180; pos >= 1; pos--){
    myservo.write(pos);
    delay(15);  
  }  
}

技术图片

 

(三) 超声传感器

代码

const int trig = 8;
const int echo = 9;

void setup() {
  // put your setup code here, to run once:
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  long IntervalTime = 0;
  while(1){
    digitalWrite(trig, HIGH);
    delayMicroseconds(15);
    digitalWrite(trig, LOW);
    IntervalTime = pulseIn(echo, HIGH);
    float S = IntervalTime/58.00;
    Serial.println(S);
    S = 0; IntervalTime = 0;
    delay(500);
  }
}

技术图片

 

(四) 人体传感器

代码

int ledpin = 12;
int inpin = 13;
int buttonState;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledpin, OUTPUT);
  pinMode(inpin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  buttonState = digitalRead(inpin);
  
  if(buttonState == HIGH){
    digitalWrite(ledpin, HIGH);
    delay(100);
    buttonState = digitalRead(inpin);

    if(buttonState == LOW){
      digitalWrite(ledpin, LOW);
      Serial.println(buttonState);
    }
  }
}

技术图片

 

(五) 1位数码管

代码

int LEDCount = 8;
const unsigned char dofly_DuanMa[16] = {0x3f,0x06,0x5b,0x4f,0x66,
                                        0x6d,0x7d,0x07,0x7f,0x6f,
                                        0x77,0x7c,0x39,0x5e,0x79,0x71};
int LEDPins[] = {0,1,2,3,4,5,6,7};

void setup() {
  // put your setup code here, to run once:
  for(int i = 0; i < LEDCount; i++){
    pinMode(LEDPins[i], OUTPUT);
  }
}

void deal(unsigned char value){
  for(int i = 0; i < 8; i++){
    digitalWrite(LEDPins[i], bitRead(value, i));
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  for(int i = 0; i < 16; i++){
    deal(dofly_DuanMa[i]);
    delay(1000);
  }
}

技术图片

 

(六) 四位数码管

  1 int LEDCount = 8;
  2 int SEGCount = 4;
  3 long previousMillis = 0;
  4 const unsigned char dofly_DuanMa[16] = {0x3f,0x06,0x5b,0x4f,0x66,
  5                                         0x6d,0x7d,0x07,0x7f,0x6f,
  6                                         0x77,0x7c,0x39,0x5e,0x79,0x71};
  7 int LEDPins[] = {12,8,5,3,2,11,6,4};
  8 int SegPins[] = {13,10,9,7};
  9 unsigned char displayTemp[4];
 10 
 11 void setup() {
 12   // put your setup code here, to run once:
 13   for(int i = 0; i < LEDCount; i++){
 14     pinMode(LEDPins[i], OUTPUT);
 15   }
 16   for(int j = 0; j < SEGCount; j++){
 17     pinMode(SegPins[j], OUTPUT);
 18     //digitalWrite(SegPins[j], HIGH);
 19   }
 20 }
 21 
 22 void deal(unsigned char value){
 23   for(int i = 0; i < 8; i++){
 24     digitalWrite(LEDPins[i], bitRead(value, i));
 25     //bitRead(value, i);
 26   }
 27 }
 28 
 29 // Static code
 30 //void loop() {
 31 //  // put your main code here, to run repeatedly:
 32 //  
 33 //  static unsigned int num;
 34 //  static unsigned long lastTime = 0;
 35 //
 36 //  if(millis() - lastTime >= 1000){
 37 //    lastTime = millis();
 38 //    num++;
 39 //  }
 40 //
 41 //  // static display 1 2 3 4
 42 //  displayTemp[0] = dofly_DuanMa[1];
 43 //  displayTemp[1] = dofly_DuanMa[2];
 44 //  displayTemp[2] = dofly_DuanMa[3];
 45 //  displayTemp[3] = dofly_DuanMa[4];
 46 //
 47 //  static int i = 0;
 48 //  unsigned long currentMillis = millis();
 49 //
 50 //  if(currentMillis - previousMillis > 0){
 51 //    previousMillis = currentMillis;
 52 //    deal(0);
 53 //   
 54 //    for(int a = 0; a < 4; a++){
 55 //      digitalWrite(SegPins[a], HIGH);
 56 //    }
 57 //    
 58 //    digitalWrite(SegPins[i], LOW);
 59 //    deal(displayTemp[i]);
 60 //    delay(500);
 61 //    i++;
 62 //    if(i == 4) { i = 0; }
 63 //  }
 64 //}
 65 
 66 // Dynamic code
 67 void loop() {
 68   // put your main code here, to run repeatedly:
 69   
 70   static unsigned int num;
 71   static unsigned long lastTime = 0;
 72 
 73   if(millis() - lastTime >= 1000){
 74     lastTime = millis();
 75     num++;
 76   }
 77 
 78   // static display 1 2 3 4
 79   displayTemp[0] = dofly_DuanMa[num/1000];
 80   displayTemp[1] = dofly_DuanMa[(num%1000)/100];
 81   displayTemp[2] = dofly_DuanMa[((num%1000)%100)/10];
 82   displayTemp[3] = dofly_DuanMa[((num%1000)%100)%10];
 83 
 84   static int i = 0;
 85   unsigned long currentMillis = millis();
 86 
 87   if(currentMillis - previousMillis > 0){
 88     previousMillis = currentMillis;
 89     deal(0);
 90    
 91     for(int a = 0; a < 4; a++){
 92       digitalWrite(SegPins[a], HIGH);
 93     }
 94     
 95     digitalWrite(SegPins[i], LOW);
 96     deal(displayTemp[i]);
 97     i++;
 98     if(i == 4) { i = 0; }
 99   }
100 }

技术图片

 

总结:

(1)舵机不能转到逆时针的180°,不知道是不是机器的原因;

(2)四位数码管,位引脚低电位有效。

(3)总的来说,代码仔细点,保证硬件有效连接,都还是可以完成的!

以上是关于Arduino UNO 学习 4&#215;4薄膜按键9G舵机超声波传感器人体传感器1位数码管四位数码管的主要内容,如果未能解决你的问题,请参考以下文章

Arduino UNO学习74HC595使用,循序渐进过程

Arduino UNO测量电容值

Arduino uno板上连接一个HC-05,设置后能与手机配对成功,但是无法连接?

arduino uno3 的小白编程问题

arduino uno +sim900+手机接收短信

为啥arduino uno只能保留两位小数