arduino 舵機控制问题-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3311|回复: 0

[过期] arduino 舵機控制问题

[复制链接]
发表于 2014-5-7 16:31 | 显示全部楼层 |阅读模式
我们想用舵机和LCD控制停车场的车辆数目,当COUNTER数=0(车位满)的时候,舵机不会开闸以防止车辆继续进入。但是当counter = 0的时候, 车闸门(舵机)仍会开闸, 想问问应如何修改程序,主要是最後那段程序不懂如何修改。
以下是我们的程序
// include the library code:
#include <LiquidCrystal.h>
#include<Servo.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(6, 5, 28, 26, 24, 22);


//Set buttons pin numbers
byte incrementButton = 2; //LCD数字增加按钮
byte decrementButton = 3; //LCD数字减数按钮


//Global variables
Servo myservo;


int switchPin = 7; //入闸舵机按钮(即与LCD数字减数按钮相同)
int value = 0; //可变数


int incrementState = 0; //variable that will read the increment button (either HIGH or LOW)
int decrementState = 0; //variable that will read the decrement button (either HIGH or LOW)
int counter = 8;//variable that will store the count
int lastIncrementState = 0;
int lastDecrementState = 0;


void setup()
{
pinMode(incrementButton, INPUT); //set incrementButton as INPUT
pinMode(decrementButton, INPUT); //set decrementButton as INPUT
lcd.begin(16, 2); //set up the LCD's number of columns and rows:
lcd.print("NewTechandMe.com"); //print a message on the LCDvoid setup() {

myservo.attach(9); //舵机输出口
pinMode(switchPin, INPUT);
}


void loop()
{
lcd.setCursor(0, 1);
incrementState = digitalRead(incrementButton); //read the increment button state
decrementState = digitalRead(decrementButton); //read the decrement button state

if(incrementState != lastIncrementState) //compare increment button state to its last state
{
if(incrementState == LOW)//increment button is pressed
{
counter = counter + 1; //increment the counter
if (counter>=8)
{ counter=8;

} else{
counter=counter;}
lcd.print(counter); //print it on serial monitor
lcd.print(" "); //16 blank spaces
delay(20); //debounce delay
}
}
lastIncrementState = incrementState;


if(decrementState != lastDecrementState)//compare decrement state to its lastState
{
if(decrementState == LOW)//decrement button is pressed
{
counter = counter - 1; //decrement the counter
if (counter<=0)
{ counter=0;

} else{
counter=counter;}
lcd.print(counter); //print it on serial monitor
lcd.print(" "); //16 blank spaces
delay(20); //debounce delay
}
}
lastDecrementState = decrementState;


value = digitalRead(switchPin);


if (HIGH == value)
{
myservo.write(90); //闸门关闭
delay(500);
}else {

myservo.write(180); //闸门开启
delay(2000);

}
}



您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-5 03:27 , Processed in 0.070592 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表