Adc conversion code

Hello guys I'm a newbie programmer,
I'm trying to use this code but I need to use analogue data from the circuit but my code reads only digital, how can I convert it?

#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);
}

Thanks :slight_smile:

except when it reads analogue data

float sensorValue = analogRead(A1);

I meant that I want it to use the analogue data for the readings:

int upState = digitalRead(upButton);
int downState = digitalRead(downButton); int rightState = digitalRead(rightButton); int leftState = digitalRead(leftButton);
int clickState = digitalRead(mouseButton);

Why would you use analogue data when reading digital pins ?

You have a problem being a newbie.

You really need to play around with coding to be able to have a go at making your changes , or just writing your own code . It’s never a great start to copy some code and lash it up to work - you’ll get frustrated when you can’t get it to work and won’t learn much .

It’s not clear what you want to do anyway - do you want analog signals or just use an analog joystick to produce your digital signals ?

The circuit we have uses EMG to read the electrical signals from muscles and convert them into voltages, these signals will be analog while the code we have right now only reads digital signals so we want to add to our code so that this analog signal is converted to a digital in order for the code to read it.

Hello
Post the terminal output of your sketch

Hmmm ..

So you read the analog value and compare it with a number , say 500. If it’s above that , then make your variable a “1” ; if below it make it a “0”.

Don’t run your Arduino via a mains derived power supply , a fault here could put mains voltage on your well connected victim . Medical experiments need a good knowledge of suitable electrical systems .

I see what you mean! Would that also mean I have to change the output ports in the arduino. As of now I am using Arduino Leonardo. The point of the project is to create a mouse which moves due to muscle contraction. The circuit and the code I already have use a digital port output. Would this mean that I have to change the ports from the digital ports to analogue port and add a variable for each of the different readings?

The ideal situation would be like this. Instead of the MyoWare (the red component on the left) I have my own circuit which I build which does the same function (converting the electrical signals to voltages). Now the code for the mouse does not use the analog ports , but it uses digital ports. So what I want to do is to edit the code in order for the readings to be converted into digital signals so that then I can read them. This should ideally be done using some sort of ADC code.

Where are you stuck ?

There is very seldom a benefit to writing more than one statement per line and it makes the sketch harder to read. Below is a re-format of your sketch.

Your sketch currently moves your mouse by +/- 'range' steps in X or Y depending on which switches are activated. You have one analog input (raw in sensorValue and scaled in millivolt). How do you want that to move the cursor? I assume your mouse is set for relative motion so xDistance is how far to move the cursor in the X direction and yDistance is how far to move the cursor in the Y direction. You just have to calculate those two values from your analog input(s).

#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);
}

I am not 100% sure how to do the ADC code because in this case I have a lot more going on. To put more into context I have done an ECG code a couple of weeks ago using and ADC code in order for me to see the signal. However that time I was only looking at the signal rather than getting readings out of it. This was the code

int x;
int Dataisready ;

// -------- Functions --------- //

static inline void initTimer(void) {
  //timer 1 is initialised
  TIMSK1 |= (1 << TOIE1); // set as 0 bit
  TCCR1B = 0b00000011;
} // set as 64 bit

static inline void initIOPorts(void) {
  // ports needed are intialised
  DDRB |= 0b00110000;
} // sets pins 4 and 5 as output

static inline void initADC(void) {
  // adc is initialised
  ADMUX |= (1 << REFS0); // set as bit 6
  ADCSRA |= (1 << ADPS2) | (1 << ADPS1); // division factor is set 64
  ADCSRA |= (1 << ADIE); // set as bit 3
  ADCSRA |= (1 << ADEN); // set as bit 4

}

// ------ Interrupt Service Routine ------ //
ISR(TIMER1_OVF_vect) {

  // interrupt service routine for timer 1 overflow
  TCNT1 = 65036; // timer count set as 65036 as readings are taken every 2 ms
  ADCSRA |= (1 << ADSC); // set as bit 6
}

ISR(ADC_vect) {
  //interrupt service routine for ADC it's executed when adc finishes a conversion
  x = ADC; //adc value stored in variable x

  Dataisready = 1 ;



}


int main(void) {
  Serial.begin(250000);
  noInterrupts();
  initIOPorts();
  initTimer();
  initADC();
  interrupts();

  while (1) {
    Serial.flush(); // waits for transmission of outgoing serial data to complete

    if (Dataisready == 1) {

      Serial.println (x);
      Dataisready = 0 ;
    } //used to set a loop in which new adc value can be intialised

  }


  return 0;                            /* This line is never reached */
}

In this case I used an Arduino uno. As you can see I included timers and 1 variable which was A0 . This was for me to get the signal out of A0. Should I do something similar here? Including also the timers and everything I have included in the ECG?

So when the muscle contracion would exceed a certain value the mouse cursor would move in the direction that we have set it to. For example if we have set the right arm muscles to be responsible for the movement of the mouse in the upwards direction, when you contract those muscles than the cursor will move up. The same thing applies for all the 4 movements (up, down, left and right) and it also applies for the left click of the mouse.

These values will indeed be calculated from the analog input

This is how you would do the same thing as a regular Arduino sketch.

void setup()
{
  Serial.begin(250000);
  while (!Serial) {}
}

void loop()
{
  static unsigned long lastReadTime = micros();

  // Every 2 milliseconds (2000 microseconds)...
  if (micros() - lastReadTime >= 2000)
  {
    lastReadTime += 2000;
    Serial.println(analogRead(A0));
  }
}

So this is the editing I made. The error is telling me I cannot use micro as a function.

#include <Mouse.h>
const int micro;
const int upButton = 2;
const int downButton = 3;
const int leftButton = 4;
const int rightButton = 5;
const int mousaeButton = 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);


  static unsigned long lastReadTime = micro();
  // every 2 millisecond (2000 micro seconds)

  if (micro() - lastReadTime >= 2000 )
  {
    lastReadTime +=2000 
    Serial.println(analogRead(A0));
    }

  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);
}

Could you help me fix it?

Why did you tell the compiler that 'micro' was an integer constant and then try to call it as if it were a function? In my example code, I used the built-in function 'micros()' to get the elapsed time in microseconds.

So you need FIVE analog inputs, not one.

"exceed a certain value" What certain value?
"the mouse cursor would move" How fast?

Here is something that will do sort of what you are hinting you want done:

#include <Mouse.h>

int range = 5;
unsigned responseDelay = 10;

const int ACertainValue = 500;

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

void loop()
{
  static unsigned long lastReadTime = millis();
  
  // every 'responseDelay' millisecond
  if (millis() - lastReadTime >= responseDelay )
  {
    lastReadTime += responseDelay;

    int upState = analogRead(A0) > ACertainValue;
    int downState = analogRead(A1) > ACertainValue;
    int rightState = analogRead(A2) > ACertainValue;
    int leftState = analogRead(A3) > ACertainValue;
    int clickState = analogRead(A4) > ACertainValue;

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

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

    if (clickState == true)
    {
      if (!Mouse.isPressed(MOUSE_LEFT))
      {
        Mouse.press(MOUSE_LEFT);
      }
    }
    else
    {
      if (Mouse.isPressed(MOUSE_LEFT))
      {
        Mouse.release(MOUSE_LEFT);
      }
    }
  }
}