void setup() {
// put your setup code here, to run once:
DDRC = B11111111; // Pins 30 to 37
Serial.begin(9600);
}
void printBits(byte number, int bits) {
String numString = String(number, BIN);
while (numString.length() < bits) {
numString = "0" + numString;
}
Serial.println(numString);
}
void setBits(byte number, int bits) {
printBits(number, bits);
PORTC = number;
}
int parseBitAmount(int bitsAmount) {
if (bitsAmount > 8) {
Serial.println("Bits amount not supported! Bits set to 8");
bitsAmount = 8;
}
return bitsAmount;
}
void loop() {
// put your main code here, to run repeatedly:
static int bits = 0;
static byte currentNumber = B0;
static long lastDelay = millis();
static int wantedDelayMS = 1000;
while (!bits) {
if (lastDelay + 1000 < millis()) {
Serial.println("No bits amount set! Please input desired amount of bits!");
lastDelay = millis();
}
if (Serial.available()) {
bits = parseBitAmount(Serial.parseInt());
Serial.print(bits);
Serial.println(" set as desired bits!");
Serial.print("Using pins 2 to ");
Serial.print(1+bits);
Serial.println(" to output numbers as binary");
Serial.println("");
Serial.println("You can change desired amount of bits by writing : b<number>");
Serial.println("And change the delay between bits as millis by writing : d<number>");
}
}
if (Serial.available()) {
int command = Serial.read();
if (command == 'b') {
Serial.println("New byte value given!");
bits = parseBitAmount(Serial.parseInt());
Serial.print(bits);
Serial.println(" bits is now set as the new amount!");
}
else if (command == 'd') {
Serial.println("New delay value given!");
wantedDelayMS = Serial.parseInt();
Serial.print("The new delay is now ");
Serial.print(wantedDelayMS);
Serial.println(" Millis!");
}
}
if (lastDelay + wantedDelayMS < millis()) {
setBits(currentNumber, bits);
currentNumber ++;
if (currentNumber > pow(2,bits)) {
currentNumber = 0;
}
lastDelay = millis();
}
}