I/O device

Class: Technologies
Software used: Arduino IDE, web browser
Device used: Arduino Uno
Components used: Diode, resistor 220om, wires, push-button x 2
 

Task

This time I had a task to create input/output device based on Arduino.
I had no any limits and as the first step in area of digital device prototyping
I chose the project of led-lamp, which brightness can be set with buttons.
Scheme is simple enough, but code is fulfilled with interesting moments,
such as functions and variables.


Code


The sketch for lamp is attached here:
#define PLUS_BUTTON_PIN     2
#define MINUS_BUTTON_PIN    3
#define LED_PIN             9
int brightness = 100; boolean plusUp = true; boolean minusUp = true; 

void setup()
{
 pinMode(LED_PIN, OUTPUT);
 pinMode(PLUS_BUTTON_PIN, INPUT_PULLUP);
 pinMode(MINUS_BUTTON_PIN, INPUT_PULLUP);
}

void loop() {

analogWrite(LED_PIN, brightness);

plusUp = handleClick(PLUS_BUTTON_PIN, plusUp, +35);

minusUp = handleClick(MINUS_BUTTON_PIN, minusUp, -35);

}

boolean handleClick(int buttonPin, boolean wasUp, int delta)

{
 boolean isUp = digitalRead(buttonPin);
 if (wasUp && !isUp) {
delay(10);
isUp = digitalRead(buttonPin);

if (!isUp)
     brightness = constrain(brightness + delta, 0, 255);
}

return isUp;

}

Tags: ,

Trackback from your site.

address: 20 Myasnitskaya ulitsa
(metro stations ‘Lubyanka’ and ‘Kitay-Gorod’)
Moscow 101000 Russia

phone: +7(495)772-95-90 *15026

email: city@hse.ru