Button Arduino渐变映射值

Button Arduino渐变映射值,button,map,arduino,Button,Map,Arduino,我正在做一个项目,我需要根据开关位置调整映射值,但我想在写入新的模拟数据时将其渐变 举个例子,假设我们想让一个按钮输出不同电压的串行机器人控制器。据我所知,它看起来是这样的: if( adjenpos == 1) { boostbtnout = map(boostbtnin, 0, 1023, 0, 51); } else if( adjenpos == 2) { boostbtnout = map(boostbtnin, 0, 1023, 0, 102); } else if( adjenp

我正在做一个项目,我需要根据开关位置调整映射值,但我想在写入新的模拟数据时将其渐变

举个例子,假设我们想让一个按钮输出不同电压的串行机器人控制器。据我所知,它看起来是这样的:

 if( adjenpos == 1) {
boostbtnout = map(boostbtnin, 0, 1023, 0, 51);
}
else if( adjenpos == 2) {
boostbtnout = map(boostbtnin, 0, 1023, 0, 102);
}
else if( adjenpos == 3) {
boostbtnout = map(boostbtnin, 0, 1023, 0, 153);
}
else if( adjenpos == 4) {
boostbtnout = map(boostbtnin, 0, 1023, 0, 204);
}
else if( adjenpos == 5) {
boostbtnout = map(boostbtnin, 0, 1023, 0, 255);
}
现在有了选项3,拿一个开/关值为0或153的按钮,我想在规定的速率上渐变0到153的值,而不仅仅是开或关

我的假设是在循环中执行类似的操作:

boostramp = 1
boostbtnvalue = analogRead(boostbtnout)
 analogWrite(boostbtn, boostbtnvalue);
 if ( boostpwr <= boostbtnout){
 boostpwr = boostpwr + boostramp;
我知道这是不正确的,但是如何将新映射值注入到一个渐变类型的函数中呢


谢谢

因此,如果我是正确的,您希望在每个开关位置增加电压,但要缓慢地增加电压,以便您的设备不会飞到它。假设这是对的,那么您需要这样的东西:

 if( adjenpos == 1) {
boostbtnout = map(boostbtnin, 0, 1023, 0, 51);
}
else if( adjenpos == 2) {
boostbtnout = map(boostbtnin, 0, 1023, 0, 102);
}
else if( adjenpos == 3) {
boostbtnout = map(boostbtnin, 0, 1023, 0, 153);
}
else if( adjenpos == 4) {
boostbtnout = map(boostbtnin, 0, 1023, 0, 204);
}
else if( adjenpos == 5) {
boostbtnout = map(boostbtnin, 0, 1023, 0, 255);
}
首先,我将创建一个函数,将位置获取到一个变量pos,然后您可以使用该函数完成其余操作

int pos;
int target;
int currentPower = 0;

if(pos == 0){ target = 100
}
else if(pos == 1){target =200
}
else if(pos == 2){ target = 300
}
if(target > currentPower){
while(currentPower < target){
currentPower++;
delay(10); //tweak to change ramp speed
}
}
if(target < currentPower){
while(currentPower > target){
currentPower--;
delay(10);
}
}
显然,将值更改为您需要的值,但这就是想法。首先通过任何方式将位置设置为0,然后确定目标,然后通过while循环增加目标