純手工割過一次後嚇壞我了...是的..我很弱
只好想想辦法讓自己輕鬆一些了
新增說明文字 |
/*
2016-03-24 for gus cuting set 0-100 step
2016-02-09 toneAC library & H/W pin ghange, pow add (Range 0~max_speed )
2016-02-03 優化參數
2016-01-21 增加充電迴路指示
2016-01-12 onboard try ok by yulie~
*/
const int max_speed = 6000; // set max step /sec
const int min_speed = 0; // set min step
const byte sw_F = 17; //pin a3
const byte sw_R = 16; //pin a2
const byte ms1 = 12; // (1) seepper /2
const byte ms2 = 11; // (1) stepper /4 ( ms1(1) + ms2(1) stepper /8 )
const byte rst = 7; //
const byte slp = 8; // pin A4988 sleep(0) & unlock motor
const byte stp = 9; // pin
const byte dir = 6; // pin
long int v1 = 0;
unsigned long t, u, v, w = 0;
#include <Wire.h>
#include <toneAC.h>
/* Pins 9 & 10 - ATmega328, ATmega128, ATmega640, ATmega8, Uno, Leonardo, etc.
Syntax
toneAC( frequency [, volume [, length [, background ]]] ) - Play a note.
frequency - Play the specified frequency indefinitely, turn off with toneAC().
volume - [optional] Set a volume level. (default: 10, range: 0 to 10 [0 = off])
length - [optional] Set the length to play in milliseconds. (default: 0 [forever], range: 0 to 2^32-1)
background - [optional] Play note in background or pause till finished? (default: false, values: true/false)
toneAC() - Stop output.
noToneAC() - Same as toneAC().
* * 句法
toneAC(頻率[,音量[,長度[背景]]]) - 彈奏音符。
頻率 -播放特定頻率無止境,隨著toneAC關閉()。
音量 - [可選]設置音量。(默認值:10,範圍:0〜10 [0 =關閉])
長度 - [可選]設置以毫秒為單位播放的長度。(默認:0 [永遠],範圍:0〜2 ^ 32-1)
背景 - [可選]在後台播放音符或暫停,直到完成了嗎?(默認:FALSE,值:真/假)
toneAC() -停止輸出。
noToneAC() -同toneAC()
*/
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f, 16, 2); // set the LCD address to 0x3f for a 16 chars and 2 line display
byte battery[8] = {B01110, B01010, B10001, B10001, B10001, B10001, B10001, B11111}; //for 2004 LCD show battery
byte battery_full[8] = {B01110, B01110, B11111, B11111, B11111, B11111, B11111, B11111}; //for 2004 LCD show battery
byte charg[8] = {B01010, B01010, B11111, B10001, B10001, B01010, B00100, B00100}; //for 2004 LCD show change jack
void setup()
{
analogReference(DEFAULT); //5v
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.createChar(0, battery); //battery address => byte(0) command-> lcd.write(byte(0));
lcd.createChar(1, battery_full); //battery address => byte(0) command-> lcd.write(byte(0));
lcd.createChar(2, charg);
lcd.backlight();
pinMode(sw_F, INPUT_PULLUP);
pinMode(sw_R, INPUT_PULLUP);
pinMode(rst, OUTPUT);
// pinMode(stp, OUTPUT);
pinMode(dir, OUTPUT);
pinMode(slp, OUTPUT);
pinMode(ms1, OUTPUT);
pinMode(ms2, OUTPUT);
digitalWrite(rst, HIGH);
digitalWrite(ms1, 1); digitalWrite(ms2, 1);
//ms1 ms2
// 0 0 -> 360 1/1 200step=1rpm
// 1 0 -> 180 1/2 400step=1rpm
// 0 1 -> 90 1/4 800step=1rpm
// 1 1 -> 45 1/8 1600step=1rpm
}
void loop()
{
t = millis(); if (t - u >= 250) {
int v0 = (analogRead(7));
v1 = pow(1.8, ((v0 / 64.0) - 1));
v1 = map(v1, 0, 6684, min_speed, max_speed);
lcd.setCursor(7, 0); lcd.print("Step: ");
lcd.setCursor(12, 0); lcd.print(v1);
u = millis();
}
v = millis(); if (v - w >= 1000) {
float v2 = (analogRead(6) * (5.0 / 1023) * 3) ; //for battery monitor
float v3 = (analogRead(1) * (5.0 / 1023) * 5.85) ; //for charg monitor
if (v3 >= 5.0) {
lcd.setCursor(0, 1); lcd.write(byte(2)); //show charg icon
lcd.print(v3, 2); lcd.print("V ");
if (v2 >= 8.6)lcd.write(byte(1)); //show battery full icon
}
else {
if (v2 < 4.6) {
lcd.setCursor(0, 1); lcd.print("USB link");
} else {
lcd.setCursor(0, 1); lcd.write(byte(0)); lcd.print(v2, 2); lcd.print("V ");
}
}
w = millis();
}
if ((digitalRead(sw_F) == LOW) && (digitalRead(sw_R) == LOW )) digitalWrite(slp, LOW); //空檔
else if ((digitalRead(sw_F) == HIGH) && (digitalRead(sw_R) == HIGH )) {
digitalWrite(slp, LOW); //空檔
lcd.setCursor(15, 1); lcd.print("N");
}
else if (digitalRead(sw_F) == LOW) {
lcd.setCursor(15, 1); lcd.print("F");
digitalWrite(slp, HIGH);
digitalWrite(dir, HIGH); //正轉
toneAC( v1);
}
else if (digitalRead(sw_R) == LOW) {
lcd.setCursor(15, 1); lcd.print("R");
digitalWrite(slp, HIGH);
digitalWrite(dir, LOW); //反轉
toneAC( v1);
}
}
//loop end~