I started out with the "Hello World" of the Arduino. Blinking the LED on the 13 Pin. My code here is mostly based off of that as well as some information on using a button. Revisions of code will be updated and the old stuff will never be shown.
As it stands now, the code for the button isn't very efficent. If the lights are blinking at 1 second intervals, its hard to catch it at the right time to trigger the button. Running at 50ms, its eaiser as you can press it a few times and you're bound to get it. If anyone has a better way of doing the button, I'd love to hear it
Learned about using the attachInterrupt function and was able to make this work out better than needing to catch the button at the right time. No matter where its at in the voild loop, once that button is pressed it will interupt the procedure and go to the pressedbtn() function. I call void() in that function because I want it to pick up where it last left off.
Here's my code:
/* The code here started out from the blink example that is included with the Arduino Application. I just started modifying it for my purposes. This project will include 5 LED's, 2 Potentiometer's, and a button. */ int x = 0; int i = 0; int lightLED = 9; int goback = 0; volatile int btnPress = 0; //0=back and forth blinking (knight rider), 1=9,10,11,12,13,repeat. void setup() { attachInterrupt(0,pressedbtn,FALLING); // // initialize the digital pins: pinMode(9, OUTPUT); pinMode(10, OUTPUT); pinMode(11, OUTPUT); pinMode(12, OUTPUT); pinMode(13, OUTPUT); } void loop() { if (btnPress == 0) { //if the button pressed is set to 0 blink knight rider style blinkLED(); if (goback == 0) { lightLED ; if (lightLED == 14) {goback = 1; lightLED = 12;} } else { //otherwise the button pressed is set to 1 which is uni-directional lights blinking lightLED--; if (lightLED == 8) {goback = 0; lightLED = 10;} } }else if (btnPress == 1) { if (lightLED == 14) {lightLED = 9;} else {lightLED ;} blinkLED(); } } void blinkLED() { checkSpeed(); digitalWrite(lightLED, HIGH); // set the LED on checkSpeed(); digitalWrite(lightLED, LOW); // set the LED off } void checkSpeed() { x = analogRead(0); //read the value from the potentiometer x = map(x, 0, 1023, 50, 1000); //map the value from the potentiometer to 50-1000ms delay(x);//delay for x amount of miliseconds determined by the potentiometer } void pressedbtn() { if (btnPress == 1) {btnPress = 0;} else if(btnPress == 0) {btnPress = 1;} void(); }