sat, 27-oct-2012, 15:17
closeup

After writing my last blog post I decided I really should update the code so I can control the temperature thresholds without having to rebuild and upload the code to the Arduino. That isn’t a big issue when I have easy access to the board and a suitable computer nearby, but doing this over the network is dangerous because I could brick the board and wouldn’t be able to disconnect the power or press the reset button. Also, the default position of the transistor is off, which means that bad code loaded onto the Arduino shuts off the fan.

But I will have a serial port connection through the USB port on the server (which is how I will monitor the status of the setup), and I can send commands at the same time I’m receiving data. Updating the code to handle simple one-character messages is pretty easy.

First, I needed to add a pair of variables to store the thresholds, and initialize them to their defaults. I also replaced the hard-coded values in the temperature difference comparison section of the program with these variables.

int temp_diff_full = 6;
int temp_diff_half = 2;

Then, inside the main loop, I added this:

// Check for inputs
while (Serial.available()) {
    int ser = Serial.read();
    switch (ser) {
        case 70: // F
            temp_diff_full = temp_diff_full + 1;
            Serial.print("full speed = ");
            Serial.println(temp_diff_full);
            break;
        case 102: // f
            temp_diff_full = temp_diff_full - 1;
            Serial.print("full speed = ");
            Serial.println(temp_diff_full);
            break;
        case 72: // H
            temp_diff_half = temp_diff_half + 1;
            Serial.print("half speed = ");
            Serial.println(temp_diff_half);
            break;
        case 104: // h
            temp_diff_half = temp_diff_half - 1;
            Serial.print("half speed = ");
            Serial.println(temp_diff_half);
            break;
        case 100: // d
            temp_diff_full = 6;
            temp_diff_half = 2;
            Serial.print("full speed = ");
            Serial.println(temp_diff_full);
            Serial.print("half speed = ");
            Serial.println(temp_diff_half);
            break;
    }
}

This checks to see if there’s any data in the serial buffer, and if there is, it reads it one character at a time. Raising and lowering the full speed threshold by one degree can be done by sending F to raise and f to lower; the half speed thresholds are changed with H and h, and sending d will return the values back to 6 and 2.

Meta Photolog Archives