Created
March 26, 2013 20:21
-
-
Save TeamBlackFlyingRobots/5248852 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| const int buttonPin = 2; // microswitch analog pin # | |
| const int ledPin = 11; // LED digital pin # | |
| const int pwPin = 10; // pulse width mod pin for sonic rangefinder | |
| int buttonState = 0; // is switch HIGH or LOW? | |
| int sonicRangeFinderDistance = 50; // see end of loop() | |
| // quantity of values to find the median (sample size). Needs to be an odd number | |
| int arraysize = 9; | |
| // declare an array to store the samples. not necessary to zero the array values here, it just makes the code clearer | |
| int rangevalue[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0}; | |
| long pulse; | |
| int modE; | |
| void setup() { | |
| pinMode(buttonPin, INPUT); // init microswitch as INPUT | |
| pinMode(ledPin, OUTPUT); | |
| Serial.begin(9600); // init serial monitor | |
| delay (500); // wait for serial connection | |
| } | |
| void loop() { | |
| buttonState = digitalRead(buttonPin); // read the state of switch | |
| if (buttonState == HIGH) { // if switch pressed | |
| Serial.println("HIGH"); | |
| } | |
| else { // if not pressed | |
| Serial.println("LOW"); | |
| } | |
| pinMode(pwPin, INPUT); | |
| for(int i = 0; i < arraysize; i++) | |
| { | |
| pulse = pulseIn(pwPin, HIGH); | |
| rangevalue[i] = pulse/58; | |
| delay(10); | |
| } | |
| Serial.print("Unsorted: "); | |
| printArray(rangevalue,arraysize); | |
| isort(rangevalue,arraysize); | |
| Serial.print("Sorted: "); | |
| printArray(rangevalue,arraysize); | |
| modE = mode(rangevalue,arraysize); | |
| Serial.print("The mode/median is: "); | |
| Serial.print(modE); | |
| Serial.println(); | |
| // This checks both the sonic rangefinder and | |
| // micro-switch to see if they're close enough | |
| // to/touching a surface. | |
| if (buttonState == HIGH && modE < sonicRangeFinderDistance) { | |
| Serial.println("BOTH ON"); | |
| digitalWrite(ledPin, HIGH); | |
| } | |
| else { | |
| digitalWrite(ledPin, LOW); | |
| } | |
| delay(1000); | |
| } | |
| /*-----------Functions------------*///Function to print the arrays. | |
| void printArray(int *a, int n) { | |
| for (int i = 0; i < n; i++) | |
| { | |
| Serial.print(a[i], DEC); | |
| Serial.print(' '); | |
| } | |
| Serial.println(); | |
| } | |
| //Sorting function | |
| // sort function (Author: Bill Gentles, Nov. 12, 2010) | |
| void isort(int *a, int n){ | |
| // *a is an array pointer function | |
| for (int i = 1; i < n; ++i) | |
| { | |
| int j = a[i]; | |
| int k; | |
| for (k = i - 1; (k >= 0) && (j < a[k]); k--) | |
| { | |
| a[k + 1] = a[k]; | |
| } | |
| a[k + 1] = j; | |
| } | |
| } | |
| //Mode function, returning the mode or median. | |
| int mode(int *x,int n){ | |
| int i = 0; | |
| int count = 0; | |
| int maxCount = 0; | |
| int mode = 0; | |
| int bimodal; | |
| int prevCount = 0; | |
| while(i<(n-1)){ | |
| prevCount=count; | |
| count=0; | |
| while(x[i]==x[i+1]){ | |
| count++; | |
| i++; | |
| } | |
| if(count>prevCount&count>maxCount){ | |
| mode=x[i]; | |
| maxCount=count; | |
| bimodal=0; | |
| } | |
| if(count==0){ | |
| i++; | |
| } | |
| if(count==maxCount){//If the dataset has 2 or more modes. | |
| bimodal=1; | |
| } | |
| if(mode==0||bimodal==1){//Return the median if there is no mode. | |
| mode=x[(n/2)]; | |
| } | |
| return mode; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment