Pages: [1]   Go Down
Author Topic: Does this code look right?  (Read 392 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How does this code look? Its supposed to run multiple PIEZO's and send the data to my serial program.

//###############################################
//VARIABLES
//###############################################
const int numOfAnalogs = 3; //Change the integer to incorporate the amount of analog inputs you use.
int       midiChannel        = 9;
int       outPin             = 13;       // the number of the output pin, main LED
int       analogSmooth       = 2;        // Amount of change needed to send control change...
int       lastAnalogArray[numOfAnalogs];
int       reading[numOfAnalogs];
//###############################################
//END OF VARIABLES
//###############################################
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  for (int i = 0; i<numOfAnalogs; i++) {
    reading = analogRead(i)/8; // change 0-1024 into 0-127
    if (abs(reading-lastAnalogArray) > analogSmooth) {
      lastAnalogArray = reading;
      sendToSerial(i,reading);
    }
  }
}
void sendToSerial(byte pin, byte velocity) {
  digitalWrite(outPin,HIGH);  // indicate we're sending MIDI data
  Serial.print(pin, BYTE);
  Serial.print(velocity, BYTE);
  digitalWrite(outPin,LOW);  // indicate we're sending MIDI data
}
« Last Edit: May 29, 2008, 01:55:13 pm by acidhax » Logged

Paris FR
Offline Offline
Full Member
***
Karma: 0
Posts: 155
cute little geek...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
const int numOfAnalogs = 3; //Change the integer to incorporate the amount of analog inputs you use.

It is usually a good idea to uppercase the constants, to avoid confusion with variables. No big deal here, but this may be confusing on bigger projects. So you'd better use :

#define       NUM_OF_ANALOGS      3

Quote
   reading = analogRead(i)/8; // change 0-1024 into 0-127

Arduino provides the map() function to do such things. Assigning the result of a division to an integer may (will smiley ) lead to a loss of precision.

T.
« Last Edit: May 29, 2008, 02:19:28 pm by tehboii » Logged

... could use some sleep

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are correct! semester one revision here i go! smiley-razz
Logged

Pages: [1]   Go Up
Jump to: