Controlling the lamp brightness with buttons

This experiment is, actually, an extension of the last experiment. With this project you will be able to control the brightness of the lamp but, this time, pressing the buttons. Each of the buttons corresponds to a different intensity.

!Important Information

A big project can always be subdivided into smaller ones and the ability to gather the ideias from two or more projects can mean success in solving a problem. Here we combine the project of reading several buttons with controlling the lamp’s brightness.

!Identifying the Components

Interface Button

!Code Components

All the code components used in this project were already explained in the previous projects.

!Running on Arduino

Run the code below on your Arduino.

// Pin connected to pushbutton
int buttonPin = A0;

// Variable to check the state of the button
int buttonState;

// Mosfet Pin
int lamp = 3;

void setup()
{
  // Defines the button pin as input
  pinMode(buttonPin, INPUT);
  pinMode(lamp, OUTPUT);
}

void loop() {
  // Checks if the button state has changed
  buttonState = analogRead(buttonPin);

  // Cleans noises
  buttonState = 1023;

  // While no button is being pressed, reads again
  while (buttonState >= 897) {
    buttonState = analogRead(buttonPin);
  }

  // If the value is less than 897, this means that some button was pressed
  if (buttonState < 897) {
    // If the RIGHT button is being pressed
    if (buttonState < 69) {
      analogWrite(lamp, 0);
    }
    // If the UP button is being pressed
    if (buttonState >= 69 && buttonState <= 230) {
      analogWrite(lamp, 105);
    }
    // If the DOWN button is being pressed
    if (buttonState >= 231 && buttonState <= 407) {
      analogWrite(lamp, 155);
    }
    // If the LEFT button is being pressed
    if (buttonState >= 408 && buttonState <= 630) {
      analogWrite(lamp, 205);
    }
    // If the SEL button is being pressed
    if (buttonState >= 631 && buttonState <= 896) {
      analogWrite(lamp, 255);
    }
  }
  delay(100);
}

!Exercise

Create a version of the code above using functions!

Answer

int lamp = 3;
int button = A0;
int led1 = A1;

int intensity_button(int btn) {
  if (btn == 0) {
    return 0;
  } else if (btn == 1) {
    return 105;
  } else if (btn == 2) {
    return 155;
  } else if (btn == 3) {
    return 205;
  } else if (btn == 4) {
    return 255;
  }
}

int getButtonState() {
  int value = analogRead(button);
  if (value < 69) {
    return 1;
  } else if (value >= 69 && value <= 230) {
    return 2;
  } else if (value >= 231 && value <= 407) {
    return 3;
  } else if (value >= 408 && value <= 630) {
    return 4;
  } else if (value >= 631 && value <= 896) {
    return 5;
  }
  return 0;
}

int readButton()
{
  int state = getButtonState();

  int count = 1;

  while (count < 5)
  {
    delay(5);
    int newState = getButtonState();
    if (newState == state) {
      count++;
    } else {
      count = 1;
    }
    state = newState;
  }

  return state;
}

void setup()
{
  pinMode(lamp, OUTPUT);
  analogWrite(lamp, 0);
  pinMode(led1, OUTPUT);
}

void loop()
{
  // Lamp on while the button is not pressed
  int btn;
  while ((btn = readButton()) != 5) {
    int light = intensity_button(btn);
    analogWrite(lamp, light);
  }

  // Turns of the light
  analogWrite(lamp, 0);
  digitalWrite(led1, HIGH);

  // Waits for the button to be released
  while (readButton() != 0);

  // Waits for the button to be pressed
  while (readButton() != 5);

  // Waits for the button to be reseased
  while (readButton() != 0);
  digitalWrite(led1, LOW);
}