Arithmetic Alarm Clock
ContentsObjectiveHere we build an alarm clock. We adopt the joystick to set the clock, use the keys to set the system time and do arithmetic choice. PrincipleDetect the rocker direction of the Joystick: In the Y-axis direction: Short press the upper key to increase the time and long press it to start or close the alarm clock. In the X-axis direction: Shake left to increase the time and shake right to decrease. When the alarm is closed, the key1 can set to increase the hour of the system time, the key2 can set to decrease that time, the key3 can set to increase the minute and the key4 can set to decrease the minute. The time setting for the key1, key2, key3 and key4 can be treated as the four choice questions of A, B, C and D when the alarm is started. Equipment
Hardware Buildup
Code DownloadFile:Music clock-Calculation.zip Code Description
int alarm_hour = 19;
int alarm_minute = 45;
if (alarm)
{
if (alarm_switch == true)
audio_play();
else
audio_pause();
}
if (!down && alarm_switch)
{
num_one = random(0, 100);
num_two = random(0, 100);
data_choose = random(0, 4);
if (data_choose == 0)
{
num_data[0] = num_one + num_two;
num_data[1] = num_one + num_two - 10;
num_data[2] = num_one + num_two + 10;
num_data[3] = num_one + num_two + 1;
}
if (data_choose == 1)
{
num_data[0] = num_one + num_two - 10;
num_data[1] = num_one + num_two;
num_data[2] = num_one + num_two + 10;
num_data[3] = num_one + num_two - 1;
}
if (data_choose == 2)
{
num_data[0] = num_one + num_two + 10;
num_data[1] = num_one + num_two - 10;
num_data[2] = num_one + num_two ;
num_data[3] = num_one + num_two - 1;
}
if (data_choose == 3)
{
num_data[0] = num_one + num_two + 10;
num_data[1] = num_one + num_two - 10;
num_data[2] = num_one + num_two + 1;
num_data[3] = num_one + num_two;
}
down = true;
}
if (key_num > 0)
{
if (key_num == data_choose + 1 )
{
onec_change -= 1;
down = false;
}
else
onec_change += 1;
} ResultYou can set the time through the Crash sensor and set the alarm clock through the joystick. Besides, you can build beautiful shell for this project with LEGOs since mCookie modules can be easily stacked with LEGO boards. Video |