Combine 2 Arduino Sketches

Hello, I have two sketches that both compile without errors. I have attached both. Sketch A makes the Arduino appear as a game controller on win 10, but unfortunately it uses potentiometers connected to A0 thru A4. I want to use optical encoders connected to the digital side. My sketch B does that but it does not show up as a game controller. How do I combine sketch A and B? I want to keep the overall construct of sketch A, and simply strip out the potentiometer code, and insert the encoder code from sketch B. Please help

Sketch A Joystick Gamecontroller.txt (4.06 KB)

Sketch B Encoder to motor.txt (1.78 KB)

See How to combine two Arduino sketches

Thanks R. What does

redefinition of void setup()

mean? It also says exit status 1

Sketch A+B Combined Joystick Gamecontroller+Encoder.txt (5.84 KB)

It probably means you have two sets of “ void setup()” in your sketch - you can only have one .

but i need two sets of void setups(). I am combining two sketches into one

No

Move the code from one of those setup()s into the other setup().

Ok thanks, Now I get another error that says

expected constructor, destructor, or type conversion before '(' token

it is stuck at this line.

// Set LED pins as outputs
pinMode (ledCW,OUTPUT);
pinMode (ledCCW,OUTPUT);

How much of either sketch did you write? How familiar are you with Arduino? Coding? etc.

Post code.
Post actual error message (s)

Use code tags.

I did not write any of the sketches. I downloaded them, and I am just trying to combine both of them into one sketch. Unfortunately sketch A does everything I want, but it cant seem to recognize the optical encoders. Sketch B recognizes the encoders, but does not show up as a game controller in win 10. So I am trying to combine them with cu and paste. I am a newbie

Post code and error messages, not excuses.

Hi Mr Awol. Here is the code.

// Program used to test the USB Joystick library when used as
// a Flight Controller on the Arduino Leonardo or Arduino
// Micro.
//
// Matthew Heironimus
// 2016-05-29 - Original Version
//------------------------------------------------------------

#include "Joystick.h"

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_MULTI_AXIS, 32, 0,
true, true, false, false, false, false,
true, true, false, false, false);

// Set to true to test "Auto Send" mode or false to test "Manual Send" mode.
//const bool testAutoSendMode = true;
const bool testAutoSendMode = false;

const unsigned long gcCycleDelta = 1000;
const unsigned long gcAnalogDelta = 25;
const unsigned long gcButtonDelta = 500;
unsigned long gNextTime = 0;
unsigned int gCurrentStep = 0;

void testSingleButtonPush(unsigned int button)
{
if (button > 0)
{
Joystick.releaseButton(button - 1);
}
if (button < 32)
{
Joystick.pressButton(button);
}
}

void testMultiButtonPush(unsigned int currentStep)
{
for (int button = 0; button < 32; button++)
{
if ((currentStep == 0) || (currentStep == 2))
{
if ((button % 2) == 0)
{
Joystick.pressButton(button);
} else if (currentStep != 2)
{
Joystick.releaseButton(button);
}
} // if ((currentStep == 0) || (currentStep == 2))
if ((currentStep == 1) || (currentStep == 2))
{
if ((button % 2) != 0)
{
Joystick.pressButton(button);
} else if (currentStep != 2)
{
Joystick.releaseButton(button);
}
} // if ((currentStep == 1) || (currentStep == 2))
if (currentStep == 3)
{
Joystick.releaseButton(button);
} // if (currentStep == 3)
} // for (int button = 0; button < 32; button++)
}

void testXYAxis(unsigned int currentStep)
{
int xAxis;
int yAxis;

if (currentStep < 256)
{
xAxis = currentStep - 127;
yAxis = -127;
Joystick.setXAxis(xAxis);
Joystick.setYAxis(yAxis);
}
else if (currentStep < 512)
{
yAxis = currentStep - 256 - 127;
Joystick.setYAxis(yAxis);
}
else if (currentStep < 768)
{
xAxis = 128 - (currentStep - 512);
Joystick.setXAxis(xAxis);
}
else if (currentStep < 1024)
{
yAxis = 128 - (currentStep - 768);
Joystick.setYAxis(yAxis);
}
else if (currentStep < 1024 + 128)
{
xAxis = currentStep - 1024 - 127;
Joystick.setXAxis(xAxis);
Joystick.setYAxis(xAxis);
}
}

void testThrottleRudder(unsigned int value)
{
Joystick.setThrottle(value);
Joystick.setRudder(255 - value);
}

/*
Rotary Encoder Demo
rot-encode-demo.ino
Demonstrates operation of Rotary Encoder
Displays results on Serial Monitor
DroneBot Workshop 2019

*/

// Rotary Encoder Inputs
#define inputCLK 4
#define inputDT 5

// LED Outputs
#define ledCW 8
#define ledCCW 9

int counter = 0;
int currentStateCLK;
int previousStateCLK;

String encdir ="";

void setup() {

Joystick.setXAxisRange(-127, 127);
Joystick.setYAxisRange(-127, 127);
Joystick.setZAxisRange(-127, 127);
Joystick.setThrottleRange(0, 255);
Joystick.setRudderRange(0, 255);

if (testAutoSendMode)
{
Joystick.begin();
}
else
{
Joystick.begin(false);
}

pinMode(A0, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
}

// Set encoder pins as inputs
pinMode (inputCLK,INPUT);
pinMode (inputDT,INPUT);

// Set LED pins as outputs
pinMode (ledCW,OUTPUT);
pinMode (ledCCW,OUTPUT);

// Setup Serial Monitor
Serial.begin (9600);

// Read the initial state of inputCLK
// Assign to previousStateCLK variable
previousStateCLK = digitalRead(inputCLK);

}

void loop() {

// System Disabled
if (digitalRead(A0) != 0)
{
// Turn indicator light off.
digitalWrite(LED_BUILTIN, 0);
return;
}

// Turn indicator light on.
digitalWrite(LED_BUILTIN, 1);

if (millis() >= gNextTime)
{

if (gCurrentStep < 33)
{
gNextTime = millis() + gcButtonDelta;
testSingleButtonPush(gCurrentStep);
}
else if (gCurrentStep < 37)
{
gNextTime = millis() + gcButtonDelta;
testMultiButtonPush(gCurrentStep - 33);
}
else if (gCurrentStep < (37 + 256))
{
gNextTime = millis() + gcAnalogDelta;
testThrottleRudder(gCurrentStep - 37);
}
else if (gCurrentStep < (37 + 256 + 1024 + 128))
{
gNextTime = millis() + gcAnalogDelta;
testXYAxis(gCurrentStep - (37 + 256));
}

if (testAutoSendMode == false)
{
Joystick.sendState();
}

gCurrentStep++;
if (gCurrentStep >= (37 + 256 + 1024 + 128))
{
gNextTime = millis() + gcCycleDelta;
gCurrentStep = 0;
}

void loop() {

// Read the current state of inputCLK
currentStateCLK = digitalRead(inputCLK);

// If the previous and the current state of the inputCLK are different then a pulse has occured
if (currentStateCLK != previousStateCLK){

// If the inputDT state is different than the inputCLK state then
// the encoder is rotating counterclockwise
if (digitalRead(inputDT) != currentStateCLK) {
counter --;
encdir ="CCW";
digitalWrite(ledCW, LOW);
digitalWrite(ledCCW, HIGH);

} else {
// Encoder is rotating clockwise
counter ++;
encdir ="CW";
digitalWrite(ledCW, HIGH);
digitalWrite(ledCCW, LOW);

}
Serial.print("Direction: ");
Serial.print(encdir);
Serial.print(" -- Value: ");
Serial.println(counter);
}
// Update previousStateCLK with the current state
previousStateCLK = currentStateCLK;
}
}
}

I don't see error messages, so I assume you've fixed it.
I didn't see code tags, so I assume you've ignored me.

You have to wait five minutes between replies.

Use them well.

Always use “CTRL T” or “CMD T” on your sketches.

The error msg is

expected constructor, destructor, or type conversion before '(' token
I don't know what a code tag is.

In my last post I pasted the code, but now
I have attached my .ino file

sketch_feb02a.ino (5.83 KB)

The whole error message.
It contains important information.

Please, don't make this be like doing dental work.

Here is the entire error list. Sorry about the confusion.

sketch_feb02a:157:9: error: expected constructor, destructor, or type conversion before '(' token
pinMode (inputCLK, INPUT);
^
sketch_feb02a:158:9: error: expected constructor, destructor, or type conversion before '(' token
pinMode (inputDT, INPUT);
^
sketch_feb02a:161:9: error: expected constructor, destructor, or type conversion before '(' token
pinMode (ledCW, OUTPUT);
^
sketch_feb02a:162:9: error: expected constructor, destructor, or type conversion before '(' token
pinMode (ledCCW, OUTPUT);
^
sketch_feb02a:165:1: error: 'Serial' does not name a type
Serial.begin (9600);
^~~~~~
sketch_feb02a:169:1: error: 'previousStateCLK' does not name a type
previousStateCLK = digitalRead(inputCLK);
^~~~~~~~~~~~~~~~
sketch_feb02a:171:1: error: expected declaration before '}' token
}
^
Multiple libraries were found for "Joystick.h"
Used: C:\Users\michael\Documents\Arduino\libraries\Joystick
Not used: C:\Users\michael\Documents\Arduino\libraries\ArduinoJoystickLibrary-master
Using library Joystick at version 2.0.5 in folder: C:\Users\michael\Documents\Arduino\libraries\Joystick
exit status 1
expected constructor, destructor, or type conversion before '(' token

This code is outside of any function. I assume it should be in setup().

// Set encoder pins as inputs
pinMode (inputCLK, INPUT);
pinMode (inputDT, INPUT);

// Set LED pins as outputs
pinMode (ledCW, OUTPUT);
pinMode (ledCCW, OUTPUT);

// Setup Serial Monitor
Serial.begin (9600);

// Read the initial state of inputCLK
// Assign to previousStateCLK variable
previousStateCLK = digitalRead(inputCLK);

}

You have 2 loop() functions.

Also, I believe the code was written for a different joystick library.

to give you a mechanical example:
if you would like to drive a 40 ton excavator.
You know how to drive a car. But you have no clue about the function of those pedals je joystick and these six levers to control this beast.
Will this work? no chance !

what do you have to learn?
Yes the basic function of all these levers, the joystick and the pedal. Otherwise you will damage a lot of things. The samw principle applys to writing code. if you don't start to learn you will be asking new basic question for moth