C++ 如何在Arduino中用电位计编码EEPROM
我有一个网站上的代码。我一直在尝试在代码中添加EEPROM,但我不能正确地得到它。我尝试添加EEPROM.write和EEPROM.read我还添加了另一个伺服 有人能帮我吗,在代码中添加EEPROM吗?提前谢谢:) 以下是代码(具有播放和录制功能的简单机械臂):C++ 如何在Arduino中用电位计编码EEPROM,c++,arduino,robotics,eeprom,servo,C++,Arduino,Robotics,Eeprom,Servo,我有一个网站上的代码。我一直在尝试在代码中添加EEPROM,但我不能正确地得到它。我尝试添加EEPROM.write和EEPROM.read我还添加了另一个伺服 有人能帮我吗,在代码中添加EEPROM吗?提前谢谢:) 以下是代码(具有播放和录制功能的简单机械臂): #包括 #包括 常量int PinButton1=2;//引脚2 常量int PinButton2=3;//引脚3 int mode=1;//案例1编程机器人手臂。案例2重播位置 int bounce=0; volatile int
#包括
#包括
常量int PinButton1=2;//引脚2
常量int PinButton2=3;//引脚3
int mode=1;//案例1编程机器人手臂。案例2重播位置
int bounce=0;
volatile int buttonPress=0;
无符号长lastButtonPressTime=0;
易失性无符号长bounceTime=0;
伺服系统//伺服对象
伺服臂1伺服;//我已将伺服系统重新编号为26 1 2017
伺服臂2伺服;
伺服臂3SERVO;
伺服臂4伺服;
int pos1,pos2,pos3,pos4,pos0;
int PosArm[5];//排列
int ArmPos[100][5];//阵列可将手臂位置保持在最多100个位置
int PosCount=0;//统计按下按钮时增加的位置数
int PosCountMax=0;//双击按钮启动重播模式时记录的位置数
int=0;//用于检查手臂是否已从一个记录位置移动到下一个位置
int addr=0;
布尔值=假;
无效设置(){
对于(inti=0;i175)PosArm[i]=175;//servo.write limited 5-175
}
Arm0Servo.write(PosArm[0]);
Arm1Servo.write(PosArm[1]);
Arm2Servo.write(PosArm[2]);
Arm3Servo.write(PosArm[3]);
Arm4Servo.write(PosArm[4]);
Play();
延误(5);
}
void Play(){//将臂位置数据写入伺服
对于(int i=0;i<5;i++){
如果(PosArm[i]<5)PosArm[i]=5;//限制伺服运动以防止撞到末端止动块
如果(PosArm[i]>175)PosArm[i]=175;//servo.write limited 5-175
}
if(addr==0){
PosArm[0]=EEPROM.read(地址);
pos0=EEPROM读取(addr+5);
addr++;
PosArm[1]=EEPROM.read(地址);
pos1=EEPROM读取(addr+5);
addr++;
PosArm[2]=EEPROM.read(地址);
pos2=EEPROM读取(addr+5);
addr++;
PosArm[3]=EEPROM.read(addr);
pos3=EEPROM读取(addr+5);
addr++;
PosArm[4]=EEPROM.read(地址);
pos4=EEPROM读取(addr+5);
addr++;
}
}
void ButtonPress(){//中断以捕获按钮按下
如果(micros()>(bounceTime+3000)){//debounce计时器
bounceTime=micros();//ingnore因按钮反弹而中断
buttonPress=1;//按钮按下的标志
}
}由于以下问题,您发布的代码将永远不会尝试从EEPROM读取数据: 启动微控制器时,变量
mode
设置为1。如果仔细查看循环
功能,您可以看到您的代码将进入案例1
分支至少一次,在那里它基本上将数据写入EEPROM,更重要的是,由于addr++
指令,将addr
设置为0以外的值(除非您运行代码的时间太长,以致于遇到整数溢出)
您尝试从EEPROM读取的唯一位置是
播放
功能。由于此功能中的if(addr==0)
条件,您将永远不会执行EEPROM。读取
指令,因为addr
不会如上文所述为0。您说的“添加EEPROM”是什么意思?您的程序可以写入和/或读取,您想做什么?到目前为止您尝试了什么?程序运行良好,它可以写入和读取,但我想将写入数据永久保存在EEPROM中。我尝试了EEPROM。写入和EEPROM。读取,它仍然工作,但无法读取写入数据。谢谢:)那么你的代码在哪里呢?我将编辑上面发布的代码。请减少你的代码示例!这不是你工作的审查中心!如果您有像写入eeprom这样的问题,请将您的代码精确地减少到这个程度!约翰尼斯是对的。此外,除非进入模式2或3,否则该草图将每秒写入EEPROM 1000多次。这将使您的EEPROM的使用寿命不到2小时,达到100000个细胞周期,因为ATMEL是寿命。所以你真的想重新处理EEPROM。你的Arduino可能已经有一个死掉的EEPROM了。
#include <Servo.h>
#include <EEPROM.h>
const int PinButton1 = 2; // pin 2
const int PinButton2 = 3; // pin 3
int mode = 1; // case 1 program robot arm. case 2 replay positions
int bounce = 0;
volatile int buttonPress = 0;
unsigned long lastButtonPressTime = 0;
volatile unsigned long bounceTime = 0;
Servo Arm0Servo; //servo objects
Servo Arm1Servo; // Ihave renumbered servos 26 1 2017
Servo Arm2Servo;
Servo Arm3Servo;
Servo Arm4Servo;
int pos1,pos2,pos3,pos4,pos0;
int PosArm[5] ; // array
int ArmPos[100][5]; // array to hold arm positions up to 100
int PosCount = 0; // to count number of positions increased when button pressed
int PosCountMax = 0; // number of positions recorded when double button push initiates replay mode
int PosReached = 0; // used to check that arm has moved from one recorded position to next position
int addr = 0;
boolean recorded = false;
void setup() {
for(int i = 0; i <100 ; i++ ){
for(int p = 0; p <5 ; p++ ){
ArmPos[i][p] = -1;
}
}
pinMode(PinButton1 , OUTPUT);
digitalWrite(PinButton1 ,LOW);
pinMode(PinButton2, INPUT_PULLUP);
// I have made a small change here due to problem using some Arduinos
//attachInterrupt( digitalPinToInterrupt(PinButton2),ButtonPress , LOW );
// digitalPinToInterrupt(PinButton2) may not be defined!
attachInterrupt( 1,ButtonPress , LOW ); // interupt to capture button presses
// attach servos to relervent pins on arduino nano
Arm0Servo.attach(12); // grip 90 to 180 open limits of servo movement
Arm1Servo.attach(11); // elbow to 130 up
Arm2Servo.attach(10); // shoulder 10 to 50 down
Arm3Servo.attach(9); // turn 0 to 180 right
Arm4Servo.attach(8); // turn 0 to 180 right
}
void loop() {
switch(mode){
case 1 : // program robot arm. 1 press to remember position. 2 presses to progress next case 2 replay mode
// analogRead(pin) that reads poteniometers on training arm
PosArm[0] = map(analogRead(0),480,1024,180,10); // map (480,1024 value from potentiometer to 180,90 value sent to servo)
EEPROM.write(addr, PosArm[0]);
addr++;
PosArm[1] = map(analogRead(1),480,1024,180,10);
EEPROM.write(addr, PosArm[1]);
addr++;
PosArm[2] = map(analogRead(2),480,1024,180,10);
EEPROM.write(addr, PosArm[2]);
addr++;
PosArm[3] = map(analogRead(3),480,1024,180,10);
EEPROM.write(addr, PosArm[3]);
addr++;
PosArm[4] = map(analogRead(4),480,1024,180,10);
EEPROM.write(addr, PosArm[4]);
addr++;
MoveArm(); // call method
if(buttonPress == 1){ // flag set by interupt when button is pressed
buttonPress = 0; // reset flag
if( millis() > (lastButtonPressTime + 1000)){ // only one button press in one secound
// record position of arm PosArm to array[100][] of armpositions
ArmPos[PosCount][0] = PosArm[0];
ArmPos[PosCount][1] = PosArm[1];
ArmPos[PosCount][2] = PosArm[2];
ArmPos[PosCount][3] = PosArm[3];
ArmPos[PosCount][4] = PosArm[4];
if (addr == 512) {
EEPROM.write(addr, 255);
break;
}
if( PosCount < 100) { // stop recording if over 100 positions recorded (memory limitations)
PosCount++;
}
}else{ // more than one button press
mode = 2; // go to next phase
PosCountMax = PosCount; // set number of arm positions recorded
PosCount = 0; // reset count ready to read arm position array from begining
}
lastButtonPressTime = millis();
}
break;
case 2 : // read arm position array
PosReached = 0;
for(int i = 0; i <5 ; i++ ){ //adjust servo positions
// we move the servos in small steps and the delay(20) makes arm motion smooth and slow
if( PosArm[i] > ArmPos[PosCount][i]) PosArm[i] = PosArm[i] - 1; // if actual position is greater than requird position reduce position by 1
if( PosArm[i] < ArmPos[PosCount][i]) PosArm[i] = PosArm[i] + 1; // if actual position is less than required position value increase position valuue by 1
if( PosArm[i] == ArmPos[PosCount][i]) PosReached++; // check if servo has reached required position/angle
}
if(PosReached == 5) PosCount++; // if all 4 servos have reached position then increase array index (PosCount)to next position in array
if(PosCount == PosCountMax) PosCount = 0; // if end of array reached reset index to 0 and repeat.
Play(); // physically move arm to updated position, this is broken into small steps
delay(5); // pause between moves so over all motion is slowed down
break;
default :
break;
}
}
void MoveArm() { // write arm position data to servos
for (int i = 0 ; i < 5 ; i++) {
if (PosArm[i] < 5) PosArm[i] = 5; // limit servo movement to prevent hitting end stops
if (PosArm[i] > 175) PosArm[i] = 175; // servo.write limited 5 - 175
}
Arm0Servo.write(PosArm[0]);
Arm1Servo.write(PosArm[1]);
Arm2Servo.write(PosArm[2]);
Arm3Servo.write(PosArm[3]);
Arm4Servo.write(PosArm[4]);
Play();
delay(5);
}
void Play() { // write arm position data to servos
for (int i = 0 ; i < 5 ; i++) {
if (PosArm[i] < 5) PosArm[i] = 5; // limit servo movement to prevent hitting end stops
if (PosArm[i] > 175) PosArm[i] = 175; // servo.write limited 5 - 175
}
if (addr == 0) {
PosArm[0] = EEPROM.read(addr);
pos0 = EEPROM.read(addr+5);
addr++;
PosArm[1] = EEPROM.read(addr);
pos1 = EEPROM.read(addr+5);
addr++;
PosArm[2] = EEPROM.read(addr);
pos2 = EEPROM.read(addr+5);
addr++;
PosArm[3] = EEPROM.read(addr);
pos3 = EEPROM.read(addr+5);
addr++;
PosArm[4] = EEPROM.read(addr);
pos4 = EEPROM.read(addr+5);
addr++;
}
}
void ButtonPress(){ // interupt to capture button press
if(micros() > (bounceTime + 3000)){ // debounce timer
bounceTime = micros(); // ingnore interupts due to button bounce
buttonPress = 1; // flag for button pressed
}