Intergrate code with project

im trying to intergrate

`#include "Mouse.h"
#include "Keyboard.h"

// set pin numbers for switch, joystick axes, and LED:
const int switchPin = 2; // switch to turn on and off mouse control
const int mouseButton = 3; // input pin for the mouse pushButton
const int xAxis = A0; // joystick X axis
const int yAxis = A1; // joystick Y axis
const int ledPin = 5; // Mouse control LED

// parameters for reading the joystick:
int range = 12; // output range of X or Y movement
int responseDelay = 5; // response delay of the mouse, in ms
int threshold = range / 4; // resting threshold
int center = range / 2; // resting position value

bool mouseIsActive = false; // whether or not to control the mouse
int lastSwitchState = LOW; // previous switch state

void setup() {
pinMode(switchPin, INPUT); // the switch pin
pinMode(ledPin, OUTPUT); // the LED pin
// take control of the mouse:
Mouse.begin();
}

void loop() {
// read the switch:
int switchState = digitalRead(switchPin);
// if it's changed and it's high, toggle the mouse state:
if (switchState != lastSwitchState) {
if (switchState == HIGH) {
mouseIsActive = !mouseIsActive;
// turn on LED to indicate mouse state:
digitalWrite(ledPin, mouseIsActive);
}
}
// save switch state for next comparison:
lastSwitchState = switchState;

// read and scale the two axes:
int xReading = readAxis(A0);
int yReading = readAxis(A1);

// if the mouse control state is active, move the mouse:
if (mouseIsActive) {
Mouse.move(xReading, yReading, 0);
if(xReading != yReading){
Keyboard.print("i");
}
}

// read the mouse button and click or not click:
// if the mouse button is pressed:
if (digitalRead(mouseButton) == HIGH) {
// if the mouse is not pressed, press it:
if (!Mouse.isPressed(MOUSE_LEFT)) {
Mouse.press(MOUSE_LEFT);
}
}
// else the mouse button is not pressed:
else {
// if the mouse is pressed, release it:
if (Mouse.isPressed(MOUSE_LEFT)) {
Mouse.release(MOUSE_LEFT);
}
}

delay(responseDelay);
}

/*
reads an axis (0 or 1 for x or y) and scales the analog input range to a range
from 0 to
*/

int readAxis(int thisAxis) {
// read the analog input:
int reading = analogRead(thisAxis);

// map the reading from the analog input range to the output range:
reading = map(reading, 0, 1023, 0, range);

// if the output reading is outside from the rest position threshold, use it:
int distance = reading - center;

if (abs(distance) < threshold) {
distance = 0;
}

// return the distance for this axis:
return distance;
}with my code #include <Keyboard.h>
#include <KeyboardLayout.h>
#include <Mouse.h>
const byte scrollPin = A5;
int currentPotValue;
int scrollBy;
int oldPotValue;
const int Xpin = A0;
const int Ypin = A1;

String potValueString = "Current Pot Value: ";

void setup() {
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
Serial.begin(19200);
oldPotValue = analogRead(scrollPin);
}

void loop(){
if(digitalRead(1) == LOW)
{
Keyboard.print("1");

}
if(digitalRead(2) == LOW)
{
Keyboard.print("2");

}
if(digitalRead(3) == LOW)
{
Keyboard.print("3");

}
if(digitalRead(4) == LOW)
{
Keyboard.print("4");

}
if(digitalRead(5) == LOW)
{
Keyboard.print("z");

}
if(digitalRead(6) == LOW)
{
Keyboard.print("x");

}
if(digitalRead(7) == LOW)
{
Keyboard.print("c");

}
if(digitalRead(8) == LOW)
{
Keyboard.print(" ");
Keyboard.print(" ");
}
int currentPotValue = analogRead(scrollPin);
int scrollBy = currentPotValue - oldPotValue;

if (abs(scrollBy) > 5) {
scrollBy *= 0.1;

Mouse.move(0,0,scrollBy);

oldPotValue = currentPotValue;
}
Serial.print(potValueString);
Serial.println(currentPotValue);
Serial.print("scrollBy Value: ");
Serial.println(scrollBy);
delay(10);
}`
i cant figure out how to pls help

Please start by reading this:

Then, edit your post so your code is properly formatted.
Then, tell us about your project.

google combining sketches

The fast and dirty way to merge two (or more) working sketches into one (possibly working) sketch is to create this main sketch:

void setup()
{
  setup1();
  setup2();
  // setup3();
  // setup4();
}
void loop()
{
  loop1();
  loop2();
  // loop3();
  // loop4{};
}

Then, put the two (or more) sketches into 'tabs'. You can add, remove, and rename tabs with the tab menu: the little triangle at the right end of the tab bar.

Rename the setup() and loop() in each tab to setup1()/loop1() in the first tab, setup2()/loop2() in the second, etc.

If you are lucky there will be no problems and both sketches will take turns so fast they will appear to be running 'at the same time'. If you are unlucky, you will have to resolve conflicts and rewrite each sketch that hogs the CPU.

Problems to look for:

Name Conflicts: Two or more sketches defining global variables or functions of the same name. You will have to change the names until there are no conflicts.

Pin conflicts: Two or more sketches using the same pin number for different purposes. You will have to change pin assignments to avoid conflicts.

Hardware Usage Conflicts: Two or more sketches that use the same hardware feature in different ways. For example, using the Serial port at different data rates. If you can get them all to agree on a data rate you can replace the several Serial.begin(rate) calls with one at the top of setup() in the 'main' sketch.

Library Conflicts: Some libraries don't work together because they use the same hardware for different purposes. For example, the Servo library uses Timer1 and so does the IRremote library. If you are lucky you will get an error message when they both try to use the same hardware interrupt. If you are not lucky, things just won't work.

1 Like

Thanks this really helped!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.