S SmartDocs
Serie: C++ c 103 righe · Aggiornato 2026-07-21

lab8.c

C++/Mavis_Homework/Lab8/lab8.c

/*
program: lab8.c
author: firstName lastName
date: July 21 2026
purpose: using for loop
*/

#include <stdio.h>
#include "deviceAlpha.h"

int main(void)
{
	int numberOfReadings = 0;	/* how many readings the user wants   */
	int readingNumber;		/* for loop counter (1, 2, 3, ...)     */
	int inputStatus;		/* return value of scanf               */
	double currentTemperature;	/* one temperature reading             */
	double totalTemperature = 0.0;	/* running sum of all readings         */
	double averageTemperature;	/* totalTemperature / numberOfReadings */

	/* step 1: turn on the BLUE led and ask for the number of readings */
	SetLEDState(BLUE_LED, ON);

	printf("How many temperature readings will be taken? ");
	inputStatus = scanf("%d", &numberOfReadings);

	/* step 2: validate the input - repeat until a positive number is entered */
	while (inputStatus != 1 || numberOfReadings <= 0)
	{
		/* remove the invalid characters left in the input buffer */
		while (getchar() != '\n')
			;

		printf("Not a valid amount!\n");

		/* alarm: BUZZER + RED led on, wait for press AND release of any button */
		SetBuzzerState(BUZZER, ON);
		SetLEDState(RED_LED, ON);

		while (GetPushButtonState(ANY_PUSHBUTTON) == RELEASED)
			;	/* wait until a button is pressed  */
		while (GetPushButtonState(ANY_PUSHBUTTON) == PRESSED)
			;	/* wait until the button is released */

		SetBuzzerState(BUZZER, OFF);
		SetLEDState(RED_LED, OFF);

		/* ask again */
		printf("How many temperature readings will be taken? ");
		inputStatus = scanf("%d", &numberOfReadings);
	}

	/* remove the newline left behind by the valid scanf */
	while (getchar() != '\n')
		;

	/* step 3: BLUE off, HEATER on, GREEN on */
	SetLEDState(BLUE_LED, OFF);
	SetHeaterState(HEATER, ON);
	SetLEDState(GREEN_LED, ON);

	/* step 4: take the readings with a for loop */
	for (readingNumber = 1; readingNumber <= numberOfReadings; readingNumber++)
	{
		/* show the reading number (1, 2, 3, ...) on the 7-Segment display */
		DisplayIntValue(readingNumber);

		/* short pause so the display updates and the sensor stabilizes */
		Wait(100);

		currentTemperature = GetTemperature(TEMPERATURE_SENSOR);
		totalTemperature = totalTemperature + currentTemperature;

		/* 1 second pause BETWEEN readings: GREEN off, YELLOW on */
		if (readingNumber < numberOfReadings)
		{
			SetLEDState(GREEN_LED, OFF);
			SetLEDState(YELLOW_LED, ON);

			Wait(1000);

			SetLEDState(YELLOW_LED, OFF);
			SetLEDState(GREEN_LED, ON);
		}
	}

	/* step 5: heater off and report the average temperature */
	SetHeaterState(HEATER, OFF);
	SetLEDState(GREEN_LED, OFF);

	averageTemperature = totalTemperature / numberOfReadings;

	printf("The average temperature from %d readings is %.2f\n",
	       numberOfReadings, averageTemperature);
	DisplayDoubleValue(averageTemperature, 2);

	/* step 6: wait for the user to press and release ANY button, then end */
	while (GetPushButtonState(ANY_PUSHBUTTON) == RELEASED)
		;	/* wait for the press   */
	while (GetPushButtonState(ANY_PUSHBUTTON) == PRESSED)
		;	/* wait for the release */

	return 0;
}

Articoli correlati