#define channelnumber 4 // Number of channels
#define framecount 1 //
#define minsync 3000 /
int inputpin = 48; // PPM input pin
int inverted = 0;
long serialspeed = 115200;
long synclength[framecount]; // syncvalues
int channel[channelnumber*framecount]; // Channel values
long framelength[framecount]; // frame start times
int counter = 0; // counter for measuring timings
int outcounter = 0; // counter for output measured timings
int synced = 0; // used boolean after found sync, start get all frames from pattern
int offset_max = 1300;
int offset_min = 900;
int pin_out_1 = 12;
int pin_out_2 = 11;
int pin_out_3 = 8;
int pin_out_4 = 7;
void forward()
{
digitalWrite(pin_out_1, LOW);
digitalWrite(pin_out_2, HIGH);
digitalWrite(pin_out_3, HIGH);
digitalWrite(pin_out_4, LOW);
}
void back()
{
digitalWrite(pin_out_1, HIGH);
digitalWrite(pin_out_2, LOW);
digitalWrite(pin_out_3, LOW);
digitalWrite(pin_out_4, HIGH);
}
void left()
{
digitalWrite(pin_out_1, HIGH);
digitalWrite(pin_out_2, LOW);
digitalWrite(pin_out_3, HIGH);
digitalWrite(pin_out_4, LOW);
}
void right()
{
digitalWrite(pin_out_1, LOW);
digitalWrite(pin_out_2, HIGH);
digitalWrite(pin_out_3, LOW);
digitalWrite(pin_out_4, HIGH);
}
void halt()
{
digitalWrite(pin_out_1, LOW);
digitalWrite(pin_out_2, LOW);
digitalWrite(pin_out_3, LOW);
digitalWrite(pin_out_4, LOW);
}
void init()
{
Serial.begin(serialspeed);
pinMode(inputpin, INPUT);
synced = 0;
pinMode(pin_out_1, OUTPUT);
pinMode(pin_out_2, OUTPUT);
pinMode(pin_out_3, OUTPUT);
pinMode(pin_out_4, OUTPUT);
}
void manualcontrol()
{
while(1)
{
synclength[counter] = 0;
if (synced == 0){
if ( inverted == 0 ){
synclength[counter] = pulseIn(inputpin, HIGH);
}
else{
synclength[counter] = pulseIn(inputpin, LOW);
}
}
if ((synclength[counter] > minsync)or(synced == 1)){
synced = 1;
framelength[counter] = micros();
synclength[counter] = 0;
if ( inverted == 0 ){ // if inverted signal selected
for (int channelcounter = 0; channelcounter < channelnumber; channelcounter++){ // loop for measuring all channels
channel[(counter * channelnumber) + channelcounter]=pulseIn(inputpin, HIGH);
}
synclength[counter] = pulseIn(inputpin, HIGH); // get syncpulse length
}
else{ // else inverted is selected
for (int channelcounter = 0; channelcounter < channelnumber; channelcounter++){ // loop for measuring all channels
channel[(counter * channelnumber) + channelcounter]=pulseIn(inputpin, LOW);
}
synclength[counter] = pulseIn(inputpin, LOW); // get syncpulse length
}
framelength[counter] = micros() - framelength[counter] ; // keep difference between start- and endtime
if (counter >= (framecount-1)){ // if captured enough frames
synced = 0; // set synced to "false"
outcounter = 0; // start output with second (third) frame, the first (first to) get measured bad
while (outcounter < framecount){ // loop for all captured frames
if(channel[0]>offset_max)
{
right();
}
else if(channel[0]<offset_min)
{
left();
}
else if(channel[1]>offset_max)
{
forward();
}
else if(channel[1]<offset_min)
{
back();
}
else
{
halt();
}
Serial.println("");
outcounter++;
}
Serial.println("");
counter = 0;
}
else{
counter++;
}
}
}
}