CONCEPT:
This project is an upgrade from my project (Red or Green?)from last week. The concept is the same. Four LEDs will be blinking (in a random fashion) and you must use the push button to catch a colour or your choice. You may use the potentiometer to change the speed of the blinking to make the game harder or easier for yourself.
BEHAVIOUR:
The assignment states that I must use one digital input and one digital output to control at least two LEDs in a digital and analog fashion respectively. The push button (digital input) is used to pause the entire system and in a sense catch a colour. Because of its binary nature, it satisfies the digital input requirement of the assignment. Moreover, the potentiometer is used to control the speed on 6 different levels. As such, the range of values satifies the analog input requirement. Finally, as I am using more than 4 LEDs, I have also satisfied the more than 2 LEDs requirement.
SCHEMATIC:
PROBLEMS:
I had a problem with the push button last week because I had not used a resistor with it. After clarifying the issue in class, I managed to set up the circuit.
PROGRAM:
int red = 2; int yellow = 3; int blue = 4; int green = 5; int pushButton = 6; int analog = A0; int value = 0; int sensorValue = 0; int randomNo = 0; int ledState = 0; unsigned long previousMillis = 0; int speedConstant = 100; void setup() { // put your setup code here, to run once: pinMode(red, OUTPUT); pinMode(yellow, OUTPUT); pinMode(blue, OUTPUT); pinMode(green, OUTPUT); pinMode(pushButton, INPUT); Serial.begin(9600); randomNo = random(2, 6); } void loop() { // put your main code here, to run repeatedly: value = digitalRead(pushButton);//reading from digital input Serial.println(value); //if the push button is pressed down, do not move onto the next LED if (value) { } else { //reading values from analog input sensorValue = analogRead(value);//reading from analog input sensorValue = map(sensorValue, 0, 1023, 0, 6); unsigned long currentMillis = millis();//blink without using delay function //LED blinks if (currentMillis - previousMillis >= speedConstant * sensorValue) { // save the last time you blinked the LED previousMillis = currentMillis; if (ledState) { digitalWrite(randomNo, LOW); randomNo = random(2, 6); digitalWrite(randomNo, HIGH); } ledState = !ledState; } } }