Last active
April 11, 2017 16:52
-
-
Save konsfik/e60e813879f0cac44808f2de2b80aea2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Github repo: Programmable Digital Synthesizers | |
Project name: Monk | |
Author: Kostas Sfikas | |
Date: April 2017 | |
*/ | |
// declare the array to hold the analog pins values | |
byte myArray[13]; | |
const int numReadings = 10; //number of readings | |
int readings[numReadings]; //array containing all the readings | |
int readIndex = 0; //index of current reading | |
int readAverage = 0; | |
void setup() | |
{ | |
Serial.begin(115200); | |
for(int thisReading = 0; thisReading < numReadings; thisReading++){ | |
readings[thisReading] = 0; | |
} | |
} | |
void loop(){ | |
// The main programme loop | |
int byteIndex = 0; | |
//set the first Byte to 0xc0 (this will be recognized as the start character of the message) | |
myArray[byteIndex] = 0xc0; // start character (binary: 1100 0000) (decimal:192) (hex: c0) | |
// go through the analog pins one by one | |
for(int i = 0; i < 6; i++) { //cycles through potentiometers 0 to 5 -> A0 to A5 | |
unsigned int knob = getSmoothReading(i);// analogRead(i); | |
byte knobByte1 = knob & 0x007f; //^ keeps the last 7 digits of a number... (007f -> binary: 0111 1111) | |
byte knobByte2 = knob >> 7; // moves the bits by 7 positions to the right... | |
byteIndex++; //advance index (index-values at this position: 1, 3, 5, 7, 9, 11) | |
myArray[byteIndex] = knobByte1; // calculated above | |
byteIndex++; //advance index (ndex-values at this position: 2, 4, 6, 8, 10, 12) | |
myArray[byteIndex] = knobByte2; // calculated above | |
} | |
Serial.write(myArray, 13); | |
} | |
int getSmoothReading(int potIndex){ | |
/* This function smooths out the arduino's analog readings (eliminates some of the "noise"). | |
It does so by reading multiple times from the same potentiometer and then calculating the average value. */ | |
int addedValues = 0; // will hold the added values of all readings | |
for(int i = 0; i < numReadings; i++){ // loop for reading multiple values from the same potentiometer | |
readings[i] = analogRead(potIndex); // put the reading in the array, at the current index | |
delayMicroseconds(500); // a very small delay to ensure correct readings | |
} | |
for(int i = 0; i < numReadings; i++){ //loop for adding the values together | |
addedValues += readings[i]; | |
} | |
readAverage = addedValues / numReadings; //calculate the average value | |
return readAverage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment