Im currently making a “cyclone” sort of game with my new arduino.
Here is the code:
int buttonPin = 2;
int period = 1000;
unsigned long time_now = 0;
void setup() {
for (int Pin = 4; Pin < 8; Pin++) {
pinMode(Pin, OUTPUT);
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
}
void loop() {
for (int Pin = 4; Pin < 8; Pin++) {
digitalWrite(Pin, HIGH);
delay(150);
digitalWrite(Pin, LOW);
}
for (int Pin = 7; Pin >= 4; Pin--) {
digitalWrite(Pin, HIGH);
digitalWrite(Pin, LOW);
}
// leds();
int buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
delay(1);
}
I have a button set up to print “1” (in the serial monitor) whenever its clicked. But print “0” when not clicked. But in the meantime my LEDs are also making its task. So, when I click the button whenever the LED goes on or off, it doesn’t print 1, since its like if delay() is ignoring anything else apart from the LEDs when being switched. Now when I click the button in the right time, it does print. Is there a more efficient way of making this?
Not sure what you mean by “affect”… delay is blocking your code where it is for an amount of time. That’s why it’s called delay(). (Could also have been called pleaseWaitHereWithoutDoingAnything() But that was too long to type )
This code will basically lockup the processor for 600ms
The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.