1

I've been working with MEGA before. This code works completely with MEGA. I'm still at beginner level in Arduino.

Q: How do I adapt this code to DUE?

Note: I'm asking this question because I think the only problem in adaptation seems to be SoftwareSerial.

#include <SoftwareSerial.h>
SoftwareSerial bluetooth(52,53);    // TX,RX                               
int bar_analog=0;                                    
int head_analog=0;
int bar_anim=0;
int strip_anim=0;
int motor_speed=1;
int direct=0;
int level=0;
boolean debug = true;
int IN1a=49;
int IN2a=48;
int PWM1=3;
int IN1b=22;
int IN2b=23;
int PWM2=4;
int hiz=100;
int PWM_kare=2;
int INA_kare = 51;  
int INB_kare = 50; //LED '+'sı OUTPUT B'de olduğu için INB'yi  HIGH yapıyoruz.
void m1_cw() {
    digitalWrite(IN1a,HIGH);
    digitalWrite(IN2a,LOW);
    analogWrite(PWM1,hiz);
}
void m1_ccw() {
    digitalWrite(IN1a,LOW);
    digitalWrite(IN2a,HIGH);
    analogWrite(PWM1,hiz);
}
void m2_cw() {
    digitalWrite(IN1b,HIGH);
    digitalWrite(IN2b,LOW);
    analogWrite(PWM2,hiz);
}
void m2_ccw() {
    digitalWrite(IN1b,LOW);
    digitalWrite(IN2b,HIGH);
    analogWrite(PWM2,hiz);
}
void m1_stop() {
    digitalWrite(IN1a,LOW);
    digitalWrite(IN2a,LOW);
    analogWrite(PWM1,0);
}
void m2_stop() {
    digitalWrite(IN1b,LOW);
    digitalWrite(IN2b,LOW);
    analogWrite(PWM2,0);
}
void mast_up() {
    digitalWrite(24,HIGH);
    digitalWrite(25,LOW);
}
void mast_down() {
    digitalWrite(24,LOW);
    digitalWrite(25,HIGH);
}
void mast_stop() {
    digitalWrite(24,LOW);
    digitalWrite(25,LOW);
}
void kare() {
    analogWrite(PWM_kare,head_analog);
    digitalWrite(INB_kare, HIGH);
    digitalWrite(INA_kare, LOW);
}
void setup() {
    Serial.begin(9600);                              
    bluetooth.begin(9600);                           
    Serial.println("Program is starting...");              
    Serial.println("by Berke Ogulcan Parlak"); 
    pinMode(IN1a,OUTPUT);
    pinMode(IN2a,OUTPUT);
    pinMode(IN1b,OUTPUT);
    pinMode(IN2b,OUTPUT);
    pinMode(PWM1,OUTPUT);
    pinMode(PWM2,OUTPUT);
    pinMode(24,OUTPUT);
    pinMode(25,OUTPUT);
    pinMode(INA_kare, OUTPUT);
    pinMode(INB_kare, OUTPUT);
    pinMode(PWM_kare, OUTPUT);
    digitalWrite(IN1a,LOW);
    digitalWrite(IN2a,LOW);
    digitalWrite(IN1b,LOW);
    digitalWrite(IN2b,LOW);
    digitalWrite(PWM1,LOW);
    digitalWrite(PWM2,LOW);  
    digitalWrite(24,LOW);
    digitalWrite(25,LOW);
    digitalWrite(INA_kare, LOW);
    digitalWrite(INB_kare, LOW);
    digitalWrite(PWM_kare, LOW); 
}

void loop() {
    if (bluetooth.available()) {
        while (bluetooth.available())                    
        {                                                //AppInventor'dan çektiğimiz verileri,değişkenlere atıyoruz.
            bar_analog = bluetooth.parseInt();              
            head_analog = bluetooth.parseInt();
            bar_anim = bluetooth.parseInt();
            strip_anim = bluetooth.parseInt();
            motor_speed = bluetooth.parseInt();
            direct = bluetooth.parseInt();
            level = bluetooth.parseInt();
            if (debug) {                                   // Arduin seri ekranından değerleri okumak için gereklidir.
                Serial.print("Bar Brightness: ");                        
                Serial.println(bar_analog);
                Serial.print("Bar Animation Type: ");
                Serial.println(bar_anim);
                Serial.print("Headlight Brightness: ");
                Serial.println(head_analog);
                Serial.print("Strip Animation Type: ");
                Serial.println(strip_anim);
                Serial.print("Motor Speed Level: ");
                Serial.println(motor_speed);
                Serial.print("Direction: ");
                Serial.println(direct);
                Serial.print("Mast Goes: ");
                Serial.println(level);
                Serial.println("--------------------------------");
            }
            if (bluetooth.read() == '\n') {               //Yazdırma işlemleri bu satırda yapılacak..

            }
        }}
    if(direct==1) { //ileri
        m1_ccw();
        m2_ccw(); 
    }
    else if(direct==2) { //geri
        m1_cw();
        m2_cw(); 
    }
    else if(direct==3) { //sol
        m1_ccw();
        m2_cw(); 
    }
    else if(direct==4) { //sağ
        m1_cw();
        m2_ccw(); 
    }
    else if(direct==0) { //durur
        m1_stop();
        m2_stop(); 
    }
    if(level==1) { //mast yukarı
        mast_up(); 
    }
    else if(level==2) { //mast aşağı
        mast_down(); 
    }
    else if(level==0) { //mast durur
        mast_stop(); 
    }
    kare();            //kare LED aktif.
}
  • 1
    Why would you have been using SoftwareSerial on the Mega in the first place?! – Majenko Jul 26 at 9:04
  • You posted almost 300 lines of code with no explanation of what it does, and asked a very broad, open-ended question "how to convert." The code also only has a couple of comments, and those are not in English. You need to provide a detailed description of what your program does, what hardware it interfaces with, what I/O lines it uses, and what MEGA-specific features it uses. – Duncan C Jul 26 at 13:04
  • You should also add detailed comments to your code, in English, so your readers can understand what it's doing. Further, you need to make an attempt to rewrite your sketch yourself, test it, and then post back asking for help with the problem you find. – Duncan C Jul 26 at 13:06
0

SoftwareSerial is a workaround for Arduino versions without enough Hardware Serial ports, which is not the case of Arduino Mega or Arduino Uno which have 4.

Delete SoftwareSerial, replace all occurrences of bluetooth by Serial1, and rewire the Serial communication from pins 52|53 into pins 18|19 instead of (TX|RX)

Besides, the Arduino Due tends to be more "strict" than Arduino Mega compiler.

Besides that, your code will compile with no issues.

Also, remember Arduino Due is 3.3V and not 5V.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.