Programming Issue need help

Hello! I am a new programmer and I am trying to code a program which will act as a mouse. The way it will work is your muscled will be connected to electrodes. I will find the corresponding voltages of these contractions and by contracting the muscles this voltage will go up. After it passes a threshold the mouse functions should work. I am using Arduino Uno to code the process and using a different program which will act as a mouse. As of now I am trying to get the mouse to just left click with the contraction but it is not working. What is happening is the mouse cursor is just going to the top left corner and is stuck there. I am not sure what to change in my code to make the mouse click.
Here is the code:

#include "Mouse.h"

// declaring variables

 int upButton = A0;

 int downButton = A1;

 int leftButton = A2;

 int rightButton = A3;

 int mouseButton = A4;
 
 int inByte = 0;         // incoming serial byte
 int x=0;
 int y=0;
 int c=0;
 bool collectData=false;

int upState = digitalRead(upButton);

int downState = digitalRead(downButton);

int rightState = digitalRead(rightButton);

int leftState = digitalRead(leftButton);

int clickState = digitalRead(mouseButton);

int ACertainValue ;


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(A0);
 int millivolt = analogRead(A0);

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

Serial.print("Voltage: ");
Serial.print(millivolt*1000);
Serial.println(" mV");
Serial.println("");
delay(1); 

  // if we get a valid byte, read analog ins:
   if (Serial.available() > 0) {
     // get incoming byte:
     inByte = Serial.read();
     if (inByte==255) {collectData=true;}
     else if (inByte==224) {collectData=false;}
   }
   if (collectData) {

    x = 0;
    y = 0;
    c = 0;


//    if 
    
//    ( downState = analogRead(A1) > ACertainValue);
//    { y = -1;
//    }
//    if 
  
//       ( rightState = analogRead(A2) > ACertainValue);

//       { x = 1;
//       }
    
    
    //  if 
    
      // ( leftState = analogRead(A3) > ACertainValue);

      // { x = -1;
 /      }
  
    if 
  
       ( clickState = analogRead(A4) > ACertainValue);
       { c = 1;
}

         Serial.write(x); 
         Serial.write(y);
         Serial.write(c);
        delay(10);//probably need a delay here. 
       }
     
{
  static unsigned long lastReadTime = millis();
  
  // every 'responseDelay' millisecond
  if (millis() - lastReadTime >= responseDelay )
  {
    lastReadTime += responseDelay;
     
    

   int xDistance = (leftState - rightState) * range;
    int yDistance = (upState - downState) * range; 

      }
    }
}



    
/*int upState = digitalRead(upButton);

int downState = digitalRead(downButton);

int rightState = digitalRead(rightButton);

int leftState = digitalRead(leftButton);

int clickState = digitalRead(mouseButton); */


Indent the code and double check all your tests. the trailing ; is saying that if the condition is true, do nothing...

PS: when posting code, please remove all the commented out lines so that we can focus on what you really do and post code that at least compile... Not the case here.

The code is verified and I am struggling to find the mistake. The serial monitor should be printing 001 repeating as the c value stands for clicking.

#include "Mouse.h"

// declaring variables

 int upButton = A0;

 int downButton = A1;

 int leftButton = A2;

 int rightButton = A3;

 int mouseButton = A4;
 
 int inByte = 0;         // incoming serial byte
 int x=0;
 int y=0;
 int c=0;
 bool collectData=false;

int upState ;
int downState;
int rightState; 
int leftState ;
int clickState ;

//int upState = digitalRead(upButton);

//int downState = digitalRead(downButton);
 
//int rightState = digitalRead(rightButton);

//int leftState = digitalRead(leftButton);

//int clickState = digitalRead(mouseButton);

int ACertainValue ;


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(){





  // if we get a valid byte, read analog ins:
   if (Serial.available() > 0) {
     // get incoming byte:
     inByte = Serial.read();
     if (inByte==255) {collectData=true;}
     else if (inByte==224) {collectData=false;}
   }

      pinMode( 7 ,INPUT) ;
      
      if 
 
   (digitalRead(7) == HIGH) {

    collectData = true;
   }

  
   
   if (collectData) {

    x = 0;
    y = 0;
    c = 0;


 
  
    if 
  
       ( clickState = analogRead(A4) > 600000);
       { c = 1;
}
 Serial.print(x);
 Serial.print(y);
 Serial.println(c);
//         Serial.write(x); 
//         Serial.write(y);
//        Serial.write(c);
        delay(10);//probably need a delay here. 
       }
     
{
  static unsigned long lastReadTime = millis();
  
  // every 'responseDelay' millisecond
  if (millis() - lastReadTime >= responseDelay )
  {
    lastReadTime += responseDelay;
     
    

   int xDistance = (leftState - rightState) * range;
    int yDistance = (upState - downState) * range; 

      }
    }
}



    
/*int upState = digitalRead(upButton);

int downState = digitalRead(downButton);

int rightState = digitalRead(rightButton);

int leftState = digitalRead(leftButton);

int clickState = digitalRead(mouseButton); */


There are many problems with this line. Please explain what it should do?

There you still go or go again.

The semicolon is syntactically correct but almost certainly not what you want to do.

an if statement looks like

    if ( <some condition>) {
        // perform the statements within the braces
        // if the condition is true
    }

No semicolon. In your code The semicolon is the end of the if statement, and your code in the braces always is performed, it’s an entirely separate statement.

Welcome to C/C++, no smarter than any other program language. :expressionless:

Braces must match. using the IDE’s autoformat tool will sometimes show you where your logic isn’t exactky what you think.

HTH

a7

analogRead(A4) > 600000

What range of values does analogRead() return on a Uno ?

If the analog value would be higher than the value stated the mouse should start clicking. Probably I have changed the value now and that line is

   if 
  
       ( clickState = analogRead(A4) > 1800);
       { c = 1;
}

The range of values which it should work within are
Between 1.8 - 2V so that would be 1800- 2000 mV

Would you say that now this part of the code is fine?

    if 
  
       (clickState = analogRead(A4) > 1800)
       { x = 0;
        y = 0; 
        c = 1;
}

Please read #5 above.

That semicolon is subverting your intent.

    if  ( clickState = analogRead(A4) > 1800) { 
        c = 1;
    }

a7

Haha, yes, except the indenting of the closing brace, that’s just a style matter. The IDE can autoformat your code…

a7

I ask again, what range of values does analogRead() return on a Uno ?

0 to 1023

so what are the chances of analogRead(A4) > 1800 being true?

At some point you will realise that voltages on the analog pin are converted to those 0 - 1023 values. If I tell you that 0 = 0V and 1023 = 5V does that help?

Steve