Arduino 项目笔记|旋转鞋柜#按钮去抖Debounce#电机#压力传感器

一、轻触按键开关

零基础入门学用Arduino-基础知识篇-13 数字输入1 - 按键开关

零基础入门学用Arduino-基础知识篇-14 数字输入2 - 按键开关控制电路与程序

按键开关是一种功能性的电子开关,使用时轻轻点触开关按钮就可以是开关接通,当松开手时开关就断开。特点相连不同侧,同侧不相连。
1.上拉电阻

arduino内部自带内部上拉电阻,如果要使用该内部上拉电阻,只需要设置pinMode(pin,INPUT_PULLUP);若该引脚pin设置为上拉电阻后,请勿将该引脚pin接入高于5V的电压。
Arduino 项目笔记|旋转鞋柜#按钮去抖Debounce#电机#压力传感器_第1张图片Arduino 项目笔记|旋转鞋柜#按钮去抖Debounce#电机#压力传感器_第2张图片

  • Debounce 按钮去抖

  • 方案一

int pushButton = 7;//按钮的输入引脚
int buttonState =0;//按钮的状态
int beforeState =0;//前一刻的按钮状态
int presstime  = 0;//假设按下按钮的次数
void setup()
{
	Serial.begin(9600);//串口波特率
	pinMode(pushButton,INPUT);//设定7号脚为输入	
	
}
void loop() 
{	
	buttonState = digitalRead(pushButton);
	if(buttonState == 1 && beforeState == 0 )
	//从没有按到按下按钮的一瞬间:按下为0,按下之前为1
	//按钮是上拉电阻按下为“0”
	{
		presstime=presstime+1;
		
		Serial.print("hi~~");
		Serial.println(presstime);
		
		
	}
	if(buttonState!=beforeState)
	{
		delay(20);
	}
	
	beforeState=buttonState;
	
	
	delay(100);	
}
  • 方案二
    // constants won’t change. They’re used here to set pin numbers:
    const int button2Pin = 2; // the number of the pushbutton pin
    const int ledPin = 10; // the number of the LED pin
    const int button4Pin = 4; // variables will change:
    const int motor3pin = 3;
    const int motor5pin = 5;
    int pspin = A0;
    float val1 = 0;

int buttonState2 = 0; // variable for reading the pushbutton status
int buttonState4 = 0;
#define sudu 100

void setup() {

Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin,LOW);
// initialize the pushbutton pin as an input:
pinMode(button2Pin, INPUT);
pinMode(button4Pin, INPUT);

pinMode(motor3pin,OUTPUT);
pinMode(motor5pin,OUTPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState2 = digitalRead(button2Pin);
buttonState4 = digitalRead(button4Pin);

float val1 = analogRead(pspin);

Serial.println(buttonState2);
Serial.println(buttonState4);

Serial.println(val1);

if(val1>10)
{

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState2 == LOW) {
// turn LED on:
digitalWrite(ledPin, LOW);

 digitalWrite(motor5pin, LOW);
 //digitalWrite(motor3pin, HIGH); 
 analogWrite(motor3pin, sudu); 

}
if (buttonState4 == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);

digitalWrite(motor3pin, LOW);
//digitalWrite(motor5pin, HIGH);
analogWrite(motor5pin, sudu);

}
if ((buttonState4 == LOW)&&(buttonState2 == LOW))
{
digitalWrite(motor3pin, LOW);
digitalWrite(motor5pin, LOW);

for(int i=0;i<10;i++){
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
}

}

}
else
{
delay(20);
}
delay(200);
}

  • 方案三
//boolean state = false;
boolean buttonUp = true;//flag旗标-之前被按下过为true,按钮又弹起来

void setup()
{
	//pinMode(13,OUTPUT);
	//digitalWrite(13,LOW);
	pinMode(7,INPUT);
	digitalWrite(HIGH);//初始化为HIGH,上拉电阻为“1”
}
void loop()
{
	if(digitalRead(7) == LOW && buttonUp == true)//按钮按下的一瞬间
		//digitalRead(7)!= HIGH && buttonUp ==true
	{
	digitalWrite(13,!digitalRead(13));//测试点亮LED灯
	buttonUp = false;
	}
	else if(digitalRead(7)== HIGH && buttonUp == false)//按钮没有按下
		//digitalRead(7)== HIGH && buttonUp !=true
	{
		buttonUp = true;
	}
	
	delay(100);
}

注意:方案三简单好理解!!!

二、电机

  • L298N电机驱动
    简介:电机驱动模块非常适合在电池供电的智能小车、玩具小车、机器人等上面使用,供电电压2V~10V,可同时驱动两个直流电机或者1个4线2相式步进电机,可实现正反转和调速的功能,每路电流能到1.5A持续电流,峰值电流可达2.5A,有热保护并且能够自动恢复。

  • 注意:
    1.电源正极与负极接反肯定会造成电路损坏。

    2.输出对地短路或输出端短路,还有电机堵转的情况下,芯片都会热保护的,但是在接近或者超过10V电压且峰值电流大大超过2.5A的情况下也会造成芯片烧毁。

  • Arduino 项目笔记|旋转鞋柜#按钮去抖Debounce#电机#压力传感器_第3张图片
    功能描述:当人坐在鞋柜上(压力传感器)之后,按键pin2、pin4控制电机正(motor3pin)反(motor5pin)转。

const int button2Pin = 2;     // the number of the pushbutton pin
const int ledPin =  10;      // the number of the LED pin
const int button4Pin = 4;   // variables will change:
const int motor3pin = 3;
const int motor5pin = 5;
int pspin = A0;//压力传感器
float val1 = 0;

 int buttonState2 = 0;         // variable for reading the pushbutton status
 int buttonState4 = 0;
#define   sudu   100//电机的转速


void setup() {

  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin,LOW);
  // initialize the pushbutton pin as an input:
  pinMode(button2Pin, INPUT);
  pinMode(button4Pin, INPUT);
  
  pinMode(motor3pin,OUTPUT);
  pinMode(motor5pin,OUTPUT);
}

void loop() {
  // read the state of the pushbutton value:
   buttonState2 = digitalRead(button2Pin);
   buttonState4 = digitalRead(button4Pin);
   
   float val1 = analogRead(pspin);
   
  Serial.println(buttonState2);
  Serial.println(buttonState4);
  
  Serial.println(val1);

  if(val1>10)
  {
  
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState2 == LOW) {
    // turn LED on:
    digitalWrite(ledPin, LOW);
    
     digitalWrite(motor5pin, LOW);
     //digitalWrite(motor3pin, HIGH); 
     analogWrite(motor3pin, sudu); 
    
  } 
  if (buttonState4 == LOW) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    
    digitalWrite(motor3pin, LOW);
    //digitalWrite(motor5pin, HIGH);
    analogWrite(motor5pin, sudu);
  } 
  if ((buttonState4 == LOW)&&(buttonState2 == LOW)) 
  {
    digitalWrite(motor3pin, LOW);
    digitalWrite(motor5pin, LOW);
    
    for(int i=0;i<10;i++){
    digitalWrite(ledPin, HIGH);
    delay(200);
    digitalWrite(ledPin, LOW);
    delay(200);
    }
    
  } 

  
  }
  delay(200);
}

第二版:



// constants won't change. They're used here to set pin numbers:
const int button2Pin = 2;     // 按钮1
const int ledPin =  10;      // 提示灯
const int button4Pin = 4;   // 按钮2
const int motor3pin = 3;    //电机正转pin脚
const int motor5pin = 5;    //电机反转pin脚
int pspin = A0;             //压力传感器检测pin脚
float val1 = 0;             //压力值

 int buttonState2 = 0;         // 按钮的状态值
 int buttonState4 = 0;
#define   sudu   192        //电机转速初始化值


void setup() {

  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin,LOW);
  // initialize the pushbutton pin as an input:
  pinMode(button2Pin, INPUT);
  pinMode(button4Pin, INPUT);
  
  pinMode(motor3pin,OUTPUT);
  pinMode(motor5pin,OUTPUT);
}

void loop() {
  // read the state of the pushbutton value:
   buttonState2 = digitalRead(button2Pin);//读取按钮1的状态
   buttonState4 = digitalRead(button4Pin);
   
   float val1 = analogRead(pspin);
   
  Serial.println(buttonState2);
  Serial.println(buttonState4);
  
  Serial.println(val1);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState2 == LOW&&buttonState4 == HIGH)
  //按钮1按下按钮2没有按下 或者按钮1没有按下,按钮2按下;根据按钮的电路是上拉电阻还是 下拉电阻判断!!!
  {
    // turn LED on:
    digitalWrite(ledPin, LOW);
    
     digitalWrite(motor5pin, LOW);
     //digitalWrite(motor3pin, HIGH); 
     analogWrite(motor3pin, sudu); 
    
  } 
  if (buttonState4 == LOW&&buttonState2 == HIGH) 
  {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    
    digitalWrite(motor3pin, LOW);
    //digitalWrite(motor5pin, HIGH);
    analogWrite(motor5pin, sudu);
  } 
  if ((buttonState4 == LOW)&&(buttonState2 == LOW)) //电机停止转动
  {
    digitalWrite(motor3pin, LOW);
    digitalWrite(motor5pin, LOW);
    
    /*for(int i=0;i<10;i++){
    digitalWrite(ledPin, HIGH);
    delay(200);
    digitalWrite(ledPin, LOW);
    delay(200);
    }*/
    
  } 


  if(val1>10)
  {
  
  
  
  }

  delay(200);
}

第三版:


const int button2Pin = 2;     // the number of the pushbutton pin
const int ledPin =  10;      // the number of the LED pin
const int button4Pin = 4;   // variables will change:
const int motor3pin = 3;
const int motor5pin = 5;
int pspin = A0;
float val1 = 0;
int p_value = 4;

 int buttonState2 = 0;         // variable for reading the pushbutton status
 int buttonState4 = 0;
#define   sudu   255


void setup() {

  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin,LOW);
  // initialize the pushbutton pin as an input:
  pinMode(button2Pin, INPUT);
  pinMode(button4Pin, INPUT);
  
  pinMode(motor3pin,OUTPUT);
  pinMode(motor5pin,OUTPUT);
}

void loop() {
  // read the state of the pushbutton value:
   buttonState2 = digitalRead(button2Pin);
   buttonState4 = digitalRead(button4Pin);
   
   float val1 = analogRead(pspin);
   
  Serial.println(buttonState2);
  Serial.println(buttonState4);
  
  Serial.println(val1);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if ((buttonState2 == LOW)&&(buttonState4 == HIGH )&&( val1>=p_value))
  //有人坐下、按钮2(下拉电阻)没有按下、按钮4按下(正转)
  {
    // turn LED on:
    digitalWrite(ledPin, LOW);
    
     digitalWrite(motor5pin, LOW);
     //digitalWrite(motor3pin, HIGH); 
     analogWrite(motor3pin, sudu); 
    
  } 
  if ((buttonState4 == LOW)&&(buttonState2 == HIGH )&& (val1>=p_value))
  //反转,按钮2按下,按钮4没有按下,有人坐下
  {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    
    digitalWrite(motor3pin, LOW);
    //digitalWrite(motor5pin, HIGH);
    analogWrite(motor5pin, sudu);
  } 
  if ((buttonState4 == LOW)&&(buttonState2 == LOW) && (val1<=p_value))
  //两个按钮没有按下,没有人(电机停止)
  {
    digitalWrite(motor3pin, LOW);
    digitalWrite(motor5pin, LOW);
    
    /*for(int i=0;i<10;i++){
    digitalWrite(ledPin, HIGH);
    delay(200);
    digitalWrite(ledPin, LOW);
    delay(200);
    }*/
    
  } 
  if ((buttonState4 == LOW)&&(buttonState2 == LOW) && (val1>=p_value)) 
  //两个按钮没有按下,有人坐在上面(压力值),电机停止
  {
    digitalWrite(motor3pin, LOW);
    digitalWrite(motor5pin, LOW);
    
  } 

  delay(200);
}

三、FSR402压力传感器

Arduino 传感器: 使用FSR402压力传感器检测压力

  • FSR402 薄膜压力传感器

  • 公头杜邦线

  • 面包板

  • Arduino Uno R3

  • 10KΩ 电阻一个

  • Type A转B, USB 2.0线一根(打印机线), 作为Arduino Uno的电源

  • Arduino 项目笔记|旋转鞋柜#按钮去抖Debounce#电机#压力传感器_第4张图片
    关键代码:

analogRead(A0);

你可能感兴趣的:(Arduino,arduino按钮,电机,debounce)