Hi I am coding a raspberry Pi to light up 5 LEDS for a binary number. I am missing some code because I am not sure what to put there, and my output is not coming out correctly. My output is below
How many bits? 5
Enter the pin number for LED 1: 0
Enter the pin number for LED 2: 3
Enter the pin number for LED 3: 21
Enter the pin number for LED 4: 22
Enter the pin number for LED 5: 23
Number to Binary Converter
Enter number between 127 and -128 to be converted to binary. Enter a non-numeric input to end.
3
3 is out of range.
Enter a number within 0, -1413512704 to be respresented.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <wiringPi.h>
void numberToBinary(int arr[], int numOfBits, int userNum);
void addOne(int arr[], int numOfBits);
void flipBits(int arr[], int numOfBits);
void printArray(int binaryArr[], int numberOfElements);
void binaryToLED(int binaryArr[], int numOfBits, int pinNumbers[]);
int main(void) {
if (wiringPiSetup() == -1) {
puts("Raspberry Pi setup failed. Check the wiringPi library. ");
return 1;
}
int numOfBits;
printf("How many bits? ");
scanf("%d", & numOfBits);
int pinNumbers[numOfBits];
int userPin;
for (int i = 0; i < numOfBits; i++) {
printf("Enter the pin number for LED %d: ", i + 1);
scanf("%d", & userPin);
pinNumbers[i] = userPin;
pinMode(pinNumbers[i], OUTPUT);
}
int binaryArr[numOfBits];
int upperBound = pow(2, numOfBits - 1) - 1;
int lowerBound = pow(2, numOfBits - 1);
int userNum;
printf("\n \n%38s", "Number to Binary Converter");
printf("\n \n%44s", "Enter number between 127 and -128 to be converted to binary. Enter a non-numeric input to end.");
printf("\n \n\n");
while (scanf("%d", & userNum) == 1) {
if ((userNum < lowerBound) || userNum > upperBound) {
printf("%d is out of range. \n", userNum);
printf("Enter a number within [%d, %d] to be respresented.");
continue;
}
for (int i = 0; i < numOfBits; i++) {
binaryArr[i] = 0;
}
numberToBinary(binaryArr, numOfBits, abs(userNum));
if (userNum < 0) {
flipBits(binaryArr, numOfBits);
addOne(binaryArr, numOfBits);
}
printf("Binary: ");
printArray(binaryArr, numOfBits);
puts("");
binaryToLED(binaryArr, numOfBits, pinNumbers);
}
return EXIT_SUCCESS;
}
void addOne(int arr[], int numOfBits) {}
void printArray(int binaryArr[], int numberOfElements) {}
void numberToBinary(int binaryArr[], int numOfBits, int userNum) {}
void flipBits(int binaryArr[], int numOfBits) {
for (int i = 0; i < numOfBits; i++) {
if (binaryArr[i] == 1) {
binaryArr[i]++;
}
}
return;
}
void binaryToLED(int binaryArr[], int numOfBits, int pinNumbers[]) {
for (int i = 0; i < numOfBits; i++) {
digitalWrite(pinNumbers[i], binaryArr[i]);
}
delay(5000);
for (int i = 0; i < numOfBits; i++) {
digitalWrite(pinNumbers[i], 0);
}
return;
}