//Incrementing Motor Speed Control
一開始motor=9
const int MOTOR = 9; //Motor connected to digital pin 9
const int BUTTON = 2; //Button connected to pin 2
boolean lastButton = LOW; //Variable containing the previous
//button state
boolean currentButton = LOW; //Variable containing the current
//button state
int speedIncrement = 0;
void setup()
{
pinMode (MOTOR, OUTPUT); //Set the MOTOR pin as an output
pinMode (BUTTON, INPUT); //Set button as an input
}
/*
* Debouncing Funtion
* Pass it the previous button state,
* and get back the current debounced button state.
*/
boolean debounce(boolean last)
{
boolean current = digitalRead(BUTTON); //Read the button state
if (last != current) //if it's different
{
delay(5); //wait 5ms
current = digitalRead(BUTTON); //read it again
}
return current;
}
/*
*Motor Speed Selection
*Pass a number for the Motor speed and set it
*/
void setSpeed(int increment)
{
//speed one
if (increment == 1)
{
analogWrite(MOTOR, 110);
}
else if (increment == 2)
{
analogWrite(MOTOR, 150);
}
else if (increment == 3)
{
analogWrite(MOTOR, 180);
}
else if (increment == 4)
{
analogWrite(MOTOR, 200);
}
else if (increment == 5)
{
analogWrite(MOTOR, 255);
}
//OFF
else
{
analogWrite(MOTOR, 0);
}
}
void loop()
{
currentButton = debounce(lastButton); //read debounce state
&&:且為兩個條件組合起來
if (lastButton == LOW && currentButton == HIGH) //if it was pressed
{
speedIncrement++; //Increment motor speed
}
lastButton = currentButton; //reset button value
//if cycled through all speed increments
//reset the speed to 0
if (speedIncrement == 6) speedIncrement = 0;
setSpeed(speedIncrement);
}
2019年6月18日 星期二
訂閱:
張貼留言 (Atom)
algorithm
#include <iostream> #include <string.h> using namespace std; int main(int argc, char** argv) { for(int j=2;j<=100;j++)//j...
-
本文旨在補充《 超圖解Arduino互動設計入門 》第18章「RFID無線識別裝置與問答遊戲製作」單元,書本採用的RFID讀卡機模組是採用9600bps, TTL序列通訊介面,RFID的通訊頻率為125KHz。這種模組的接線和程式都很簡單,每當感測到RFID卡,讀卡機就把...
-
3.Keil C 幫我們寫好 程式:#include <reg51.h> vs c語言 #include <stdio.h> //幫您宣告 P0=0x80 有相對的記憶體以十六進位表示,它定義定reg51.h,我們直接拿來用他的代碼即可 P1=0...
-
/* 多重按鈕開關 */ //==宣告區================================ #include // 定義8051暫存器之標頭檔,P2-17~19 #include "myio.h" // 自己寫的I/O程式庫 sbit PB...
沒有留言:
張貼留言