Question

I've been fiddling with this code all day but I'm stuck yet again...thanks in advance

Basically this program is giving me issues and below are the snippets of code that I believe are causing the problem. Basically the user can change their "current temperature" and if it exceeds the threshold of high or low limit a "compareLimit" function is called...this function then calls various other functions (such as beeping, and allowing for one line sensor records to be printed out on screen). However the following are my problems (please keep in mind I have to use beeping, although if you have a better method please advise):

  • beeping is a pain in the ass, and my entire "alarm" mode revolves around the duration of the beep. This means that everytime i want to press the letter "O" to print out a report, it prints on the whim of it coinciding with the end of a beep trigger. How do i eliminate this?

  • I want to allow the user to still be able to access the last block of code (the case statements) even while being in the "while loop" of alarm mode, so that they can actually exit alarm mode by pressing r or f to change the temperature, and return the system to normal.

    void updateDisplay()
    {
    clrscr();
        HANDLE hConsole; //standard c library call 
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //used for output screen buffer to allow for coloured text
    
    SetConsoleTextAttribute(hConsole, 2); //sets output screen colour for following text
    printf("\nCurrent Temperature for channel 1 is %d\n\n", temperatureSensor1Reading);
    printf("Upper Limit for channel 1 is  %d\n\n", getHighLimit(CH1));
    printf("Lower Limit for channel 1 is  %d\n\n", getLowLimit(CH1));
    setCurrentTemperature(CH1,temperatureSensor1Reading);
    

This block of code is where I call my functions from my other cpp source file in the same project to begin compare the current temperature with the set limits.

comparedLimits1 = compareLimit(CH1);
SetConsoleTextAttribute(hConsole, 14);

if (comparedLimits1 == TRUE) {
    printf("please press O to print out a temperature report for channel %i \n \n", selectChannel + 1);
    printf("please press P to silence the alarm for channel %i \n \n", selectChannel + 1);
}

while(comparedLimits1 == TRUE) {
    activateAlarm(CH1,temperatureSensor1Reading);
    comparedLogs1 = sensorLog();
    if (comparedLogs1 == TRUE) {
        printf("\n Channel %i has registered a temperature of %i \n \n ", selectChannel+1, temperatureSensor1Reading);
    }
}

this is my function that compares the current temperature to the set temperature limits, and calls other functions to "beep" or allow the user to print out a one line "report".

temperature_t compareLimit (int channelID)
{
    temperature_t limitIsExceeded = FALSE;
    if ((temperatureChannel[channelID].currentTemperature > temperatureChannel[channelID].highLimit) | (temperatureChannel[channelID].currentTemperature < temperatureChannel[channelID].lowLimit))
    limitIsExceeded = TRUE;

    return limitIsExceeded;
}

void activateAlarm(int channelID, temperature_t temperature)
{   
    int key = 0;

    if ((temperatureChannel[channelID].currentTemperature > temperatureChannel[channelID].highLimit) | (temperatureChannel[channelID].currentTemperature < temperatureChannel[channelID].lowLimit))
        callBeep();

    sensorLog();
if (_kbhit())
        key = _getch();

    if ((key == 'P') | (key == 'p')) {
        silenceBeep();
    }       
}

void callBeep()
{
    Beep(250,2000);
}

void silenceBeep()
{
    Beep(0,2000);
}

temperature_t sensorLog()
{   
    int key = 0;
    temperature_t notedLog = FALSE;
    if (_kbhit())
        key = _getch();

    if ((key == 'O') | (key == 'o')) {
        notedLog = TRUE;
        return notedLog;
    }
}

this is the piece of code I still want to be able to manipulate while the user is in "alarm" mode. for the user to get out of "alarm" mode they have to be able to reduce the current temperature which can be done from the below case statements

if( _kbhit()  ) {
    selectedCommand = _getch();

    switch(selectedCommand) {
        case 'R': //if user input is R
        case 'r'://if user input is r
            (*temperatureSensorReadings[selectChannel])++;
            break; //exits loop

        case 'F': //if user input is 'F'
        case 'f': //if user input is 'f'            
            (*temperatureSensorReadings[selectChannel])--;          
            break; //exits loop
Was it helpful?

Solution

Instead of Beep use PlaySound to play a .wav file instead using the SND_ASYNC flag to return immediately and play the sound asynchronously. You can even use one of the system sounds with one of the predefined values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top