Pages: [1]   Go Down
Author Topic: multiple inputs makey makey ciucuit using leonardo  (Read 468 times)
0 Members and 1 Guest are viewing this topic.
Bournemouth
Offline Offline
Full Member
***
Karma: 0
Posts: 133
I am a digital media student at aucb in bounremouth , england.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello , would anyone be able to tell me how to amend the following code to allow for multiple inputs. basically I have two identical circuits one read at a0 and the other at a1. I have tried doubling everything and renaming all outputs and inputs reffering to a1 as output2 or input2. any help would be greatly appreciated , thank you.

Code:
#include <MovingAvarageFilter.h>


// Add the Moving Average Library, use the link below to download the zip file of library
 
 
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 < 200 ) {   // you can change this parameter to fine tune the sensitivity
if (!check){         
Keyboard.print("d");         
Serial.println(output);           
check = !check;   
  }         
  }
 
 
if (output >600) {     
  if (check){               
  check = !check;   
  }     
  }
 
 
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49101
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have an instance of the class MovingAvarageFilter called movingAvarageFilter. Create another one. Call it asgtasdhynregxhcihuaSndg (or something).

Feed one with data from A0. Feed the other with data from A1.
Logged

Bournemouth
Offline Offline
Full Member
***
Karma: 0
Posts: 133
I am a digital media student at aucb in bounremouth , england.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you but i'm not entirely sure what you mean. which exact instance in the code is the class ? Also I have googled it and cannot find something on renaming classes , sorry if this is an ignorant question.

thank you
Logged

Bournemouth
Offline Offline
Full Member
***
Karma: 0
Posts: 133
I am a digital media student at aucb in bounremouth , england.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok figured it out my code is now (below) it seems very convoluted now though ?. also now completion of the circuit results in a continuous string of d's or s's rather than a single one ?

thank you

#include <MovingAvarageFilter.h>


Code:
// Add the Moving Average Library, use the link below to download the zip file of library
 
 
MovingAvarageFilter movingAvarageFilter(20);

 
boolean check = false;
 
 #include <MovingAvarageFilter.h>


// Add the Moving Average Library, use the link below to download the zip file of library
 
 
MovingAvarageFilter movingFilter(20);

 

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

       
 
// declare input and output variables
 
float input2 =  analogRead(1); // without a real input, looking at the step respons (input at unity, 1)
 
float output2 = 0;
 
 
output2 = movingFilter.process(input2);
 
 
// here we call the fir routine with the input. The value 'fir' spits out is stored in the output variable.
 
 
 
if (output < 200 ) {   // you can change this parameter to fine tune the sensitivity
if (!check){         
Keyboard.print("d");         
Serial.println(output);           
check = !check;   
  }         
  }
 
 
if (output >600) {     
  if (check){               
  check = !check;   
  }     
  }
 
 if (output2 < 200 ) {   // you can change this parameter to fine tune the sensitivity
if (!check){         
Keyboard.print("s");         
Serial.println(output2);           
check = !check;   
  }         
  }
 
 
if (output2 >600) {     
  if (check){               
  check = !check;   
  }     
  }
 
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26250
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That looks to me like you're averaging the two sets in the same filter.
Don't you want two separate instances of the moving average?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Bournemouth
Offline Offline
Full Member
***
Karma: 0
Posts: 133
I am a digital media student at aucb in bounremouth , england.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I expect i do ! sorry i am am very new to any kind of programming. how would i go about creating two separate instances ? also do you think this is responsible for the new speed at which the key presses are being generated ?

thank you
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26250
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
MovingAvarageFilter movingFilter(20);
There's one instance.
You could create another with a different name
Code:
MovingAvarageFilter movingFilter1(20);
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Bournemouth
Offline Offline
Full Member
***
Karma: 0
Posts: 133
I am a digital media student at aucb in bounremouth , england.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i thought I had already done this as one was named movingfilter and one movingaveragefilter. here is my new code I have tidied by removing the additional

Code:
#include <MovingAvarageFilter.h>

new code. the problem remains of the continuously generating keystrokes? I notice when I delete the if statements relating to input2 that input 1 goes back to generating a single "d" per contact as it did before?

thank you

Code:
#include <MovingAvarageFilter.h>


// Add the Moving Average Library, use the link below to download the zip file of library
 
 
MovingAvarageFilter movingAvarageFilter(20);
MovingAvarageFilter movingFilter(20);

boolean check = false;
 



// Add the Moving Average Library, use the link below to download the zip file of library
 


 

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

       
 
// declare input and output variables
 
float input2 =  analogRead(1); // without a real input, looking at the step respons (input at unity, 1)
 
float output2 = 0;
 
 
output2 = movingFilter.process(input2);
 
 
// here we call the fir routine with the input. The value 'fir' spits out is stored in the output variable.
 
 
 
if (output < 200 ) {   // you can change this parameter to fine tune the sensitivity
if (!check){         
Keyboard.print("d");         
Serial.println(output);           
check = !check;   
  }         
  }
 
 
if (output >600) {     
  if (check){               
  check = !check;   
  }     
  }
 
 if (output2 < 200 ) {   // you can change this parameter to fine tune the sensitivity
if (!check){         
Keyboard.print("s");         
Serial.println(output2);           
check = !check;   
  }         
  }
 
 
if (output2 >600) {     
  if (check){               
  check = !check;   
  }     
  }
 
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49101
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

The analogRead() function does not return a float. Why are you storing the value in a float? Why are you storing the value at all?

Code:
float output = 0;
 
 
output = movingAvarageFilter.process(input);
Is there some



reason


for all


the blank lines


in your code?

The code I've copied so far could be:
Code:
float output = movingAvarageFilter.process(analogRead(0));

Much more succinct, don't you think?

Use the same technique for the other pin. Use meaningful names, like output0 and output1, not output and output1. The filter names are ridiculous, too. filter0 and filter1 would be clearer.

Put all { on a new line. Use Tools + Auto Format. Get rid of most of the blank lines.

Explain what your problem is better. Using two moving average filters correctly does not seem to be related to your problem at all. Yes, it needed to be done, but I can't see the relationship between that problem and the problem you are complaining about.
Logged

Bournemouth
Offline Offline
Full Member
***
Karma: 0
Posts: 133
I am a digital media student at aucb in bounremouth , england.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you I will make those changes to my code. the problem that is bothering me is that when I complete the circuit , when a0 for instance reads less than 200 it generates the d key rapidly the entire time the circuit is complete. Before adding the the second if statement relating to a1 it would generate one d or would take 1 reading and then cease. now that I've added the second statement completing either circuit cause's continuous readings to be taken at a rapid rate. I added a delay function but I need the first reading to be taken instantly (on circuit completion) and no others to precede it?

thank you
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49101
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I added a delay function but I need the first reading to be taken instantly (on circuit completion) and no others to precede it?
Is this a question or a statement?

What does "instantly" mean? You are reading from an analog device. There is nothing "instantly" about analog reading.

Adding the delay() was the wrong approach.

Fix the code to use meaningful names. Remove the blank lines. Auto Format so it is readable, and post it again.
Logged

Bournemouth
Offline Offline
Full Member
***
Karma: 0
Posts: 133
I am a digital media student at aucb in bounremouth , england.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it was a statement. When I say instantly I suppose I mean without a significant delay in the context of human perception like a key press or a piano note.

here is my tidied code. cheers for helping.

Code:
#include <MovingAvarageFilter.h>

MovingAvarageFilter movingAvarageFilter1(20);
MovingAvarageFilter movingAvarageFilter2(20);

boolean check = false;

void setup() {

  Serial.begin(115200);     
}

void loop() {       

  float output1 = movingAvarageFilter1.process(analogRead(0));       
  float output2 = movingAvarageFilter2.process(analogRead(1));

  if (output1 < 200 ) {   // you can change this parameter to fine tune the sensitivity
    if (!check){         
      Keyboard.print("d");         
      Serial.println(output1);           
      check = !check;   
    }         
  }

  if (output1 >600) {     
    if (check){               
      check = !check;   
    }     
  }

  if (output2 < 200 ) {   // you can change this parameter to fine tune the sensitivity
    if (!check){         
      Keyboard.print("s");         
      Serial.println(output2);           
      check = !check;   
    }         
  }

  if (output2 >600) {     
    if (check){               
      check = !check;   
    }     
  }

}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49101
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One problem that I see, now that all the code fits together in a reasonable amount of space, is that you have only one check variable that you are using for both input/output sets. See if having check1 and check2 makes more sense in terms of getting the output that you want.

Another problem is that you only print output1 and output2 if the value in each is less than 200. For debugging purposes, you should print both on every pass, so that you can confirm that the correct choices are being made.
Logged

Bournemouth
Offline Offline
Full Member
***
Karma: 0
Posts: 133
I am a digital media student at aucb in bounremouth , england.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

that was the ticket! thanks very much its working fine now. >600 readings printed.


cheers

Logged

Pages: [1]   Go Up
Jump to: