Aggiungo il codice qui:
#include <TM1637Display.h>
#include <Servo.h>
#include <Encoder.h>
#include <EEPROM.h>
const int pedalswitch1 = 22;
const int pedalswitch2 = 23;
const int pedalswitch3 = 24;
const int pedalswitch4 = 25;
const int PRESETSWITCH1= 26;
const int PRESETSWITCH2= 27;
const int PRESETSWITCH3= 28;
const int PRESETSWITCH4= 29;
const int PINLED1 = 2;
const int PINLED2 = 3;
const int PINLED3 = 30;
const int PINLED4 = 31;
byte
currSwitch1,
lastSwitch1;
unsigned long
timeStart1;
bool
bCheckingSwitch1;
byte
currSwitch2,
lastSwitch2;
unsigned long
timeStart2;
bool
bCheckingSwitch2;
byte
currSwitch3,
lastSwitch3;
unsigned long
timeStart3;
bool
bCheckingSwitch3;
byte
currSwitch4,
lastSwitch4;
unsigned long
timeStart4;
bool
bCheckingSwitch4;
Encoder ENC1(7, 6);
Encoder ENC2(5, 4);
Encoder ENC3(13, 12);
const int homeposition1 = 90;
const int homeposition2 = 90;
const int homeposition3 = 90;
const int stepValue = 1;
Servo SERVO1;
Servo SERVO2;
Servo SERVO3;
int servoAngle1 =homeposition1;
int servoAngle2 =homeposition2;
int servoAngle3 =homeposition3;
int displaynumber1;
int displaynumber2;
int displaynumber3;
#define CLK1 32
#define DIO1 33
#define CLK2 34
#define DIO2 35
#define CLK3 36
#define DIO3 37
TM1637Display display1(CLK1, DIO1);
TM1637Display display2(CLK2, DIO2);
TM1637Display display3(CLK3, DIO3);
void setup() {
Serial.begin(9600);
SERVO1.attach(9);
SERVO2.attach(10);
SERVO3.attach(11);
SERVO1.write(servoAngle1);
SERVO2.write(servoAngle2);
SERVO3.write(servoAngle3);
display1.setBrightness(0x0f);
display2.setBrightness(0x0f);
display3.setBrightness(0x0f);
pinMode( PINLED1, OUTPUT );
digitalWrite( PINLED1, LOW );
pinMode( PRESETSWITCH1, INPUT_PULLUP );
lastSwitch1 = digitalRead( PRESETSWITCH1);
bCheckingSwitch1 = false;
{ pinMode( PINLED2, OUTPUT );
digitalWrite( PINLED2, LOW );
pinMode( PRESETSWITCH2, INPUT_PULLUP );
lastSwitch2 = digitalRead( PRESETSWITCH2);
bCheckingSwitch2 = false;}
{ pinMode( PINLED3, OUTPUT );
digitalWrite( PINLED3, LOW );
pinMode( PRESETSWITCH3, INPUT_PULLUP );
lastSwitch3 = digitalRead( PRESETSWITCH3);
bCheckingSwitch3 = false;}
{ pinMode( PINLED4, OUTPUT );
digitalWrite( PINLED4, LOW );
pinMode( PRESETSWITCH4, INPUT_PULLUP );
lastSwitch4 = digitalRead( PRESETSWITCH4);
bCheckingSwitch4 = false;}
}
long oldposition1 = -999;
long oldposition2 = -999;
long oldposition3 = -999;
void loop() {
long newPosition1 = ENC1.read();
if (newPosition1 != oldposition1) {
if(newPosition1 > oldposition1)
{
servoAngle1 -= stepValue;
if(servoAngle1 <0)
servoAngle1 =0;
SERVO1.write(servoAngle1);
}
if(newPosition1 < oldposition1 )
{
servoAngle1 += stepValue;
if(servoAngle1 >180)
servoAngle1 =180;
SERVO1.write(servoAngle1);
}
display1.showNumberDecEx(servoAngle1, 0b01000000, false, 3, 1);
oldposition1 = newPosition1;//remember the new position
}
{
long newPosition2 = ENC2.read();
if (newPosition2 != oldposition2) {
if(newPosition2 > oldposition2)
{
servoAngle2 -= stepValue;
if(servoAngle2 <0)
servoAngle2 =0;
SERVO2.write(servoAngle2);
}
if(newPosition2 < oldposition2 )
{
servoAngle2 += stepValue;
if(servoAngle2 >180)
servoAngle2 =180;
SERVO2.write(servoAngle2);
}
display2.showNumberDecEx(servoAngle2, 0b01000000, false, 3, 1);
oldposition2 = newPosition2;//remember the new position
}
{
long newPosition3 = ENC3.read();
if (newPosition3 != oldposition3) {
if(newPosition3 > oldposition3)
{
servoAngle3 -= stepValue;
if(servoAngle3 <0)
servoAngle3 =0;
SERVO3.write(servoAngle3);
}
if(newPosition3 < oldposition3 )
{
servoAngle3 += stepValue;
if(servoAngle3 >180)
servoAngle3 =180;
SERVO3.write(servoAngle3);
}
display3.showNumberDecEx(servoAngle3, 0b01000000, false, 3, 1);
oldposition3 = newPosition3;
}
{{currSwitch1 = digitalRead( PRESETSWITCH1);
if( currSwitch1 != lastSwitch1 )
{if( currSwitch1 == LOW )
{timeStart1 = millis();
bCheckingSwitch1 = true;}
else
{bCheckingSwitch1 = false;}
lastSwitch1 = currSwitch1;}
if( bCheckingSwitch1 )
{
if( (millis() - timeStart1 ) >= 2000 )
{digitalWrite( PINLED1, HIGH );
EEPROM.write(0, servoAngle1);
EEPROM.write(1, servoAngle2);
EEPROM.write(2, servoAngle3);}}
else digitalWrite( PINLED1, LOW );}}
{{currSwitch2 = digitalRead( PRESETSWITCH2);
if( currSwitch2 != lastSwitch2 )
{if( currSwitch2 == LOW )
{timeStart2 = millis();
bCheckingSwitch2 = true;}
else
{bCheckingSwitch2 = false;}
lastSwitch2 = currSwitch2;}
if( bCheckingSwitch2 )
{
if( (millis() - timeStart2 ) >= 2000 )
{digitalWrite( PINLED2, HIGH );
EEPROM.write(3, servoAngle1);
EEPROM.write(4, servoAngle2);
EEPROM.write(5, servoAngle3);}}
else digitalWrite( PINLED2, LOW );}}
{{currSwitch3 = digitalRead( PRESETSWITCH3);
if( currSwitch3 != lastSwitch3 )
{if( currSwitch3 == LOW )
{timeStart3 = millis();
bCheckingSwitch3 = true;}
else
{bCheckingSwitch3 = false;}
lastSwitch3 = currSwitch3;}
if( bCheckingSwitch3 )
{
if( (millis() - timeStart3 ) >= 2000 )
{digitalWrite( PINLED3, HIGH );
EEPROM.write(6, servoAngle1);
EEPROM.write(7, servoAngle2);
EEPROM.write(8, servoAngle3);}}
else digitalWrite( PINLED3, LOW );}}
{{currSwitch4 = digitalRead( PRESETSWITCH4);
if( currSwitch4 != lastSwitch4 )
{if( currSwitch4 == LOW )
{timeStart4 = millis();
bCheckingSwitch4 = true;}
else
{bCheckingSwitch4 = false;}
lastSwitch4 = currSwitch4;}
if( bCheckingSwitch4 )
{
if( (millis() - timeStart4 ) >= 2000 )
{digitalWrite( PINLED4, HIGH );
EEPROM.write(9, servoAngle1);
EEPROM.write(10, servoAngle2);
EEPROM.write(11, servoAngle3);}}
else digitalWrite( PINLED4, LOW );}}
{pinMode(pedalswitch1, INPUT_PULLUP);
if(digitalRead(pedalswitch1) == LOW)
SERVO1.write(EEPROM.read(0)); // tell servo to go to position in variable 'pos'
if (digitalRead(pedalswitch1) == LOW)
SERVO2.write(EEPROM.read(1)); // tell servo to go to position in variable 'pos'
if (digitalRead(pedalswitch1) == LOW)
SERVO3.write(EEPROM.read(2));
pinMode(pedalswitch2, INPUT_PULLUP);
if(digitalRead(pedalswitch2) == LOW)
SERVO1.write(EEPROM.read(3)); // tell servo to go to position in variable 'pos'
if (digitalRead(pedalswitch2) == LOW)
SERVO2.write(EEPROM.read(4)); // tell servo to go to position in variable 'pos'
if (digitalRead(pedalswitch2) == LOW)
SERVO3.write(EEPROM.read(5));
pinMode(pedalswitch3, INPUT_PULLUP);
if(digitalRead(pedalswitch3) == LOW)
SERVO1.write(EEPROM.read(6)); // tell servo to go to position in variable 'pos'
if (digitalRead(pedalswitch3) == LOW)
SERVO2.write(EEPROM.read(7)); // tell servo to go to position in variable 'pos'
if (digitalRead(pedalswitch3) == LOW)
SERVO3.write(EEPROM.read(8));
pinMode(pedalswitch4, INPUT_PULLUP);
if(digitalRead(pedalswitch4) == LOW)
SERVO1.write(EEPROM.read(9)); // tell servo to go to position in variable 'pos'
if (digitalRead(pedalswitch4) == LOW)
SERVO2.write(EEPROM.read(10)); // tell servo to go to position in variable 'pos'
if (digitalRead(pedalswitch4) == LOW)
SERVO3.write(EEPROM.read(11)); // tell servo to go to position in variable 'pos'
}
}
}
}