Go Down

Topic: Does this code look right? (Read 477 times) previous topic - next topic

acidhax

May 29, 2008, 08:49 pm Last Edit: May 29, 2008, 08:55 pm by acidhax Reason: 1
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
}

tehboii

#1
May 29, 2008, 09:18 pm Last Edit: May 29, 2008, 09:19 pm by tehboii Reason: 1
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 :) ) lead to a loss of precision.

T.
... could use some sleep

acidhax

You are correct! semester one revision here i go! :P

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy