Changing my code from reading one analog input to multiple

Hello!

My project involves reading the analog input resistance values and when they get to a certain input they send a midi note to a converter on my computer and it plays a note through garage band. I found a way to make one analog input work and now i want to have as many as i can. I am using the arudion uno R3, any help for the best way to do this would be great! Here is my code

#include <MovingAvarageFilter.h>

const int MIDI_ON =144;
const int MIDI_OFF= 128;
const int MIDI_CHANNEL= 0;

MovingAvarageFilter movingAvarageFilter(20);

 

 

boolean check = false;

 

 

 

 

void setup() {

 

Serial.begin(115200);     

 

}

 

 

void loop() {        

 

// declare input and output variables

 

float input =  analogRead(0); // without a real input, looking at the step respons (input at unity, 1)

 

float output = 0;

 

 

output = movingAvarageFilter.process(input);

 

// here we call the fir routine with the input. The value 'fir' spits out is stored in the output variable.

 

 

 

if (output < 1000 ) {   // you can change this parameter to fine tune the sensitivity

if (!check){         
 midiSend (MIDI_ON, 90, 100);
//Keyboard.print("d");         

//Serial.println(output);           

check = !check;   
//Serial.println (output);
  }         

  }

 

 

if (output >1015) {     
//Serial.println ("output >900");
//Serial.println (output);
  if (check){               
   midiSend (MIDI_OFF, 90, 100);
  check = !check;   

  }     

  }

 

 

}
void midiSend( int cmd, int data1, int data2 )
 {
  Serial.write( cmd );
  //Serial.write(' ');
  Serial.write( data1 );
  //Serial.write(' ');
  Serial.write( data2 );
  //Serial.println();
 }

Why

is there so

much

white space in

your

code?

Haha It was from trying to test different variations of the code, I just never deleted it. :blush:

#include <MovingAvarageFilter.h>

Is it really called that? :astonished:

michael71591:
Haha It was from trying to test different variations of the code, I just never deleted it. :blush:

If you want someone to review and critique your essay, you're not going to give them a paper full of chicken scratch.

ok my bad....

#include <MovingAvarageFilter.h>

const int MIDI_ON =144;
const int MIDI_OFF= 128;
const int MIDI_CHANNEL= 0;

MovingAvarageFilter movingAvarageFilter(20);

boolean check = false;

void setup() {

     Serial.begin(115200);     

                   }

 void loop() {        

// declare input and output variables

        float input =  analogRead(0); // without a real input, looking at the step respons (input at unity, 1)

        float output = 0;

        output = movingAvarageFilter.process(input);

        // here we call the fir routine with the input. The value 'fir' spits out is stored in the output variable.

         if (output < 1000 ) 
                        {   
          // you can change this parameter to fine tune the sensitivity

          if (!check)
                        {         
                        midiSend (MIDI_ON, 90, 100);
                         //Keyboard.print("d");         

                         //Serial.println(output);           

                        check = !check;   
                        //Serial.println (output);
                         }         

                         }

                         if (output >1015) 
                          {     
                         //Serial.println ("output >900");
                        //Serial.println (output);
                           if (check)
                           {               
                                   midiSend (MIDI_OFF, 90, 100);
                                   check = !check;   

                            }     

  }

}
void midiSend( int cmd, int data1, int data2 )
 {
  Serial.write( cmd );
  //Serial.write(' ');
  Serial.write( data1 );
  //Serial.write(' ');
  Serial.write( data2 );
  //Serial.println();
 }

This is the MovingAvarageFilter.h code :

#ifndef MovingAvarageFilter_h
#define MovingAvarageFilter_h

#define MAX_DATA_POINTS 20

class MovingAvarageFilter {
public:
  //construct without coefs
  MovingAvarageFilter(unsigned int newDataPointsCount);

  float process(float in);

private:
  float values[MAX_DATA_POINTS];
  int k; // k stores the index of the current array read to create a circular memory through the array
  int dataPointsCount;
  float out;
  int i; // just a loop counter
};
#endif

This is the MovingAvarageFilter.cpp:

#include "MovingAvarageFilter.h"

MovingAvarageFilter::MovingAvarageFilter(unsigned int newDataPointsCount) {
  k = 0; //initialize so that we start to write at index 0
  if (newDataPointsCount < MAX_DATA_POINTS)
	dataPointsCount = newDataPointsCount;
  else
	dataPointsCount = MAX_DATA_POINTS;
  
  for (i=0; i<dataPointsCount; i++) {
    values[i] = 0; // fill the array with 0's
  }
}

float MovingAvarageFilter::process(float in) {
  out = 0;

  values[k] = in;
  k = (k+1) % dataPointsCount;

  for (i=0; i<dataPointsCount; i++) {
    out += values[i];
  }

  return out/dataPointsCount;
}

I don't get why you're summing the array every time.
Just keep a running total, subtract the oldest reading in the buffer, add the newest, and overwrite the oldest with the newest.

michael71591:
I found a way to make one analog input work and now i want to have as many as i can. I am using the arudion uno R3, any help for the best way to do this would be great!

On a UNO, 'as many as you can' means 'six'. Is that enough to be useful?

There are lots of different ways to implement this but the simplest is to use arrays to store each of the things that you need to deal with a single pin:

The pin number (A0 etc).
The 'MovingAvarageFilter' (sic).
Whether the associated note is currently on. (I have no idea why you named this 'check'.)
The pair of int values that define the MIDI command for this input.

Having defined these arrays, implement a FOR loop which performs the logic currently in loop() for each of your inputs. For example,

int input =  analogRead(0); // without a real input, looking at the step respons (input at unity, 1)
int output = movingAvarageFilter.process(input);
if (output < 1000 ) 
{
  if (!check)
  {         
    midiSend (MIDI_ON, 90, 100);
    check = !check;

might become

for(int i = 0; i < INPUT_COUNT; i++)
{
  int filtered = movingAverageFilter[i].process(analogRead(inputPin[i]));
  if(filtered < Threshold)
  {
    if(!noteOn[i])
    {
      midiSend(MIDI_ON, data1[i], data2[i]);
      noteOn[i] = true;
      ... etc

Please note the correct spelling of "average" and also that analogRead returns an int not a float.

As a matter of style, I recommend that you get in the habit of putting each { and } on lines on their own with matching pairs indented to the same level and the lines between them indented one extra level, and severely reduce the quantity of blank lines in your code. As a starting point I suggest you delete every blank line in your existing code that does not have a comment next to it, and any blank like that is immediately before or after a { or }.

Then use the tools/auto format option to indent your code properly.

Hello and thank you for your replies! I found this code online and am new to the c++ world, it was from a foreign guy and didn't know English very well that's why there are the miss spellings. I am basically making a makey makey with arduino(s). Eventually hoping to have enough keys to do a whole octave on a keyboard. Is it right that I can use the pins 0 and 1 to have the arduinos send serial between each other? I tried making the multipe input code but still cannot figure it out here is what i have and the errors i recieve:

#include <MovingAvarageFilter.h>

const int MIDI_ON =144;
const int MIDI_OFF= 128;
const int MIDI_CHANNEL= 0;

MovingAvarageFilter movingAvarageFilter(20);

boolean check = false;
int INPUT_COUNT = 0
void setup() 
            {
   Serial.begin(115200);     
             }
void loop() {        
  for(int i = 0; i < INPUT_COUNT; i++)
  int pitch = 0;
  for(int pitch = 90; < INPUT_COUNT; i++)
  int filtered = movingAverageFilter[i].process(analogRead(inputPin[i]));
{
  if(filtered < 1000 )
  {
    if(!noteOn[i])
    {
      midiSend(MIDI_ON, pitch, 100);
      noteOn[i] = true;
      }
    }
  } 
  if(filtered >1015)
 {
 if(!noteOn[i])
    {
      midiSend(MIDI_OFF, pitch, 100);
      noteOn[i] = false ;
    }
  }
}
void midiSend( int cmd, int data1, int data2 )
{
  Serial.write( cmd );
  //Serial.write(' ');
  Serial.write( data1 );
  //Serial.write(' ');
  Serial.write( data2 );
  //Serial.println();
}

Errors :

touch_sensor:-1: error: expected unqualified-id before numeric constant
touch_sensor:11: error: expected ',' or ';' before 'void'
touch_sensor.cpp: In function 'void loop()':
touch_sensor:18: error: expected primary-expression before '<' token
touch_sensor:18: error: name lookup of 'i' changed for new ISO 'for' scoping
touch_sensor:16: error: using obsolete binding at 'i'
touch_sensor:19: error: 'movingAverageFilter' was not declared in this scope
touch_sensor:19: error: 'inputPin' was not declared in this scope
touch_sensor:21: error: 'filtered' was not declared in this scope
touch_sensor:23: error: 'noteOn' was not declared in this scope
touch_sensor:30: error: 'filtered' was not declared in this scope
touch_sensor:32: error: 'noteOn' was not declared in this scope

int INPUT_COUNT = 0

Luckily, I've got a spare. Here, you have it. ;

Is there missing code? Haha it seems a little too short...