Buonasera a tutti, ho una domanda per voi.
Ho scritto un semplice programma che scrive la posizione di 4 servo motori servo nella eprom di arduino ogni qualvolta il motore si muove.
la posizione viene letta dalla eprom nella procedura di startup() e viene scritta nella porta pwm prima della funzione attach().
questo per evitare spostamenti di default dei servomotori al riavvio di arduino.
il problema è che su arduino UNO funziona correttamente, invece su arduino NANO EVERY no, ovvero le letture della eprom sono (a volte) errate.
#include <Servo.h>
#include <EEPROM.h>
const int servo01_MinDegrees = 1000; // motore 1 : piattaforma
const int servo01_MidDegrees = 1200;
const int servo01_MaxDegrees = 1400;
const int servo01_Interval = 40;
int servo01_Degrees = 1;
int servo01_Position = servo01_MidDegrees;
unsigned long servo01_previousMillis = 0;
const int servo02_MinDegrees = 1000; // motore 2 : bancone
const int servo02_MidDegrees = 1200;
const int servo02_MaxDegrees = 1400;
const int servo02_Interval = 30;
int servo02_Degrees = 1;
int servo02_Position = servo02_MidDegrees;
unsigned long servo02_previousMillis = 0;
const int servo03_MinDegrees = 1000; // motore 1 : bicchieri
const int servo03_MidDegrees = 1200;
const int servo03_MaxDegrees = 1400;
const int servo03_Interval = 20;
int servo03_Degrees = 1;
int servo03_Position = servo03_MidDegrees;
unsigned long servo03_previousMillis = 0;
const int servo04_MinDegrees = 1000; // motore 1 : gioconda
const int servo04_MidDegrees = 1200;
const int servo04_MaxDegrees = 1400;
const int servo04_Interval = 10;
int servo04_Degrees = 1;
int servo04_Position = servo04_MidDegrees;
unsigned long servo04_previousMillis = 0;
Servo servo01; // create servo object to control a servo
Servo servo02; // create servo object to control a servo
Servo servo03; // create servo object to control a servo
Servo servo04; // create servo object to control a servo
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
int eprom01 = servo01_MidDegrees;
int eprom02 = servo02_MidDegrees;
int eprom03 = servo03_MidDegrees;
int eprom04 = servo04_MidDegrees;
void setup() {
Serial.begin(9600);
delay(100);
eprom01 = (EEPROM.read(1) * 10) + EEPROM.read(11);
eprom02 = (EEPROM.read(2) * 10) + EEPROM.read(12);
eprom03 = (EEPROM.read(3) * 10) + EEPROM.read(13);
eprom04 = (EEPROM.read(4) * 10) + EEPROM.read(14);
if ((eprom01 > servo01_MinDegrees) && (eprom01 < servo01_MaxDegrees)) {
servo01_Position = eprom01;
} else {
servo01_Position = servo01_MidDegrees;
}
if ((eprom02 > servo02_MinDegrees) && (eprom02 < servo02_MaxDegrees)) {
servo02_Position = eprom02;
} else {
servo02_Position = servo02_MidDegrees;
}
if ((eprom03 > servo03_MinDegrees) && (eprom03 < servo03_MaxDegrees)) {
servo03_Position = eprom03;
} else {
servo03_Position = servo03_MidDegrees;
}
if ((eprom04 > servo04_MinDegrees) && (eprom04 < servo04_MaxDegrees)) {
servo04_Position = eprom04;
} else {
servo04_Position = servo04_MidDegrees;
}
Serial.println("*** Starting BlaBlaBar ***"); // so we know what sketch is running
Serial.print("servo 01: ");
Serial.print(eprom01);
Serial.print(" - ");
Serial.println(servo01_Position);
Serial.print("servo 02: ");
Serial.print(eprom02);
Serial.print(" - ");
Serial.println(servo02_Position);
Serial.print("servo 03: ");
Serial.print(eprom03);
Serial.print(" - ");
Serial.println(servo03_Position);
Serial.print("servo 04: ");
Serial.print(eprom04);
Serial.print(" - ");
Serial.println(servo04_Position);
servo01.writeMicroseconds(servo01_Position); // sets the initial position
servo02.writeMicroseconds(servo02_Position); // sets the initial position
servo03.writeMicroseconds(servo03_Position); // sets the initial position
servo04.writeMicroseconds(servo04_Position); // sets the initial position
delay(100);
servo01.attach(3);
servo02.attach(6);
servo03.attach(9);
servo04.attach(10);
}
void loop() {
currentMillis = millis();
servo01_Sweep();
servo02_Sweep();
servo03_Sweep();
servo04_Sweep();
}
void servo01_Sweep() {
if (currentMillis - servo01_previousMillis >= servo01_Interval) {
servo01_previousMillis += servo01_Interval;
servo01_Position = servo01_Position + servo01_Degrees;
if ((servo01_Position >= servo01_MaxDegrees) || (servo01_Position <= servo01_MinDegrees)) {
servo01_Degrees = -servo01_Degrees;
servo01_Position = servo01_Position + servo01_Degrees;
}
servo01.writeMicroseconds(servo01_Position);
EEPROM.write(1, servo01_Position / 10);
EEPROM.write(11, servo01_Position % 10);
}
}
void servo02_Sweep() {
if (currentMillis - servo02_previousMillis >= servo02_Interval) {
servo02_previousMillis += servo02_Interval;
servo02_Position = servo02_Position + servo02_Degrees;
if ((servo02_Position >= servo02_MaxDegrees) || (servo02_Position <= servo02_MinDegrees)) {
servo02_Degrees = -servo02_Degrees;
servo02_Position = servo02_Position + servo02_Degrees;
}
servo02.writeMicroseconds(servo02_Position);
EEPROM.write(2, servo02_Position / 10);
EEPROM.write(12, servo02_Position % 10);
}
}
void servo03_Sweep() {
if (currentMillis - servo03_previousMillis >= servo03_Interval) {
servo03_previousMillis += servo03_Interval;
servo03_Position = servo03_Position + servo03_Degrees;
if ((servo03_Position >= servo03_MaxDegrees) || (servo03_Position <= servo03_MinDegrees)) {
servo03_Degrees = -servo03_Degrees;
servo03_Position = servo03_Position + servo03_Degrees;
}
servo03.writeMicroseconds(servo03_Position);
EEPROM.write(3, servo03_Position / 10);
EEPROM.write(13, servo03_Position % 10);
}
}
void servo04_Sweep() {
if (currentMillis - servo04_previousMillis >= servo04_Interval) {
servo04_previousMillis += servo04_Interval;
servo04_Position = servo04_Position + servo04_Degrees;
if ((servo04_Position >= servo04_MaxDegrees) || (servo04_Position <= servo04_MinDegrees)) {
servo04_Degrees = -servo04_Degrees;
servo04_Position = servo04_Position + servo04_Degrees;
}
servo04.writeMicroseconds(servo04_Position);
EEPROM.write(4, servo04_Position / 10);
EEPROM.write(14, servo04_Position % 10);
}
}