Hey guy, me again. ![]()
So this time I had to get 50 random numbers into an array, switch them to ascending order when there's an input, and then output the highest and lowest numbers in the array to LEDs in binary.
I have managed everything but outputting the lowest number in the array. I figured it would be the same as the highest array, but lower down on the LEDs.
Anyway here's the full code...
int incomingByte; //a variable to read incoming serial data into
int Array[50] = {
};
int pinArray[] = {
6, 7, 8, 9, 10, 11, 12, 13
};
int highArray;
int lowArray;
int pinCount = 14; //amount of pins being used on the arduino
void setup() {
//initialize serial communication:
Serial.begin(9600);
for (int thisPin = 0; thisPin < pinCount; thisPin++) //initialize the LED pins as an output
{
pinMode(pinArray[thisPin], OUTPUT);
}
// if analog input pin 0 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
randomSeed(analogRead(0));
for (int i = 0; i < 50; i++) // repeats 50 times
{
Array[i] = random(256); // Puts a random number from 0-255 into the array
Serial.println(Array[i]);
}
}
void loop() {
if (Serial.available() > 0) {
//read oldest byte in the serial buffer:
incomingByte = Serial.read();
sort(Array, 50); //sorts the array
for (int i = 0; i < 50; i++)
{
Serial.print(Array[i]);
Serial.print(",");
}
Serial.println();
lowArray = Array[0];
highArray = Array[49];
Serial.print("Low Array = ");
Serial.println(lowArray);
Serial.print("High Array = ");
Serial.println(highArray);
highestArrayBinary();
delay(5000);
allLedsOff();
lowestArrayBinary();
delay(5000);
allLedsOff();
}
}
void sort(int a[], int sizeOfArray) { // bubble sort to ascending order
boolean swapped = true;
while (swapped)
{
swapped = false;
for (int index = sizeOfArray - 1; index >= 0; index--)
{
if (a[index] < a[index - 1]) {
int tmp = a[index];
a[index] = a[index - 1];
a[index - 1] = tmp;
swapped = true;
}
}
}
}
void highestArrayBinary()
{
int i = 128;
int thisPin = 7;
int remainder = highArray;
while (remainder > 0)
{
while (remainder >= i)
{
double j = i / 2;
digitalWrite(pinArray[thisPin], HIGH);
remainder = remainder - i;
i = i - j;
thisPin--;
}
while (remainder < i)
{
int j = i / 2;
if (j >= 1)
{
i = i - j;
thisPin--;
}
else
{
i = 0;
}
}
}
}
void lowestArrayBinary()
{
int i = 128;
int thisPin = 7;
int remainder = lowArray;
while (remainder > 0)
{
while (remainder >= i)
{
double j = i / 2;
digitalWrite(pinArray[thisPin], HIGH);
remainder = remainder - i;
i = i - j;
thisPin--;
}
while (remainder < i)
{
int j = i / 2;
if (j >= 1)
{
i = i - j;
thisPin--;
}
else
{
i = 0;
}
}
}
}
void allLedsOff()
{
for (int thisPin = 0; thisPin < pinCount; thisPin++) //initialize the LED pins as an output
{
digitalWrite(pinArray[thisPin], LOW);
}
}
When doing the lowest number in the array, for some reason only the last two lights turn on, (1 & 2 in binary) this means that if the Lowest array is 1-3 it shows it. Also if the array would end with the 1st and 2nd pin, (e.g. 19, would be 16,2,1) then 2,1 would turn on but not the 16.
while (remainder < i)
{
int j = i / 2;
if (j >= 1)
{
i = i - j;
thisPin--;
}
else
{
i = 0;
}
I think the error is in this part, though I cannot see anything wrong.
In HighestArrayBinary, I used "i=i-j" rather than "i=i/2" because when i=1 it will not be able to half anymore, so i wont ever equal 0. So I made j a double, meaning it could equal 0.5, then when j < 1, i=0. This way i does eventually equal 0.
I have explained it in as much detail as I think you guys may need, if anybody can see what I have done wrong, any help would be greatly appreciated.
This is for a university assignment due tomorrow, which is why I'm posting here rather than emailing a lecturer, who'd probably not respond until tomorrow.
Attached is a screen shot of the assignment, which should explain in greater detail what I have had to do.
Again, any help would be very welcome and greatly appreciated.
~ Jake
