#include <EEPROM.h>
const byte servoin = 2;
const byte servo1 = 6;
const byte servo2 = 5;
const byte servo3 = 4;
const byte servo4 = 3;
const byte mode1 = 14;
const byte mode2 = 15;
const byte decrease = 16;
const byte increase = 17;
const byte centerLED = 18;
const byte epa1LED = 12;
const byte epa2LED = 11;
const byte servo1LED = 10;
const byte servo2LED = 9;
const byte servo3LED = 8;
const byte servo4LED = 7;
const byte editstep = 1;
volatile int servoinPWM = 1500;
volatile int servo1PWM = 1500;
volatile int servo2PWM = 1500;
volatile int servo3PWM = 1500;
volatile int servo4PWM = 1500;
volatile int intcounter = 0;
volatile byte sregvalue;
long x;
volatile byte progflag = 0;
volatile byte keyflag = 0;
byte eepromerror = 0;
byte hibyte;
byte lobyte;
unsigned long timer;
int servo1cnt;
unsigned int servo1epa1;
unsigned int servo1epa2;
int servo2cnt;
unsigned int servo2epa1;
unsigned int servo2epa2;
int servo3cnt;
unsigned int servo3epa1;
unsigned int servo3epa2;
int servo4cnt;
unsigned int servo4epa1;
unsigned int servo4epa2;
volatile byte LED = 0;
byte keypress = B00111111;
int key = 0;
int key1 = 0;
int key2 = 0;
int b = 0;
const int debounce = 200;
const int bounce = 160;
void timerinit()
{
noInterrupts(); // disable global interrupts
TCCR2A = 0; // prescaler 256
TCCR2B = 0;
TCCR2B |= (0 << CS20);
TCCR2B |= (1 << CS21);
TCCR2B |= (1 << CS22);
TCNT2 = 0;
TIMSK2 |= (1 << TOIE2); // enable global interrupts
interrupts();
}
ISR(TIMER2_OVF_vect)
{
sregvalue = SREG;
intcounter++;
if (intcounter > 4)
{
intcounter = 0;
if (progflag == 1)
{
x = servoinPWM - 1500;
if (x < 0)
{
servo1PWM = 1500 + ((x * servo1epa1) / 700) + servo1cnt;
servo2PWM = 1500 + ((x * servo2epa1) / 700) + servo2cnt;
servo3PWM = 1500 + ((x * servo3epa1) / 700) + servo3cnt;
servo4PWM = 1500 + ((x * servo4epa1) / 700) + servo4cnt;
}
else
{
servo1PWM = ((x * servo1epa2) / 700) + 1500 + servo1cnt;
servo2PWM = ((x * servo2epa2) / 700) + 1500 + servo2cnt;
servo3PWM = ((x * servo3epa2) / 700) + 1500 + servo3cnt;
servo4PWM = ((x * servo4epa2) / 700) + 1500 + servo4cnt;
}
}
servoout();
}
SREG = sregvalue;
}
void setup()
{
pinMode(mode1, INPUT_PULLUP);
pinMode(mode2, INPUT_PULLUP);
pinMode(decrease, INPUT_PULLUP);
pinMode(increase, INPUT_PULLUP);
pinMode(centerLED, OUTPUT);
pinMode(epa1LED, OUTPUT);
pinMode(epa2LED, OUTPUT);
pinMode(servo1LED, OUTPUT);
pinMode(servo2LED, OUTPUT);
pinMode(servo3LED, OUTPUT);
pinMode(servo4LED, OUTPUT);
pinMode(servo1, OUTPUT);
pinMode(servo2, OUTPUT);
pinMode(servo3, OUTPUT);
pinMode(servo4, OUTPUT);
if ((digitalRead(mode1) == 0) && (digitalRead(mode2) == 0))
{
keyflag = 1;
}
delay(1000);
if ((digitalRead(mode1) == 0) && (digitalRead(mode2) == 0) && keyflag == 1)
{
progflag = 1;
flashleds2();
}
else
{
flashleds();
}
EEPROMtest();
if (eepromerror > 0)
{
while (eepromerror > 0)
{
digitalWrite(servo1LED, HIGH);
digitalWrite(servo2LED, HIGH);
digitalWrite(servo3LED, HIGH);
digitalWrite(servo4LED, HIGH);
delay(300);
digitalWrite(servo1LED, LOW);
digitalWrite(servo2LED, LOW);
digitalWrite(servo3LED, LOW);
digitalWrite(servo4LED, LOW);
delay(300);
}
}
EEPROMoku();
delay(100);
timerinit();
}
void loop()
{
if (progflag == 0)
{
noInterrupts();
digitalWrite(servo1LED, HIGH);
digitalWrite(servo2LED, HIGH);
digitalWrite(servo3LED, HIGH);
digitalWrite(servo4LED, HIGH);
normal();
}
else
{
interrupts();
progmode();
}
}
void normal()
{
servoinPWM = pulseIn(servoin, HIGH, 21000);
x = servoinPWM - 1500;
if (x < 0)
{
servo1PWM = 1500 + ((x * servo1epa1) / 700) + servo1cnt;
servo2PWM = 1500 + ((x * servo2epa1) / 700) + servo2cnt;
servo3PWM = 1500 + ((x * servo3epa1) / 700) + servo3cnt;
servo4PWM = 1500 + ((x * servo4epa1) / 700) + servo4cnt;
}
else
{
servo1PWM = ((x * servo1epa2) / 700) + 1500 + servo1cnt;
servo2PWM = ((x * servo2epa2) / 700) + 1500 + servo2cnt;
servo3PWM = ((x * servo3epa2) / 700) + 1500 + servo3cnt;
servo4PWM = ((x * servo4epa2) / 700) + 1500 + servo4cnt;
}
if (servoinPWM >= 800 && servoinPWM <= 2200) servoout();
}
void progmode()
{
int servosira = 1;
int islevsira = 1;
int pressedkey = 0; // 0: Key yok, 1: Mode1(servo), 2: Mode2(işlev), 3: Decrease, 4: Increase, 5: Exit
while (pressedkey < 5)
{
noInterrupts();
switch (islevsira)
{
case 1:
servo1PWM = 1500 + servo1cnt;
servo2PWM = 1500 + servo2cnt;
servo3PWM = 1500 + servo3cnt;
servo4PWM = 1500 + servo4cnt;
servoinPWM = 1500;
break;
case 2:
servo1PWM = 1500 + servo1cnt - servo1epa1;
servo2PWM = 1500 + servo2cnt - servo2epa1;
servo3PWM = 1500 + servo3cnt - servo3epa1;
servo4PWM = 1500 + servo4cnt - servo4epa1;
servoinPWM = 1000;
break;
case 3:
servo1PWM = 1500 + servo1cnt + servo1epa2;
servo2PWM = 1500 + servo2cnt + servo2epa2;
servo3PWM = 1500 + servo3cnt + servo3epa2;
servo4PWM = 1500 + servo4cnt + servo4epa2;
servoinPWM = 2000;
break;
}
interrupts();
pressedkey = button();
switch (pressedkey)
{
case 0:
break;
case 1:
servosira++;
if (servosira > 4) servosira = 1;
break;
case 2:
islevsira++;
if (islevsira > 3) islevsira = 1;
break;
case 3:
if (servosira == 1)
{
switch (islevsira)
{
case 1:
servo1cnt = servo1cnt - editstep;
if (servo1cnt < (-200)) servo1cnt = -200;
break;
case 2:
servo1epa1 = servo1epa1 - editstep;
if (servo1epa1 < 1) servo1epa1 = 1;
break;
case 3:
servo1epa2 = servo1epa2 - editstep;
if (servo1epa2 < 1) servo1epa2 = 1;
break;
}
}
if (servosira == 2)
{
switch (islevsira)
{
case 1:
servo2cnt = servo2cnt - editstep;
if (servo2cnt < (-200)) servo2cnt = -200;
break;
case 2:
servo2epa1 = servo2epa1 - editstep;
if (servo2epa1 < 1) servo2epa1 = 1;
break;
case 3:
servo2epa2 = servo2epa2 - editstep;
if (servo2epa2 < 1) servo2epa2 = 1;
break;
}
}
if (servosira == 3)
{
switch (islevsira)
{
case 1:
servo3cnt = servo3cnt - editstep;
if (servo3cnt < (-200)) servo3cnt = -200;
break;
case 2:
servo3epa1 = servo3epa1 - editstep;
if (servo3epa1 < 1) servo3epa1 = 1;
break;
case 3:
servo3epa2 = servo3epa2 - editstep;
if (servo3epa2 < 1) servo3epa2 = 1;
break;
}
}
if (servosira == 4)
{
switch (islevsira)
{
case 1:
servo4cnt = servo4cnt - editstep;
if (servo4cnt < (-200)) servo4cnt = -200;
break;
case 2:
servo4epa1 = servo4epa1 - editstep;
if (servo4epa1 < 1) servo4epa1 = 1;
break;
case 3:
servo4epa2 = servo4epa2 - editstep;
if (servo4epa2 < 1) servo4epa2 = 1;
break;
}
}
break;
case 4:
if (servosira == 1)
{
switch (islevsira)
{
case 1:
servo1cnt = servo1cnt + editstep;
if (servo1cnt > 200) servo1cnt = 200;
break;
case 2:
servo1epa1 = servo1epa1 + editstep;
if (servo1epa1 > 700) servo1epa1 = 700;
break;
case 3:
servo1epa2 = servo1epa2 + editstep;
if (servo1epa2 > 700) servo1epa2 = 700;
break;
}
}
if (servosira == 2)
{
switch (islevsira)
{
case 1:
servo2cnt = servo2cnt + editstep;
if (servo2cnt > 200) servo2cnt = 200;
break;
case 2:
servo2epa1 = servo2epa1 + editstep;
if (servo2epa1 > 700) servo2epa1 = 700;
break;
case 3:
servo2epa2 = servo2epa2 + editstep;
if (servo2epa2 > 700) servo2epa2 = 700;
break;
}
}
if (servosira == 3)
{
switch (islevsira)
{
case 1:
servo3cnt = servo3cnt + editstep;
if (servo3cnt > 200) servo3cnt = 200;
break;
case 2:
servo3epa1 = servo3epa1 + editstep;
if (servo3epa1 > 700) servo3epa1 = 700;
break;
case 3:
servo3epa2 = servo3epa2 + editstep;
if (servo3epa2 > 700) servo3epa2 = 700;
break;
}
}
if (servosira == 4)
{
switch (islevsira)
{
case 1:
servo4cnt = servo4cnt + editstep;
if (servo4cnt > 200) servo4cnt = 200;
break;
case 2:
servo4epa1 = servo4epa1 + editstep;
if (servo4epa1 > 700) servo4epa1 = 700;
break;
case 3:
servo4epa2 = servo4epa2 + editstep;
if (servo4epa2 > 700) servo4epa2 = 700;
break;
}
}
break;
}
if (pressedkey > 0);
{
digitalWrite(centerLED, LOW);
digitalWrite(epa1LED, LOW);
digitalWrite(epa2LED, LOW);
digitalWrite(servo1LED, LOW);
digitalWrite(servo2LED, LOW);
digitalWrite(servo3LED, LOW);
digitalWrite(servo4LED, LOW);
switch (servosira)
{
case 1:
digitalWrite(servo1LED, HIGH);
break;
case 2:
digitalWrite(servo2LED, HIGH);
break;
case 3:
digitalWrite(servo3LED, HIGH);
break;
case 4:
digitalWrite(servo4LED, HIGH);
break;
}
switch (islevsira)
{
case 1:
digitalWrite(centerLED, HIGH);
break;
case 2:
digitalWrite(epa1LED, HIGH);
break;
case 3:
digitalWrite(epa2LED, HIGH);
break;
}
}
}
EEPROMyaz();
/*
digitalWrite(centerLED, LOW);
digitalWrite(epa1LED, LOW);
digitalWrite(epa2LED, LOW);
for (int n = 0; n < 5; n++)
{
digitalWrite(servo1LED, HIGH);
digitalWrite(servo2LED, HIGH);
digitalWrite(servo3LED, HIGH);
digitalWrite(servo4LED, HIGH);
delay(100);
digitalWrite(servo1LED, LOW);
digitalWrite(servo2LED, LOW);
digitalWrite(servo3LED, LOW);
digitalWrite(servo4LED, LOW);
delay(100);
}
*/
flashleds2();
progflag = 0;
}
void EEPROMoku()
{
noInterrupts();
servo1cnt = EEPROM.read(1) + 256 * EEPROM.read(0) - 2000;
servo1epa1 = EEPROM.read(3) + 256 * EEPROM.read(2);
servo1epa2 = EEPROM.read(5) + 256 * EEPROM.read(4);
servo2cnt = EEPROM.read(7) + 256 * EEPROM.read(6) - 2000;
servo2epa1 = EEPROM.read(9) + 256 * EEPROM.read(:coolxf:;
servo2epa2 = EEPROM.read(11) + 256 * EEPROM.read(10);
servo3cnt = EEPROM.read(13) + 256 * EEPROM.read(12) - 2000;
servo3epa1 = EEPROM.read(15) + 256 * EEPROM.read(14);
servo3epa2 = EEPROM.read(17) + 256 * EEPROM.read(16);
servo4cnt = EEPROM.read(19) + 256 * EEPROM.read(1:coolxf: - 2000;
servo4epa1 = EEPROM.read(21) + 256 * EEPROM.read(20);
servo4epa2 = EEPROM.read(23) + 256 * EEPROM.read(22);
interrupts();
}
void EEPROMyaz()
{
noInterrupts();
hibyte = (servo1cnt + 2000) / 256;
lobyte = (servo1cnt + 2000) - 256 * hibyte;
EEPROM.write(0, hibyte);
EEPROM.write(1, lobyte);
hibyte = servo1epa1 / 256;
lobyte = servo1epa1 - 256 * hibyte;
EEPROM.write(2, hibyte);
EEPROM.write(3, lobyte);
hibyte = servo1epa2 / 256;
lobyte = servo1epa2 - 256 * hibyte;
EEPROM.write(4, hibyte);
EEPROM.write(5, lobyte);
hibyte = (servo2cnt + 2000) / 256;
lobyte = (servo2cnt + 2000) - 256 * hibyte;
EEPROM.write(6, hibyte);
EEPROM.write(7, lobyte);
hibyte = servo2epa1 / 256;
lobyte = servo2epa1 - 256 * hibyte;
EEPROM.write(8, hibyte);
EEPROM.write(9, lobyte);
hibyte = servo2epa2 / 256;
lobyte = servo2epa2 - 256 * hibyte;
EEPROM.write(10, hibyte);
EEPROM.write(11, lobyte);
hibyte = (servo3cnt + 2000) / 256;
lobyte = (servo3cnt + 2000) - 256 * hibyte;
EEPROM.write(12, hibyte);
EEPROM.write(13, lobyte);
hibyte = servo3epa1 / 256;
lobyte = servo3epa1 - 256 * hibyte;
EEPROM.write(14, hibyte);
EEPROM.write(15, lobyte);
hibyte = servo3epa2 / 256;
lobyte = servo3epa2 - 256 * hibyte;
EEPROM.write(16, hibyte);
EEPROM.write(17, lobyte);
hibyte = (servo4cnt + 2000) / 256;
lobyte = (servo4cnt + 2000) - 256 * hibyte;
EEPROM.write(18, hibyte);
EEPROM.write(19, lobyte);
hibyte = servo4epa1 / 256;
lobyte = servo4epa1 - 256 * hibyte;
EEPROM.write(20, hibyte);
EEPROM.write(21, lobyte);
hibyte = servo4epa2 / 256;
lobyte = servo4epa2 - 256 * hibyte;
EEPROM.write(22, hibyte);
EEPROM.write(23, lobyte);
interrupts();
}
void EEPROMtest()
{
eepromerror = 0;
unsigned long b = 0;
unsigned int temp;
for (int i = 0; i < 18; i++)
{
b = b + EEPROM.read(i);
}
if (b == 4590)
{
temp = EEPROM.read(0);
EEPROM.write(0, 0);
if (EEPROM.read(0) != 0)
{
eepromerror = 1;
}
EEPROM.write(0, temp);
}
}
int button() // 0: Geçerli tuş yok. 1: Mode1. 2: Mode2. 3: Decrease. 4: Increase. 5: Done.
{
key1 = buttonbas();
key2 = debouncebutton();
if ((key1 == key2) && ((key1 * key2) != 0))
{
return key1;
}
return 0;
}
int buttonbas()
{
keypress = PINC & B00001111;
for (int n = 0; n < 10; n++)
{
keypress &= PINC;
}
if (keypress == B00001100) return 5;
if (keypress == B00001110) return 1;
if (keypress == B00001101) return 2;
if (keypress == B00001011) return 3;
if (keypress == B00000111) return 4;
return 0;
}
int debouncebutton() //Buraya gelirken key1 değişkeni basıldığı belirlenen butonu içeriyor. Yani 0 - 5 arası bir değer.
{
if (key1 == 0) return key1;
b = 0; //Sayaç sıfırlanıyor
for (int n = 0; n < debounce; n++)
{
key = buttonbas();
if (key == key1) b++;
else if (key != 0) return 0;
}
if (b < bounce) return 0;
if (key1 > 2 && key1 < 5)
{
delay(20);
}
else
{
while (buttonbas() != 0)
{
}
}
return key1;
}
void flashleds()
{
digitalWrite(centerLED, HIGH);
delay(200);
digitalWrite(centerLED, LOW);
digitalWrite(epa1LED, HIGH);
delay(200);
digitalWrite(epa1LED, LOW);
digitalWrite(epa2LED, HIGH);
delay(200);
digitalWrite(epa2LED, LOW);
digitalWrite(servo1LED, HIGH);
delay(200);
digitalWrite(servo1LED, LOW);
digitalWrite(servo2LED, HIGH);
delay(200);
digitalWrite(servo2LED, LOW);
digitalWrite(servo3LED, HIGH);
delay(200);
digitalWrite(servo3LED, LOW);
digitalWrite(servo4LED, HIGH);
delay(200);
digitalWrite(servo4LED, LOW);
}
void flashleds2()
{
for (int n = 0; n < 5; n++)
{
digitalWrite(centerLED, HIGH);
digitalWrite(epa1LED, HIGH);
digitalWrite(epa2LED, HIGH);
digitalWrite(servo1LED, HIGH);
digitalWrite(servo2LED, HIGH);
digitalWrite(servo3LED, HIGH);
digitalWrite(servo4LED, HIGH);
delay(200);
digitalWrite(centerLED, LOW);
digitalWrite(epa1LED, LOW);
digitalWrite(epa2LED, LOW);
digitalWrite(servo1LED, LOW);
digitalWrite(servo2LED, LOW);
digitalWrite(servo3LED, LOW);
digitalWrite(servo4LED, LOW);
delay(200);
}
}
void servoout()
{
digitalWrite(servo1, HIGH);
delayMicroseconds(servo1PWM);
digitalWrite(servo1, LOW);
digitalWrite(servo2, HIGH);
delayMicroseconds(servo2PWM);
digitalWrite(servo2, LOW);
digitalWrite(servo3, HIGH);
delayMicroseconds(servo3PWM);
digitalWrite(servo3, LOW);
digitalWrite(servo4, HIGH);
delayMicroseconds(servo4PWM);
digitalWrite(servo4, LOW);
}