//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卡,讀卡機就把...
-
setw [C++] 使cout印出格式對齊-setw 設定輸出的欄位長度 85138 0 C/C++ 最近在一些程式,需要用到命令提示字元模式來印出結果,有時候我們會像表格一樣要每一筆資料可能有兩到三個欄位,但這時候,印出時都會因為印出的文字或數字太長而使排版跑掉 但是在...
-
#include "reg51.h" main() { void delay(int); int i ,j; char seg[10]={0xc0,0xf9,0xa4,0xb0,0x99, 0x92,0x82,0xf8,0x80,0x9...
沒有留言:
張貼留言