Inspiration:
I struggled to think of something creative to make, but I eventually settled on a kind of game where a red, a green, and a blue LED would light up, each with a random brightness, and then the player would have to use a potentiometer to control the brightnesses of each of the three LEDs in order to match the random brightness that they had. If the player was within a certain range, a green LED would light up, signifying that the player was correct, otherwise a red LED would light up signifying that the player was too far off.
Process:
I used some of the code from the Maintain State circuit we built, along with some from the Analog LED circuit from class as well.
Challenges:
My idea was quite complicated, especially for someone new at coding like me, and I spent too long trying to work it out. I eventually realized that I could have the same process with a blue LED that the player would control, which would make the circuit much simpler. I simplified my code and rebuilt my circuit, although for some reason, I encountered the same problem I had before, which was that all three LEDs would light up, and neither the button nor the potentiometer would change that.
I tinkered a little more, and the video shows as far as I got, however because I spent so much time on the more complicated version of this idea, I did not have enough time to completely troubleshoot either the code or the circuit. I had initially thought for a long time that the problem must have been somewhere in the code, but I think now that it might be in the circuit.
Final Work:
Diagram of my circuit on TinkerCAD
Code:
//potentiometer
const int knob = A0;
//target blue LED
const int bled = 5;
//incorrect and correct LEDs
const int iled = 9;
const int cled = 10;
//blue button
const int bbutton = 2;
//on/off button
const int obutton = 12;
bool bledOn = false;
bool iledOn = false;
bool cledOn = false;
//on off button
bool oButtonOnOff = false;
bool oButtonPrevOnOff = LOW;
//blue button
bool bButtonOnOff = false;
bool bButtonPrevOnOff = LOW;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(bled, OUTPUT);
pinMode(iled, OUTPUT);
pinMode(cled, OUTPUT);
pinMode(bbutton, INPUT);
pinMode(obutton, INPUT);
}
void loop() {
//turning the LED on and off with the BLACK button
bool oButtonCurrentState = digitalRead(obutton);
int randomLED = random(255);
if(oButtonCurrentState == HIGH && oButtonCurrentState != oButtonPrevOnOff){
digitalWrite(bled, randomLED);
}
oButtonPrevOnOff = oButtonCurrentState;
bledOn = !bledOn;
//turning the LED on and off with the BLUE button
bool bButtonCurrentState = digitalRead(bbutton);
if(bButtonCurrentState == HIGH && bButtonCurrentState != bButtonPrevOnOff){
digitalWrite(bled, 255);
}
bButtonPrevOnOff = bButtonCurrentState;
bledOn = true;
int knobValue = analogRead(knob);
//fix high and low based on potentiometer
int mappedValue = map(knobValue, 850, 350, 0, 255);
int constrainedValue = constrain(mappedValue, 0, 255);
digitalRead(bled);
if(bled == HIGH && bButtonPrevOnOff == HIGH){
analogRead(knob);
analogWrite(bled, constrainedValue);
}
int bledValue = analogRead(bled);
if(bButtonCurrentState == HIGH && (randomLED - 5) < (bledValue) < (randomLED + 5)){
bool cledOn = !cledOn;
}
if(bButtonCurrentState == LOW && (bledValue) < (randomLED - 5) || (randomLED + 5) < (bledValue)){
iledOn = !iledOn;
}
}
