Smart Table Lamp Challenge

! Challenge

Like in the previous challenge, this is a project that gathers everything you have learned so far!

!Meet the Smart Bedside Lamp

The Smart Bedside Lamp is a lamp that responds dynamically to the environment and to the needs of the one who uses it. This way it can respond automatically to the ambient light or work with a determined configuration.

!Do It Yourself!

Search through the previous projects and think about how a Smart Bedside Lamp would work. Think of how you could do it exploring the resources of your kit and create a program for that!

!Tips for the Project

For this project, you will need:

  • Make the lamp change from a state to the other (Automatic to Manual) – use the Mosfet pins for that
  • You will have to do it in an intuitive manner, so that it can be used by anyone
  • You will need to check that it works nicely
  • You will need a on/off button

Below you can find our code for this project, but we recommend that you do it yourself, because this way you will truly learn! If you need some help, use our code to clarify your doubts or find out how to solve more specific problems.

Our Code

int lamp = 3;
int lamp_2 = 6;
int button = A0;
int light;
int temp, oldTemp;

int intensity(int sensorValue)
{
  float intensityReturn;
  float bright = (sensorValue / 4);
  intensityReturn = 255 - bright;
  return intensityReturn;
}

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);
  Serial.begin(9600);
}

void loop()
{
  // Lamp on while the button is not pressed
  int btn = readButton();

  while (btn != 5) {
    int sensorValue = analogRead(A3);
    light = intensity(sensorValue);
    analogWrite(lamp, light);
    if (btn == 1) {
      analogWrite(lamp_2, 55);
      temp = 55;
    } else if (btn == 2) {
      analogWrite(lamp_2, 105);
      temp = 105;
    } else if (btn == 3) {
      analogWrite(lamp_2, 155);
      temp = 155;
    } else if (btn == 4) {
      analogWrite(lamp_2, 255);
      temp = 255;
    }
    btn = readButton();
  }

  analogWrite(lamp, 0);
  analogWrite(lamp_2, 0);

  // 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 released
  while (readButton() != 0);
  analogWrite(lamp_2, temp);
  oldTemp = temp;
}