Question: This is a C++ language problem that's built upon my previous codes, and I need some guidance on this problem. Material source: https://drive.google.com/file/d/14tXk9-A5htqR-Gj3yMQxnsS3HQtEOkK2/view?usp=sharing The problem:

This is a C++ language problem that's built upon my previous codes, and I need some guidance on this problem.

Material source: https://drive.google.com/file/d/14tXk9-A5htqR-Gj3yMQxnsS3HQtEOkK2/view?usp=sharing

The problem:

part (a): please put together some C++ code to obtain hour, minute and second values using the your previous work that runs every second. You can use the following code as a starting point:

second++; //count one second

if (second==60) { //when you reach 60 seconds

minute++; //count one minute

second = 0; //and reset the seconds

}

Add two more if statements, either nested or consecutively, to allow minutes and hours to roll

over. Make sure to pay attention to what values you want the variables to roll over at, and what

values you want to reset them to when they do.

Part (b): To display the time properly, we will need to have it stored as hour, minute and second variables. Take the code you created for part (a), and paste it inside loop(), in the if(currentMillis>nextMillis) block that runs every second. Make sure to declare these variables as global variables. At the top of the .ino file, in the section where your other global variables are listed, follow the example here to declare them and give them reasonable initial values. Make sure you use the same names you did for your precious work.

int hour = 1;

Part (c): Now all that's left is to add some code right after where you read from the light sensor to modify that value and write it to the backlight pin.

int backlight = map(readings, ???, ???, 50, 255);

backlight = constrain(backlight, 50, 255);

analogWrite(Pin_Backlight, backlight);

The first line maps the value you're reading from the light sensor into an appropriate range for the backlight. Analog writes on the Tiva LaunchPad use an 8-bit unsigned value, giving a range of 0 to 255. It's been cut off at 50 here so the backlight will never turn off completely. Replace the ??? with the lower and upper light levels you found earlier respectively. The second line constrains the backlight value so it won't go out of range even if there's a light sensor reading higher than what you're expecting. The final line writes the analog value to the backlight pin. Make sure you're using the same name for the pin here that you defined earlier. Analog writes on the LaunchPad use pulse width modulation, so the pin is actually being switched between fully off and fully on very fast, so that its average value is the value you write to it.

My Previous Codes:

const int Pin_RedLED = 30; //defined Pin_RedLED as a constant integer-type global variable unsigned long currentMillis; unsigned long nextMillis; //variables to store the current time read from millis() as well as the time of the next tick. //millis() returns a 32-bit unsigned number, so we need to use the unsigned long data type here to properly do arithmetic with it. void setup() { // put your setup code here, to run once: pinMode(Pin_RedLED, OUTPUT); //set the direction of the red LED pin } void loop() { // put your main code here, to run repeatedly: //add some code to actually call the blink function, and then wait a second before repeating. //never use this as part of a more complicated clock program, because delay() pauses the program for the number of milliseconds that drift the clock's time as it never counts code running time //millis() allows time to be counted since the initiation of the code and every time the loop function runs currentMillis=millis(); //every time the loop runs we will need to update the value of currentMillis if (currentMillis >= nextMillis) { nextMillis=millis()+1000; //a line to the setup() function to initialize the nextMillis variable. //We could have done this where the variable was first declared, but putting it here allows us to start the clock when the board is ready to run. blink(Pin_RedLED); //add some code to actually call the blink function, and then wait a second before repeating. delay(1000); //never use this as part of a more complicated clock program, because delay() pauses the program for the number of milliseconds that drift the clock's time as it never counts code running time //check if currentMillis is greater than nextMillis, and blink the LED if so //Add your own code inside the if loop to add 1000 to nextMillis and call the blink() function. Don't forget to comment out the delay() function from part 1. } } void blink(int pin) { int ledState = digitalRead(pin); digitalWrite(pin, !ledState); // a function to actually toggle the LED. This requires us to read the current value of the pin, and then write the opposite. 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Mathematics Questions!