目前我正在使用键盘控制我的BLDC电机,键入伺服值(0-180)。但我的主管需要我使用0-9000而不是0-180之间的值来控制它。我一直在学习map()
函数。不知何故,我不太明白,我目前仍然试图将其与我的代码合并。如何将(0-180)的伺服值映射到(0-9000)?这是我的代码:
#include <Servo.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// Set the LCD I2C address
Servo myservo;
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
//row pinouts of the keypad (L1, L2, L3, L4)
byte colPins[COLS] = {5, 4, 3};
//column pinouts of the keypad (R1, R2, R3)
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// STATE CONDITION FOR MAIN LOOP
enum { enter_values, spin , finish } systemstate;
//FOR MOTORSPIN
unsigned long previousMillis = 0;
long interval = 1000;
// RPM MEASUREMENT
const int dataIN = 2; //IR sensor INPUT
unsigned long prevmillis; // To store time
unsigned long duration; // To store time difference
unsigned long lcdrefresh; // To store time for lcd to refresh
int rpm; // RPM value
boolean currentstate; // Current state of IR input scan
boolean prevstate; // State of IR sensor in previous scan
// DECLARE
int stage1speed , stage1time , stage2speed , stage2time , stage3speed , stage3time ;
void setup() {
Serial.begin(9600);
lcd.begin(20, 4);
myservo.attach(11);
systemstate = enter_values; // set up the starting state
pinMode(dataIN, INPUT);
prevmillis = 0;
prevstate = LOW;
lcd.setCursor(4, 1);
lcd.print("SPIN COATER");
lcd.setCursor(6, 2);
lcd.print("MACHINE");
delay(5000);
lcd.clear();
lcd.setCursor(3, 1);
lcd.print("S = speed(sv)");
lcd.setCursor(3, 2);
lcd.print("T = time(sec)");
delay(5000);
lcd.clear();
lcd.setCursor(3, 1);
lcd.print("Range of Speed");
lcd.setCursor(3, 2);
lcd.print("0-180 sv");
delay(5000);
lcd.clear();
}
//FUNCTION FOR KEY IN SPEED AND TIME
void enter_speed_time() {
lcd.setCursor(0, 0);
lcd.print("S=");
lcd.setCursor(0, 1);
lcd.print("T=");
lcd.setCursor(0, 2);
lcd.print("S=");
lcd.setCursor(0, 3);
lcd.print("T=");
lcd.setCursor(10, 0);
lcd.print("S=");
lcd.setCursor(10, 1);
lcd.print("T=");
lcd.setCursor(10, 2);
lcd.print("RPM");
stage1speed = getTheNumber();
lcd.setCursor(2, 0);
lcd.print(stage1speed);
Serial.print(stage1speed);
lcd.print("sv");
stage1time = getTheNumber();
lcd.setCursor(2, 1);
lcd.print(stage1time);
Serial.print(stage1time);
lcd.print("sec");
stage2speed = getTheNumber();
lcd.setCursor(2, 2);
lcd.print(stage2speed);
Serial.print(stage2speed);
lcd.print("sv");
stage2time = getTheNumber();
lcd.setCursor(2, 3);
lcd.print(stage2time);
Serial.print(stage2time);
lcd.print("sec");
stage3speed = getTheNumber();
lcd.setCursor(12, 0);
lcd.print(stage3speed);
Serial.print(stage3speed);
lcd.print("sv");
stage3time = getTheNumber();
lcd.setCursor(12, 1);
lcd.print(stage3time);
Serial.print(stage3time);
lcd.print("sec");
}
// FUNCTION TO GET NUMBERS FROM 4X3 MATRIC KEYPAD
int getTheNumber() {
char buffer[4];
int i = 0;
while (1) {
char key = keypad.getKey();
if ('0' <= key && key <= '9' && i < 3) {
// If it's a number AND we have space left, add to our string
buffer[i] = key;
i++;
} else if ('#' == key && i > 0) {
// If it's a * or #, end
// Null terminate
buffer[i] = 0;
int value = atoi(buffer); // Convert to an integer
break;
}
}
return atoi(buffer);
}
// FUNCTION FOR RPM MEASUREMENT
void rpmMeasure() {
// RPM Measurement
currentstate = digitalRead(dataIN); // Read IR sensor state
if (prevstate != currentstate) {
// If there is change in input
if (currentstate == HIGH) {
// If input only changes from LOW to HIGH
duration = (micros() - prevmillis); // Time difference between revolution in microsecond
rpm = (60000000 / duration); // rpm = (1/ time millis)*1000*1000*60;
prevmillis = micros(); // store time for next revolution calculation
}
}
prevstate = currentstate; // store this scan (prev scan) data for next scan
lcd.setCursor(10, 3);
lcd.print(rpm);
}
// FUNCTION FOR MOTOR SPINNING ACCORDING TO TIME AND SPEED
void motorspin() {
char key = keypad.getKey();
unsigned long currentMillis = millis();
int idleValue = 0;
static enum { IDLE , STAGE0, STAGE1, STAGE2, STAGE3 } spinningstate = IDLE;
switch (spinningstate) {
case IDLE:
if (key == '*') {
Serial.print(key);
spinningstate = STAGE0;
}
break;
case STAGE0:
myservo.write(stage1speed);
Serial.print(stage1speed);
previousMillis = currentMillis;
spinningstate = STAGE1;
break;
case STAGE1:
if (currentMillis - previousMillis >= stage1time * interval) {
myservo.write(stage2speed);
Serial.print(stage2speed);
previousMillis = currentMillis;
spinningstate = STAGE2;
}
break;
case STAGE2:
if (currentMillis - previousMillis >= stage2time * interval) {
myservo.write(stage3speed);
Serial.print(stage3speed);
previousMillis = currentMillis;
spinningstate = STAGE3;
}
break;
case STAGE3:
if (currentMillis - previousMillis >= stage3time * interval) {
myservo.write(idleValue);
Serial.print(idleValue);
spinningstate = IDLE;
}
break;
default:
spinningstate = IDLE;
break;
}
}
// MAIN LOOP
void loop() {
char key = keypad.getKey();
switch (systemstate) {
case enter_values:
enter_speed_time();
systemstate = spin;
break;
case spin:
rpmMeasure();
motorspin();
if (key == '#') {
Serial.print(key);
systemstate = finish;
}
break;
case finish:
lcd.clear();
delay(1000);
systemstate = enter_values;
break;
default:
systemstate = enter_values;
}
delay(1);
}
答案 0 :(得分:0)
将值从0-180转换为0-9000
map(value,0,180,0,9000)
将0-9000转换为0-180
map(value,0,9000,0,180)