PCB for my final project is built around ATmega328. To do all the programming I use Arduino IDE. More about programming on Week 9. Embedded Programming page.
This sketch is sreated to test how I can scange the mode with one button. By pressing the button I change the variable that changes the mode. Mode 1: only red LED on, mode 2: Only blue LED on, mode 3: RED and Blue LEDs on, deafault: LEDs off.
int inPin = 2; // the number of the input pin int outPin = 13; // the number of the output pin1 int outPin2 = 12; //the number of output pin2 int var = 0; // variable for the switch int state2 = LOW; // the current state of the output pin2 int state = HIGH; // the current state of the output pin1 int reading; // the current reading from the input pin int previous = LOW; // the previous reading from the input pin // the follow variables are long's because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long time = 0; // the last time the output pin was toggled long debounce = 200; // the debounce time, increase if the output flickers void setup() { pinMode(inPin, INPUT); pinMode(outPin, OUTPUT); pinMode(outPin2, OUTPUT); } void loop() { reading = digitalRead(inPin); // if the input just went from LOW and HIGH and we've waited long enough // to ignore any noise on the circuit, toggle the output pin and remember // the time if (reading == HIGH && previous == LOW && millis() - time > debounce) { var = var + 1; if (var >= 4) { var = 0; //zero var } switch (var) { case 1: //do something when var equals 1 state = HIGH; state2 = LOW; break; case 2: //do something when var equals 2 state2 = HIGH; state = LOW; break; case 3: //do something when var equals 2 state2 = HIGH; state = HIGH; break; default: state = LOW; state2 = LOW; // if nothing else matches, do the default // default is optional break; } time = millis(); } digitalWrite(outPin, state); digitalWrite(outPin2, state2); previous = reading; }
Using Arduino Uno I made this test circuit. LEDs are on pins 12, 13, button on pin 2. LEDs are connected to GND via 220Ω resistors. Button is connected to 5V and to GND via 10K resistor. Circuit powered by a small power bank.