3589Views3Replies

how to combine 2 Arduino codes together ? Answered

Hello, I have a question. I need to combine my code for my Arduino project. I'm using a Arduino micro board .  The 2 codes are maxsonar and another is a flying drones coding. I'm lost and don't know how to combine the both of them as the maxsonar code will overwrite the flying drones code causing it unable to fly.

Maxsonar code

const int anPin1 = 0;
const int anPin2 = 1;
int triggerPin1 = 13;
long distance1, distance2, cm1 ,cm2;
void setup() {
Serial.begin(9600);  // sets the serial port to 9600
pinMode(triggerPin1, OUTPUT);
pinMode(A5, OUTPUT);
pinMode(A11, OUTPUT);
}

void start_sensor(){
digitalWrite(triggerPin1,HIGH);
delay(1);
digitalWrite(triggerPin1,LOW);
}
{
if (distance1 == 9)
{
analogWrite(A5,25.5);
delay(10);
}
else if (distance1 == 8)
{
analogWrite(A5,76.5);
delay(10);
}
else if( distance1 == 7)
{
analogWrite(A5,153);
delay(10);
}
else if(distance1 == 6)
{
analogWrite(A5,255);
delay(10);
}

}
{
if (distance2 == 9)
{
analogWrite(A11,25.5);
delay(10);
}
else if (distance2 == 8)
{
analogWrite(A11,76.5);
delay(10);
}
else if( distance2 == 7)
{
analogWrite(A11,153);
delay(10);
}
else if(distance2 == 6 )
{
analogWrite(A11,255);
delay(10);
}
}

/*
Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
*/

}
void count_in_cm()
{
cm1 = distance1 * 2.54;
cm2 = distance2 * 2.54;
}

void print_all()
{

if( distance1 <10 && distance2 >=10 )
{
analogWrite(A11,0);
Serial.print("Sensor1: Warning");
Serial.print(" ");
Serial.print(" ");
Serial.print("S2");
Serial.print(" ");
Serial.print(distance2);
Serial.print("in");
Serial.print(" ");
Serial.print(" ");
Serial.print(cm2);
Serial.print("cm");
Serial.println();
delay(700);

}
else if(distance1 >= 10 && distance2 < 10)
{
analogWrite(A5,0);
Serial.print("S1");
Serial.print(" ");
Serial.print(distance1);
Serial.print("in");
Serial.print(" ");
Serial.print(" ");
Serial.print(cm1);
Serial.print("cm");
Serial.print(" ");
Serial.print(" ");
Serial.print("Sensor2: Warning");
Serial.println();
delay(700);

}
else if (distance1 < 10 && distance2 < 10)
{
Serial.print("Sensor1: Warning");
Serial.print("  ");
Serial.print("Sensor2: Warning");
Serial.println();
delay(700);

}
else
{
analogWrite(A11,0);
analogWrite(A5,0);
Serial.print("S1");
Serial.print(" ");
Serial.print(distance1);
Serial.print("in");
Serial.print(" ");
Serial.print(" ");
Serial.print(cm1);
Serial.print("cm");
Serial.print(" ");
Serial.print(" ");
Serial.print("S2");
Serial.print(" ");
Serial.print(distance2);
Serial.print("in");
Serial.print(" ");
Serial.print(" ");
Serial.print(cm2);
Serial.print("cm");
Serial.println();
delay(700);
}
}

void loop() {
start_sensor();

count_in_cm();
print_all();
delay(200); //This is the equivant of the amount of sensors times 50.  If you changed this to 5 sensors the delay would be 250.
}

flying drone code

#include <Servo.h>
#include <string.h>

// channel declaration
Servo channel1; // throttle
Servo channel2; // yaw
Servo channel3; // pitch
Servo channel4; // roll
Servo channel5; // ball drop servo

// PWM freq setting, should it be 62.33Hz?
const int PWMConst = 20022;//50HZ

// global variables
byte incomingByte, incomingByteUSB;
byte rcBuffer[32], usbBuffer[12];
int i = 0, j = 0, STATE = 1;
int Flag = 0, usbFlag = 0;
unsigned int thr_16int = 0, yaw_16int = 0, pit_16int = 0, rol_16int = 0, gear_16int = 0;
unsigned int thr_16int_usb = 0, yaw_16int_usb = 0, pit_16int_usb = 0, rol_16int_usb = 0;
float throttle = 1000, roll = 1500, pitch = 1500, yaw = 1500;
int gear = 1500;
float currT, prevT = 0, _flagHover, throFlag;

// functions declaration
void extract_RC_cmd(void);
void extract_USB_cmd(void);
void translate_RC_cmd(void);
void translate_USB_cmd(void);
void RC_Neutral(void);
void reset_usb(void);
void reset_rc(void);
void hover(void);
void sonar(void);

void setup()
{
// setting registers
TCCR1A = _BV(COM1A1) | _BV(COM1B1);
TCCR1B = _BV(WGM13) | _BV(CS11);
ICR1 = PWMConst;

// set PWM I/O ports
channel1.attach(6);    // throttle
channel2.attach(9);    // yaw
channel3.attach(5);    // pitch
channel4.attach(10);    // roll
channel5.attach(11);   // fail safe-attit

//init usb buffer
usbBuffer[0] = 0x03;
usbBuffer[1] = 0xE8;
usbBuffer[2] = 0x0D;
usbBuffer[3] = 0xAC;
usbBuffer[4] = 0x15;
usbBuffer[5] = 0x7C;
usbBuffer[6] = 0x1D;
usbBuffer[7] = 0x4C;

reset_rc();
reset_usb();

Serial1.begin(115200);   // rc Rx data in
Serial.begin(115200);     // USB
}

void loop()
{

// accept serial data from computer
if (Serial.available() > 0)
{
if (incomingByteUSB >= 0x03 && incomingByteUSB <= 0x07)
{
usbFlag = 1;
}
if (usbFlag == 1)
{
usbBuffer[j] = incomingByteUSB;
j++;
if (j == 12)
{
usbFlag = 0;
j = 0;
extract_USB_cmd();
}
}
}

// accepte serial data from RC
if (Serial1.available() > 0)
{
if (incomingByte >= 0x80 && incomingByte <= 0x87)
{
Flag = 1;
}

if (Flag == 1)
{
rcBuffer[i] = incomingByte;
i++;
if (i == 32)
{
Flag = 0;
i = 0;
extract_RC_cmd();
}
}
// Serial.print(rcBuffer[i]);
}

// STATE MACHINE
switch (STATE)
{
case 1:
translate_USB_cmd();
reset_rc();
//Serial.print();
break;
case 2:
translate_RC_cmd();
reset_usb();
//Serial.print('R');
break;
case 3:
landing();
break;
default:
RC_Neutral();
break;
}

// output PWM
channel1.write(throttle);
channel2.write(yaw);
channel3.write(pitch);
channel4.write(roll);
channel5.write(gear);
}

// local functions
void extract_RC_cmd()
{
if (rcBuffer[0] >= 0x80 && rcBuffer[0] <= 0x87) // double check throttle range
{
thr_16int = rcBuffer[0] << 8 | rcBuffer[1]; // 16 bit throttle
// Serial.print(thr_16int);
}
if (rcBuffer[4] >= 0x19 && rcBuffer[4] <= 0x1E) // double check yaw range
{
yaw_16int = rcBuffer[4] << 8 | rcBuffer[5]; // 16 bit yaw
//Serial.print(yaw_16int);
}
if (rcBuffer[16] >= 0x09 && rcBuffer[16] <= 0x0E) // double check yaw range
{
rol_16int = rcBuffer[16] << 8 | rcBuffer[17]; // 16 bit yaw
//Serial.print(rol_16int);
}
if (rcBuffer[20] >= 0x11 && rcBuffer[20] <= 0x16) // double check yaw range
{
pit_16int = rcBuffer[20] << 8 | rcBuffer[21]; // 16 bit yaw
//Serial.print(pit_16int);
}
if (rcBuffer[22] >= 0x21 && rcBuffer[22] <= 0x26) // double check gear range
{
gear_16int = rcBuffer[22] << 8 | rcBuffer[23]; // 16 bit pitch
//Serial.print(pit_16int);
}

// state
if (rcBuffer[24] == 0x37 && rcBuffer[25] == 0xE0)    // Fmode SW state 1: Auto
{
STATE = 1;
}
else if (rcBuffer[24] == 0x34 && rcBuffer[25] == 0x00) // Fmode SW state 2: Manual
{
STATE = 2;
}
else if (rcBuffer[24] == 0x31 && rcBuffer[25] == 0xB4) // Fmode SW state 3: Emergency landing
{
STATE = 3;
}

}

void extract_USB_cmd(void)
{
if (usbBuffer[0] >= 0x03 && usbBuffer[0] <= 0x07) // double check throttle range
{
thr_16int_usb = usbBuffer[0] << 8 | usbBuffer[1]; // 16 bit throttle
}
if (usbBuffer[2] >= 0x0B && usbBuffer[2] <= 0x0F) // double check yaw range
{
yaw_16int_usb = usbBuffer[2] << 8 | usbBuffer[3]; // 16 bit yaw
}
if (usbBuffer[4] >= 0x13 && usbBuffer[4] <= 0x17) // double check pit range
{
pit_16int_usb = usbBuffer[4] << 8 | usbBuffer[5]; // 16 bit pitch
}
if (usbBuffer[6] >= 0x1B && usbBuffer[6] <= 0x1F) // double check rol range
{
rol_16int_usb = usbBuffer[6] << 8 | usbBuffer[7]; // 16 bit role
}

// state
if (usbBuffer[10] == 0x2A && usbBuffer[11] == 0xF8)
{
//state = 1; // auto
}
else if (usbBuffer[10] == 0x2C && usbBuffer[11] == 0xEC)
{
STATE = 3; // emergency landing
}
else if (usbBuffer[10] == 0x2E && usbBuffer[11] == 0xE0)
{
STATE = 0; // default rc neutral
}
}

void translate_RC_cmd(void)
{
throttle = (thr_16int - 30752 ) / 2.016;
yaw   = (9243 - yaw_16int) / 1.382;
pitch = (pit_16int - 3047) / 1.378;
roll  = (5129 - rol_16int) / 1.373;
gear  = (gear_16int - 7506) / 1.165;

throttle = min(2000, max(1000, throttle));
yaw      = min(2000, max(1000, yaw));
pitch    = min(2000, max(1000, pitch));
roll     = min(2000, max(1000, roll));
gear     = min(2000, max(1000, gear));
}

void translate_USB_cmd(void)
{
throttle = thr_16int_usb;
yaw = yaw_16int_usb - 2000;
pitch = pit_16int_usb - 4000;
roll =  rol_16int_usb - 6000;

//Serial.print(thr_16int_usb);

throttle = min(2000, max(1000, throttle));
yaw      = min(2000, max(1000, yaw));
pitch    = min(2000, max(1000, pitch));
roll     = min(2000, max(1000, roll));
}

void RC_Neutral(void)
{
throttle = 1000;
yaw = 1500;
pitch = 1500;
roll = 1500;
}

void landing(void)
{
throttle = throttle - 1;
if (throttle <= 1000) throttle = 1000;
yaw = 1500;
pitch = 1500;
roll = 1500;
}

void reset_usb(void)
{
thr_16int_usb = 1000;
yaw_16int_usb = 3500;
pit_16int_usb = 5500;
rol_16int_usb = 7500;
}

void reset_rc(void)
{
thr_16int = 0x8000;
yaw_16int = 0x1C03;
pit_16int = 0x13FD;
rol_16int = 0x0C07;
}

Tags:

Discussions

plesae help me solve this error please

Arduino: 1.8.5 (Windows 8.1), Board: "Arduino/Genuino Uno"

C:\Program Files (x86)\Arduino\libraries\NewSoftSerial-master\NewSoftSerial.cpp:39:24: fatal error: WConstants.h: No such file or directory

#include "WConstants.h"

^

compilation terminated.

exit status 1

Error compiling for board Arduino/Genuino Uno.

"Show verbose output during compilation"

option enabled in File -> Preferences.

/*

JPEG Camera Example Sketch

The sketch will take a picture on the JPEG Serial Camera and store the jpeg to an SD card

on an SD Shield

Written by Ryan Owens

SparkFun Electronics

Hardware Notes:

This sketch assumes the arduino has the microSD shield from SparkFun attached.

The camera Rx/Tx should be attached to pins 2 and 3.

IMPORTANT: The JPEG camera requires a TTL level shifter between the camera output

and the arduino. Bypassing this may damage the Arduino pins.

*/

//This example requires the MemoryCard, SdFat, JPEGCamera and NewSoftSerial libraries

#include <MemoryCard.h>

#include <SdFat.h>

#include <JPEGCamera.h>

#include <NewSoftSerial.h>

#if defined(ARDUINO) && ARDUINO >= 100

#include "Arduino.h"

#else

#include "WProgram.h"

#endif

#include <Wire.h>

//Create an instance of the camera

JPEGCamera camera;

//Create a character array to store the cameras response to commands

char response[32];

//Count is used to store the number of characters in the response string.

unsigned int count=0;

//Size will be set to the size of the jpeg image.

int size=0;

//This will keep track of the data address being read from the camera

//eof is a flag for the sketch to determine when the end of a file is detected

//while reading the file data from the camera.

int eof=0;

void setup()

{

//Setup the camera, serial port and memory card

camera.begin();

Serial.begin(9600);

MemoryCard.begin();

//Reset the camera

count=camera.reset(response);

delay(3000);

//Take a picture

count=camera.takePicture(response);

//Print the response to the 'TAKE_PICTURE' command.

Serial.write((const uint8_t*)response, count);

Serial.println();

//Get the size of the picture

count = camera.getSize(response, &size);

//Print the size

Serial.print("Size: ");

Serial.println(size);

//Create a file called 'test.txt' on the SD card.

//NOTE: The memoryCard libary can only create text files.

//The file has to be renamed to .jpg when copied to a computer.

MemoryCard.open("/test.txt", true);

{

//Store all of the data that we read to the SD card

for(int i=0; i<count; i++){

//Check the response for the eof indicator (0xFF, 0xD9). If we find it, set the eof flag

if((response[i] == (char)0xD9) && (response[i-1]==(char)0xFF))eof=1;

//Save the data to the SD card

MemoryCard.file.print(response[i], BYTE);

//If we found the eof character, get out of this loop and stop reading data

if(eof==1)break;

}

//Make sure we stop reading data if the eof flag is set.

if(eof==1)break;

}

//Close the file

MemoryCard.close();

Serial.print("Done.");

}

void loop()

{

}

It might be a little more complicated than that. You have to understand what the flight control program is doing, especially when it is waiting for a command and then executing it. You then have to figure out the best place to perform your function of letting the sensor calibrate/sense and then light up the leds. With the delay of that, your drone could be out of control if not timed right.

+1

Full understanding of how each sketch works is needed if your going to stitch them together into a single sketch. It can get rather complicated.