Devices & Components
Arduino Uno Rev3
Pushbutton switch 12mm
General Purpose Transistor NPN
Breadboard (generic)
Jumper wires (generic)
Resistor 10k ohm
Software & Tools
Arduino IDE
Project description
Code
Flashlight Code
arduino
Enjoy!
1 2int buttonPin = 2; 3int lightPin = 3; 4static int value = 0; //Create a changing number 5void setup() { 6 pinMode(buttonPin,INPUT); 7 pinMode(lightPin,OUTPUT); //Create the pinModes 8} 9void full_light(){ 10 digitalWrite(lightPin,HIGH); 11 delay(50); 12} 13void half_light(){ 14 analogWrite(lightPin,127); //Use PWM for brightness level 15 delay(50); 16} 17void fourth_light(){ 18 analogWrite(lightPin,64); 19 delay(50); 20} 21void strobe(){ 22 digitalWrite(lightPin,HIGH); //Change the delays if you want a faster strobe 23 delay(100); 24 digitalWrite(lightPin,LOW); 25 delay(100); 26} 27void loop() { 28int button_check = digitalRead(buttonPin); 29if (button_check == 1){ //Check if the button was pressed 30 value = value + 1; 31 delay(220); 32 if (value > 4){ 33 value = 0; 34 delay(70); 35 } 36} 37else{ //Keep the LEDs off if the button was not pressed 38 digitalWrite(lightPin,LOW); 39} 40if (value == 1){ //Output the mode according to the button 41 full_light(); 42} 43if (value == 2){ 44 half_light(); 45} 46if (value == 3){ 47 fourth_light(); 48} 49if (value == 4){ 50 strobe(); 51} 52}
Flashlight Code
arduino
Enjoy!
1 2int buttonPin = 2; 3int lightPin = 3; 4static int value = 0; //Create a changing number 5void setup() { 6 pinMode(buttonPin,INPUT); 7 pinMode(lightPin,OUTPUT); //Create the pinModes 8} 9void full_light(){ 10 digitalWrite(lightPin,HIGH); 11 delay(50); 12} 13void half_light(){ 14 analogWrite(lightPin,127); //Use PWM for brightness level 15 delay(50); 16} 17void fourth_light(){ 18 analogWrite(lightPin,64); 19 delay(50); 20} 21void strobe(){ 22 digitalWrite(lightPin,HIGH); //Change the delays if you want a faster strobe 23 delay(100); 24 digitalWrite(lightPin,LOW); 25 delay(100); 26} 27void loop() { 28int button_check = digitalRead(buttonPin); 29if (button_check == 1){ //Check if the button was pressed 30 value = value + 1; 31 delay(220); 32 if (value > 4){ 33 value = 0; 34 delay(70); 35 } 36} 37else{ //Keep the LEDs off if the button was not pressed 38 digitalWrite(lightPin,LOW); 39} 40if (value == 1){ //Output the mode according to the button 41 full_light(); 42} 43if (value == 2){ 44 half_light(); 45} 46if (value == 3){ 47 fourth_light(); 48} 49if (value == 4){ 50 strobe(); 51} 52}
Downloadable files
The Circuit
The single LED represents the the LED head. It will blow if a 220 Ohm resistor is not placed between the LED and ground.
The Circuit

Comments
Only logged in users can leave comments