Combining two codes- help needed

Hello everyone. I am a newbie to arduino coding. For this particular code I am using Arduino Leonardo. I wanted to join these two codes together:
Code 1:

void setup() 
{
  Serial.begin(9600);
}
 
void loop() 
{
  float sensorValue = analogRead(A1);
  float millivolt = (sensorValue/1023)*5;
  
  Serial.print("Sensor Value: ");
  Serial.println(sensorValue);
  
  Serial.print("Voltage: ");
  Serial.print(millivolt*1000);
  Serial.println(" mV");
  Serial.println("");
  delay(1);       
}

Code 2:

#include "Mouse.h"

// set pin numbers for the five buttons:

const int upButton = 2;

const int downButton = 3;

const int leftButton = 4;

const int rightButton = 5;

const int mouseButton = 6;

int range = 5;              // output range of X or Y movement; affects movement speed
int responseDelay = 10;     // response delay of the mouse, in ms

void setup() {

  // initialize the buttons' inputs:

  pinMode(upButton, INPUT);

  pinMode(downButton, INPUT);

  pinMode(leftButton, INPUT);

  pinMode(rightButton, INPUT);

  pinMode(mouseButton, INPUT);

  // initialize mouse control:

  Mouse.begin();
}

void loop() {

  // read the buttons:

  int upState = digitalRead(upButton);

  int downState = digitalRead(downButton);

  int rightState = digitalRead(rightButton);

  int leftState = digitalRead(leftButton);

  int clickState = digitalRead(mouseButton);

  // calculate the movement distance based on the button states:

  int  xDistance = (leftState - rightState) * range;

  int  yDistance = (upState - downState) * range;

  // if X or Y is non-zero, move:

  if ((xDistance != 0) || (yDistance != 0)) {

    Mouse.move(xDistance, yDistance, 0);

  }

  // if the mouse button is pressed:

  if (clickState == 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);

    }

  }

  // a delay so the mouse doesn't move too fast:

  delay(responseDelay);
}

The first code is supposed to catch the electrical signals from the electrodes connected to the muscles and convert those to voltages. There will be different voltages when the muscles are contracted and when they are relaxed. The voltage value for a contracted muscle is going to be higher than when it is relaxed. Then the second code should capture these voltage values and a threshold voltage would be set. Above the threshold voltage depending on which electrode you are measuring the mouse would move in that direction. So if you were to contract the second electrode then the mouse would move up. When I pased the codes one after the other a lot of errors appeared. How would you suggest I make the changes so that I get the results I want?

It would be useful if you posted your attempt

Here is the combination of the two codes. There are a couple of errors though which I do not know how to fix.

void setup() 
{
  Serial.begin(9600);
}
 
void loop() 
{
  float sensorValue = analogRead(A1);
  float millivolt = (sensorValue/1023)*5;
  
  Serial.print("Sensor Value: ");
  Serial.println(sensorValue);
  
  Serial.print("Voltage: ");
  Serial.print(millivolt*1000);
  Serial.println(" mV");
  Serial.println("");
  delay(1);       
}

#include "Mouse.h"

// set pin numbers for the five buttons:

const int upButton = 2;

const int downButton = 3;

const int leftButton = 4;

const int rightButton = 5;

const int mouseButton = 6;

int range = 5;              // output range of X or Y movement; affects movement speed
int responseDelay = 10;     // response delay of the mouse, in ms

void setup ()
{

  // initialize the buttons' inputs:

  pinMode(upButton, INPUT);

  pinMode(downButton, INPUT);

  pinMode(leftButton, INPUT);

  pinMode(rightButton, INPUT);

  pinMode(mouseButton, INPUT);

  // initialize mouse control:

  Mouse.begin();
}

void loop() {

  // read the buttons:

  int upState = digitalRead(upButton);

  int downState = digitalRead(downButton);

  int rightState = digitalRead(rightButton);

  int leftState = digitalRead(leftButton);

  int clickState = digitalRead(mouseButton);

  // calculate the movement distance based on the button states:

  int  xDistance = (leftState - rightState) * range;

  int  yDistance = (upState - downState) * range;

  // if X or Y is non-zero, move:

  if ((xDistance != 0) || (yDistance != 0)) {

    Mouse.move(xDistance, yDistance, 0);

  }

  // if the mouse button is pressed:

  if (clickState == 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);

    }

  }

  // a delay so the mouse doesn't move too fast:

  delay(responseDelay);
}

You can't have more than one setup or loop function.

Now there is only 1 void setup and 1 void loop. There appears to be a problem with the brackets now. Should I erase the {} brackets which were after the extra void loop and extra void setup?

void setup() 
{
  Serial.begin(9600);
}
 
void loop() 
{
  float sensorValue = analogRead(A1);
  float millivolt = (sensorValue/1023)*5;
  
  Serial.print("Sensor Value: ");
  Serial.println(sensorValue);
  
  Serial.print("Voltage: ");
  Serial.print(millivolt*1000);
  Serial.println(" mV");
  Serial.println("");
  delay(1);       
}

#include "Mouse.h"

// set pin numbers for the five buttons:

const int upButton = 2;

const int downButton = 3;

const int leftButton = 4;

const int rightButton = 5;

const int mouseButton = 6;

int range = 5;              // output range of X or Y movement; affects movement speed
int responseDelay = 10;     // response delay of the mouse, in ms


{

  // initialize the buttons' inputs:

  pinMode(upButton, INPUT);

  pinMode(downButton, INPUT);

  pinMode(leftButton, INPUT);

  pinMode(rightButton, INPUT);

  pinMode(mouseButton, INPUT);

  // initialize mouse control:

  Mouse.begin();
}

 {

  // read the buttons:

  int upState = digitalRead(upButton);

  int downState = digitalRead(downButton);

  int rightState = digitalRead(rightButton);

  int leftState = digitalRead(leftButton);

  int clickState = digitalRead(mouseButton);

  // calculate the movement distance based on the button states:

  int  xDistance = (leftState - rightState) * range;

  int  yDistance = (upState - downState) * range;

  // if X or Y is non-zero, move:

  if ((xDistance != 0) || (yDistance != 0)) {

    Mouse.move(xDistance, yDistance, 0);

  }

  // if the mouse button is pressed:

  if (clickState == 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);

    }

  }

  // a delay so the mouse doesn't move too fast:

  delay(responseDelay);
}

You need to combine the code that was in the first setup(), with the code that was in the second setup() - what you've done is simply orphan some of the code.

This is my new code. I think now the codes should be working together. The error showing now is: Error compiling for board Arduino Leonardo.

void setup() 
{
  Serial.begin(9600);
}
 
void loop() 
{
  float sensorValue = analogRead(A1);
  float millivolt = (sensorValue/1023)*5;
  
  Serial.print("Sensor Value: ");
  Serial.println(sensorValue);
  
  Serial.print("Voltage: ");
  Serial.print(millivolt*1000);
  Serial.println(" mV");
  Serial.println("");
  delay(1);       


#include "Mouse.h"

// set pin numbers for the five buttons:

const int upButton = 2;

const int downButton = 3;

const int leftButton = 4;

const int rightButton = 5;

const int mouseButton = 6;

int range = 5;              // output range of X or Y movement; affects movement speed
int responseDelay = 10;     // response delay of the mouse, in ms


{

  // initialize the buttons' inputs:

  pinMode(upButton, INPUT);

  pinMode(downButton, INPUT);

  pinMode(leftButton, INPUT);

  pinMode(rightButton, INPUT);

  pinMode(mouseButton, INPUT);

  // initialize mouse control:

  Mouse.begin();
}

 {

  // read the buttons:

  int upState = digitalRead(upButton);

  int downState = digitalRead(downButton);

  int rightState = digitalRead(rightButton);

  int leftState = digitalRead(leftButton);

  int clickState = digitalRead(mouseButton);

  // calculate the movement distance based on the button states:

  int  xDistance = (leftState - rightState) * range;

  int  yDistance = (upState - downState) * range;

  // if X or Y is non-zero, move:

  if ((xDistance != 0) || (yDistance != 0)) {

    Mouse.move(xDistance, yDistance, 0);

  }

  // if the mouse button is pressed:

  if (clickState == 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);

    }

  }

  // a delay so the mouse doesn't move too fast:

  delay(responseDelay);
}
}

Please have another close look at reply #6.

Hint:

void setup ()
{   // ALL the setup code goes after here...


//...and before here
}
#include "Mouse.h"

const int upButton = 2;

const int downButton = 3;

const int leftButton = 4;

const int rightButton = 5;

const int mouseButton = 6;

int range = 5; 
int responseDelay = 10; 

void setup(){
pinMode(upButton, INPUT);
pinMode(downButton, INPUT);
pinMode(leftButton, INPUT);
pinMode(rightButton, INPUT);
pinMode(mouseButton, INPUT);

Mouse.begin();
Serial.begin(9600);
while (!Serial){
}
}

void loop(){

float sensorValue = analogRead(A1);
float millivolt = (sensorValue/1023)*5;

Serial.print("Sensor Value: ");
Serial.println(sensorValue);

Serial.print("Voltage: ");
Serial.print(millivolt*1000);
Serial.println(" mV");
Serial.println("");
delay(1);     
int upState = digitalRead(upButton);

int downState = digitalRead(downButton);

int rightState = digitalRead(rightButton);

int leftState = digitalRead(leftButton);

int clickState = digitalRead(mouseButton);

int  xDistance = (leftState - rightState) * range;

int  yDistance = (upState - downState) * range;

if ((xDistance != 0) || (yDistance != 0)) {
Mouse.move(xDistance, yDistance, 0);
}

if (clickState == HIGH) {
if (!Mouse.isPressed(MOUSE_LEFT)) {
Mouse.press(MOUSE_LEFT);
}
}
else {
if (Mouse.isPressed(MOUSE_LEFT)){
Mouse.press(MOUSE_LEFT);
}
}
delay(responseDelay);
}

Try this. It's ugly code but it should work fine.

EDIT - Fixed a bug or four.

I tried this. There is an error saying Serial does not have a type for the second line. Is there anything I should add?

I made an edit. Try again.

EDIT - I made another edit. It should work now.

EDIT - I should probably mention that I haven't programmed a Micro/Leonardo before. So some serial stuff might not work at first.

EDIT - Another change. Added a bracket.

Oops

1 Like

The code is now verified! Thank you! I will give it a test on the actual circuit now. Also the first line should be

1 Like

My bad. This is what happens when you use a forum as an IDE.

Hope your project works!

1 Like