- Code: Select all
int num[50];
void setup()
{
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
Serial.begin(9600);
}
void loop()
{
reset();
numberGen();
numberSort();
numberDisplay();
delay(3000);
}
void reset()
{
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
}
void numberGen()
{
for( int i = 0 ; i < 50 ; i++ )
{
num[i] = random(256);
}
}
void numberSort()
{
int sizeOfArray = sizeof(num)/sizeof(int);
int expect = 1;
if ( expect != 0 )
{
{
expect = 0;
for( int a = 0 ; a < sizeOfArray ; a++ )
{
for ( int b = 1 ; b < sizeOfArray ; b++ )
{
if( num[b-1] > num[b] )
{
int temp = num[b-1];
num[b-1] = num[b];
num[b] = temp;
expect++;
}
}
}
numberSort();
}
}
expect = 0;
for( int i = 0 ; i < sizeOfArray ; i++ )
{
for ( int j = 1 ; j < sizeOfArray ; j++ )
{
if( num[j-1] > num[j] )
{
int temp = num[j-1];
num[j-1] = num[j];
num[j] = temp;
expect++;
}
}
}
numberSort();
}
void numberDisplay()
{
int i = 10;
int a = num[50];
Serial.println(num[50]);
while ( a > 0 )
{
int num = a % 2;
a = a / 2;
if( num == 1 )
{
digitalWrite(i, HIGH);
}else{
digitalWrite(i, LOW);
}
i--;
}
}
I know that code does not work as when it loops round count is reset to 1 so the condition will never be true thus meaning it will loop round forever and ever. and my binary conversion is wrong here is an example a friend told me of how mine goes and how it should go:
Your binary conversion doesn't work though... this is your
numberDisplay() code. Here's an example. If you step through it with
the number as 13.
----------
i = 12
a = 13
while a > 0
num = 1
a = 6
turn pin 12 on
i = 11
num = 0
a = 3
turn pin 11 off
i = 10
num = 1
a = 1
turn pin 10 on
i = 9
num = 1
a = 0
turn pin 9 on
Result: 000000000101
----------
This is 5 in binary, not 13. Also, this only displays the number at
position 50 in the array. You need to display every number in the
array.
please could someone help me with what i'm trying to do been at it for days and still now luck.

