#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define SEED 420
#define bobSize 8
#define wait 2000
bool bob[bobSize];
unsigned int pos = 0;
unsigned int a;
unsigned int b = 12;
void setup() {
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
for (int i = 0; i < SEED; i++){
random(64);
}
display.display();
display.setTextSize(1);
display.setTextColor(1);
delay(200);
display.clearDisplay();
}
void loop() {
display.clearDisplay();
shuffleBob();
int d = select(bob);
display.setCursor(80, 0);
display.println(d);
display.display();
delay(wait);
}
void shuffleBob() {
for (int i = 0; i < bobSize; i++) {
bob[i] = random(2);
}
display.setCursor(0, 0);
for (int i = 0; i < bobSize; i++) {
display.print("bob item:");
display.print(i);
display.print(" ");
display.println(bob[i]);
}
}
int select(bool pow[]){
unsigned int a = 0;
unsigned int b = 12;
unsigned int pos = 0;
for (int i = 0; i < bobSize; i++) {
if (pow[i]) {
pos++;
}
}
a = random(pos);
for (int i = 0; i < bobSize; i++) {
if (b == 12) {
if (pow[i]) {
if (a == 0) {
b = i;
}
a--;
}
}
}
return b;
}