Probleempje met programma arduino

Heb probleem met mijn code.
De code tot nu toe werkt prima,echter als ik een kanaal van mijn receiver wil toevoegen werkt deze niet en stopt de serieel monitor er ook mee heb een H bridge met 2motoren een servo en een 8kanaals relais module deze werken allemaal zowel met RC als via de telefoon met Bluetooth
Daar ik een carson 14kanaals zender /ontvanger heb en nu maar 5kanalen heb aangesloten gaat alles goed declareer ik echter channel[6]=pinIn(10,HIGH,50000); werkt deze niet ook de serieel monitor geeft niets meer aan voor de rest blijft alles wel werken
Wat doe ik verkeerd wie weet raad??
Code:
const int SERIAL_BUFFER_SIZE = 8;
char serialBuffer[SERIAL_BUFFER_SIZE];
 #include <Servo.h>
boolean newData = false;
const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

//servo
const int servo1 = 32;  // Pin for the first servo
int servoVal;  
Servo myservo1;           // create servo object to control a servo
 void Motor_1();
 void bluethooth();
 void  recvWithEndMarker();
 void   showNewData();

 //l298n -------------
// Motor A
 int enA =2;
int in1 = 52;
int in2 = 50;
 // Motor B
 int enB = 3;
int in3 = 51;
int in4 = 53;
//Motor Speed Values - Start at zero
int MotorSpeed1 = 0;
int MotorSpeed2 = 0;
 //rc kanalen---------
double channel[2];
const int pwmPIN[]={4,5,6,7,8,9,10,11,12,13,44}; // an array to identify the PWM input pins (the array can be any length)
int RC_inputs = 0;
int i;
 int  buttonState =0;
 int lastbuttonState = 0;
 int x = 0;

 
void setup(){

 myservo1.attach(servo1);  // attaches the servo
 for(i=22;i<30;i++)
{
  pinMode(i, OUTPUT);//relay pins
  digitalWrite(i, HIGH);

  for(i=30;i<40;i++)
   pinMode(i, OUTPUT);//relay pins
    digitalWrite(i, LOW);

}

  // Set all the motor control pins to outputs
 
 pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
    int motorA=0;
  int motorB = 0;
  // Start with motors disabled and direction forward
   // Motor A
    digitalWrite(enA, LOW);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
   // Motor B
   digitalWrite(enB, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  Serial.begin(9600);

}
void loop() {

channel[0]=pulseIn(7, HIGH,50000);//ch2 ln298 rechter joystick x as
channel[1]=pulseIn(8, HIGH,50000);//ch4 ln298 rechter joystick y as
channel[3]=pulseIn(6, HIGH,50000);//ch8 ln298
channel[4]=pulseIn(4, HIGH,50000);//ch 14 relais1 en relais 2
channel[5]=pulseIn(5, HIGH,50000);//ch 13 2xrelais
channel[6]=pulseIn(10, HIGH,50000);//ch 13 2xrelais





   // failsave voor als het rc signaal wegvalt stopt het script
   //failsave

int sIn = 4;

unsigned long dur;
unsigned long failsafeVal = 1900;
int debounce = 1; //seconds//amount of time to regain rc signal from the time its lost
unsigned long lastDisconnection = 0;
unsigned long previousMillis = 0;
int RCstatus = 0;
int prevStatus = 0;
unsigned long currentMillis = millis();
 
  dur = pulseIn(sIn, HIGH, 500000);

  //falls to acceptable range
  if (dur >= 950 && dur <= 2000)
  {
    //check if it regains signal
    if (prevStatus == 2 && (currentMillis - lastDisconnection) < (debounce * 1000))
   
      dur = failsafeVal;
 
   
    Motor_1();
  } else {

    bluethooth();
    }
}
 void Motor_1(){
//y as biede motoren syncroon voor achteruit x as alleen linker of rechter motor met roerservo-----------

  if (channel[3] >1500){
 
  if (channel[0] < 1400)
  {
 // Set Motor A backward
  digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
 
    // Set Motor B backward
 
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
 
    MotorSpeed1 = map(channel[0], 1400, 1020, 0, 255);
    MotorSpeed2 = map(channel[0], 1400,1020, 0,255);
 }
  else if (channel[0] > 1500)
  {
    // This is Forward
 
    // Set Motor A forward
    digitalWrite(in1, HIGH);
   digitalWrite(in2, LOW);

       // Set Motor B forward
 
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
 
    //Determine Motor Speeds
    MotorSpeed1 = map(channel[0],1480, 1930, 0, 255);
    MotorSpeed2 = map(channel[0],1480, 1930, 0, 255);

  }
  else
  {
    // This is Stopped
 
    MotorSpeed1 = 0;
    MotorSpeed2 = 0;
  }
 
  // Now do the steering
  if (channel[1] < 1400)
  {
   
    // Move Left

    i = map(channel[1], 1400, 1020, 0, 255);
       
    MotorSpeed1 = MotorSpeed1 - i;
    MotorSpeed2 = MotorSpeed2 + i;

   if (MotorSpeed1 < 0)MotorSpeed1 = 0;
    if (MotorSpeed2 > 255)MotorSpeed2 = 255;
 
  }
  else if (channel[1] > 1550)
  {
    // Move Right
   
    i= map(channel[1], 1508, 1930, 0, 255);

    MotorSpeed1 = MotorSpeed1 + i;
    MotorSpeed2 = MotorSpeed2 -i;

    // Don't exceed range of 0-255 for motor speeds
 
    if (MotorSpeed1 < 0)MotorSpeed1 = 0;
    if (MotorSpeed2 > 255)MotorSpeed2 = 255;
  }



  }
  //alleen syncroon draaien met roerservo -------yas motoren x as roerservo---------------
 
   if ( channel[3]>1400 && channel[3]<1600){
      if (channel[0] < 1400)
  {
   // Set Motor A backward
  digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
 
    // Set Motor B backward
 
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);

    MotorSpeed1 = map(channel[0], 1400, 1010, 0, 255);
    MotorSpeed2 = map(channel[0], 1400,1010, 0,255);
 
  }
  else if (channel[0] > 1500)
  {
    // This is Forward
 
    // Set Motor A forward
    digitalWrite(in1, HIGH);
   digitalWrite(in2, LOW);

       // Set Motor B forward
 
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
 
    //Determine Motor Speeds
 
    MotorSpeed1 = map(channel[0],1500, 1930, 0, 255);
    MotorSpeed2 = map(channel[0],1500, 1930, 0, 255);
  }
      else
  {
    // This is Stopped
 
    MotorSpeed1 = 0;
    MotorSpeed2 = 0;
  }
   }
    if ( channel[3]<1400){
    if  (channel[1]>1500){
   
 
   //Motor A achteruit
  digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
 
    // Set Motor B vooruit
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
   
    MotorSpeed1 = map(channel[1],1500, 1930, 0, 255);
    MotorSpeed2 = map(channel[1],1500, 1930, 0, 255);
  }
 
   
  else if ( channel[3]<1400){
  if ( channel[1]<1500){

      digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
 
    // Set Motor B backward
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
   
   MotorSpeed1 = map(channel[1], 1400, 1020, 0, 255);
    MotorSpeed2 = map(channel[1], 1400,1020, 0,255);
  }
  }else {
    // This is Stopped
 
    MotorSpeed1 = 0;
    MotorSpeed2 = 0;
  }
  }


  if (MotorSpeed1 < 30)MotorSpeed1 = 0;    // Adjust to prevent "buzzing" at very low speed
  if (MotorSpeed2 < 30)MotorSpeed2 = 0;
 
  analogWrite(enA, MotorSpeed1); // Set the motor speeds
  analogWrite(enB, MotorSpeed2);


// servo roer


// servo roer
if (channel[1]>1490){//sturen bij syncroon draaien

   servoVal= map(channel[1], 1485, 1915,90,0);  // sturen met beide motoren knop middenstand
}else if (channel[1]<1450){
   servoVal= map(channel[1], 1480,1020, 90, 180);
}else if (channel[1]>1400 && channel[1]<1550){
  servoVal = 90;
}
 myservo1.write(servoVal);  

 delay(5);




if(channel[4]>1600 ){         //dekverl 1 pulse kan. 4
buttonState =digitalRead (22);
  if (buttonState ==HIGH){
  digitalWrite (22, LOW);

}  else if(channel[4]>1600 ){
  digitalWrite (22, HIGH);
}
delay(300);
}
if(channel[4]<1200 && channel[4]>800 ){  //dekverl 2
buttonState =digitalRead (33);
  if (buttonState ==HIGH){
  digitalWrite (33, LOW);

 
}  else if(channel[4]<1200 && channel[4]>800){
  digitalWrite (33, HIGH);
}

delay(300);
}
Serial.println (channel[4]);
if(channel[5]>1600 ){         //dekverl 1 pulse kan. 4
buttonState =digitalRead (31);
  if (buttonState ==HIGH){
  digitalWrite (31, LOW);
 
}  else if(channel[5]>1600 ){
  digitalWrite (31, HIGH);
}

delay(300);
}
if(channel[5]<1200 && channel[5]>800 ){  //dekverl 2
buttonState =digitalRead (35);
  if (buttonState ==HIGH){
  digitalWrite (35, LOW);

}  else if(channel[5]<1200 && channel[5]>800){
  digitalWrite (35, HIGH);
}
  Serial.println (channel[5]);
delay(300);
}


 }


 
  void bluethooth(){

 static byte ndx = 0;
 char startmarker;
    char endMarker = '>';
    char rc;
 
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        } else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
   



  int motorA;
  int motorB ;

  // Set all the motor control pins to outputs
 
 pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
 
  // Start with motors disabled and direction forward
   // Motor A
    digitalWrite(enA, LOW);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
   // Motor B
   digitalWrite(enB, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);

 
  int angle=0;
    if (newData == true) {
     char input=receivedChars;
        Serial.print(receivedChars);
        Serial.print("-");
     
        int Blt = atol(receivedChars);
        Serial.println(Blt);
       // if startmarker=C
  switch (Blt) {  
    case 1:                // Dekverlichting 1
digitalWrite(30, HIGH);
      break;
    case 2:
digitalWrite(30,LOW);

      break;
    case 3:                //Dekverlichting2
  digitalWrite(31,HIGH);
      break;
    case 4: //
digitalWrite(31,LOW);
      break;
      case 5:             //Stuurhutlicht
digitalWrite(34, HIGH);

      break;
    case 6:
digitalWrite(34,LOW);

      break;
    case 7:               //schijnwerpers
  digitalWrite(35,HIGH);
      break;
    case 8: //
digitalWrite(35,LOW);
      break;
 case 9:              //navigatielichten
digitalWrite(36, HIGH);
      break;
 case 10:
digitalWrite(36,LOW);
      break;
 case 11:          //sleeplichten
  digitalWrite(22,HIGH);
      break;
  case 12:
    digitalWrite(22,LOW);
      break;
      case 13:        //radars
     digitalWrite(28, LOW);
    break;
  case 14:
     digitalWrite(28,HIGH);
     break;
 case 15:
    digitalWrite(29,LOW);
      break;
    case 16: //
   digitalWrite(29,HIGH);
      break;

 
 
 case 109:            //Stuur BB
   servoVal = 178;
   myservo1.write(servoVal);  
   break;
 case 110:
   servoVal = 90;
   myservo1.write(servoVal);
   break;
 case 111:               //Stuur SB
   servoVal = 0;
   myservo1.write(servoVal);
   break;
 case 112:
  servoVal = 90;
   myservo1.write(servoVal);  
   break;
   
 case 100:    ///motor A vooruit
     motorA=2;
     break;
 case 102:    ///motor A stop
     motorA = 0;
    break;
  case 113:    ///motor A stopknop
     motorA = 0;
    break;
 case 105: // // set motorA achteruit
    motorA= 1;  
   break;
 case 106: //// zet motorA stop
    motorA = 0;
    break;
 case 107: //: // // set motorB achteruit
    motorB = 1;    
    break;
 case 108: //// zet motorB stop
    motorB = 0;
    break;
  case 114: //// zet motorB stopknop
    motorB = 0;
    break;
 case 103: //// set motorB voouit
    motorB =2;  
    break;
case 104: //// zet motorB stop
    motorB = 0;
  break;
  }
  if (motorA == 1 &&  Blt==406){
 // Set Motor A backward

  digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
       MotorSpeed1 = 40; ;
   
         Serial.print("A achteruit");
 }else if (motorA == 2 && Blt == 404){
    // Set Motor A forward
    Serial.print(" A vooruit");
 
    digitalWrite(in1, HIGH);
   digitalWrite(in2, LOW);
   MotorSpeed1 = 40;

 }else if (motorA == 0){
  Serial.print(" A stop");
    MotorSpeed1 = 0;
 
 }

 
 if (motorB == 1 &&Blt ==408){        
// Set Motor B backward
   
         Serial.print("B achteruit");
    digitalWrite(enB, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
 
    MotorSpeed2 = 40;
   
 }else  if (motorB == 2 &&Blt==410){  
       // Set Motor B forward

        Serial.print(" B vooruit");

    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
 
    MotorSpeed2 =40;
  }else if(motorB == 0){
     Serial.print(" b stop");

    MotorSpeed2 = 0;
 }

 if (Blt > 1000&& Blt < 1255) {
  if ( motorA== 1 || motorA == 2){

  MotorSpeed1= map (Blt,1000,1255,30,255);
 }
 
 }
 if ( Blt>2000 && Blt< 2255 ){
 if ( motorB == 1 || motorB== 2){
  MotorSpeed2 = map (Blt,2000,2255,30,255);  
 }
 }

 if ( Blt> 3000&& Blt< 3255 ){
 if ( motorB == 1 && motorA== 1|| motorB== 2 && motorA==2 || motorA==1 && motorB == 2 || motorA==2 && motorB ==1){
    MotorSpeed1 = map (Blt,3000,3255,30,255);  
  MotorSpeed2 = map (Blt,3000,3255,30,255);  
 }
 }


 
 Serial.print("-A=");
 Serial.print(motorA);
   Serial.print("-B=");
     Serial.print(motorB);
   Serial.print("-ms1_");  
 Serial.print (MotorSpeed1);
    Serial.print("-ms2_");
      Serial.println (MotorSpeed2);
 
  analogWrite(enA, MotorSpeed1); // Set the motor speeds
  analogWrite(enB, MotorSpeed2);



   
    }
 
  newData = false;  
  }

  }
 
Laatst bewerkt:
Back
Top