Best board for multiple serial devices?

Thanks for your help everyone - I've learned a lot.

Will crank up the hardware shortly and see how I go; spent the day yesterday building an Arduino electric blanket controller so that I can have 16 levels of control instead of the 3 that are all too hot.

If anyone is interested, after a 16 minute "on" interval it just modulates a solid state relay for "X" seconds on a 16 second cycle - with an "up" and a "down" switch (with auto repeat) - code below.

int const downSwitch    = 2;                                  // Define Pins
int const upSwitch      = 3;
int const heaterControl = 4;

int const autoRepeat    = 200;                                // Auto-Repeat delay in mS

int Power               = 30;                                 // Define heater power variable and set to 30 (18.75% duty cycle)
int loopCount           = 160;                                // 16 heat levels (plus off) x 1/10th sec per iteration

int onTime              = 30;                                 // Initial on time (in 1/10ths of a second)
int offTime             = 130;                                // Initial off time (in 1/10ths of a second)

void setup() 
{
  pinMode(downSwitch, INPUT);                                 // Set down switch pin as input
  pinMode(upSwitch, INPUT);                                   // Set up switch pin as input
  pinMode(heaterControl, OUTPUT);                             // Set heater relay control pin as output

  for (uint16_t modeTimer = 9600; modeTimer > 0; modeTimer--) // Count down 960 seconds
  {
    digitalWrite(heaterControl, LOW);                         // - turn heater on

    if (digitalRead(upSwitch) == 1)                           // - check for up switch press
    {
      modeTimer = 1;                                          // - exit auto mode if pressed (1 not 0 because it still gets decremented at the end of the loop)
    }

    if (digitalRead(downSwitch) == 1)                         // - check for down switch press
    {
      modeTimer = 1;                                          // - exit auto mode if pressed (1 not 0 because it still gets decremented at the end of the loop)
    }

    delay (100);                                              // - wait 100 milliseconds
              
  }                                                           // And go around the loop until timer hits zero or a key is pressed
}

void loop()                                                   // Main code starts here (runs repeatedly)
{                                                                       
  onTime = Power;                                             // Set on_time & off_time counters (in 1/10ths of a second)
  offTime = 160 - Power;                                      //

  for (loopCount = 160; loopCount > 0; loopCount--)           // Process 10x 100mS states (ie "1 second") 16 times (one for each of the possible heat levels)
  {
    if (digitalRead(upSwitch) == 1)                           // Check for "up switch"
    {
      if (Power < 160)                                        // Only do if not already at max power level
      {
        Power += 10;                                          // Increase heater power if up switch pressed
        loopCount = 1;                                        // Reset the timing loop
        delay(autoRepeat);                                    // Slow it down for auto repeat             
      }
    }

    if (digitalRead(downSwitch) == 1)                         // Check for "down switch"
    {
      if (Power > 0)                                          // Only do if not already at min power level
      {
        Power -= 10;                                          // Decrease heater power if down switch pressed
        loopCount = 1;                                        // Reset the timing loop
        delay(autoRepeat);                                    // Slow it down for auto repeat
      }
    }

    if (onTime > 0)                                           // If time on required then ...
    {                                                         //
      digitalWrite(heaterControl, LOW);                       // ... turn on the relay
      onTime--;                                               // ... and decrement the counter by 1/10th of a second
    }                                                         //

    else                                                      // If time off required then ...
    {                                                         //
      if (offTime > 0)                                        // for as long at time off hasn't timed out ...
      {                                                       //
        digitalWrite(heaterControl, HIGH);                    // ... turn off the relay
        offTime--;                                            // ... and decrement the counter by 1/10th of a second 
      }                                                       //
    }                                                         //   

    delay(100);                                               // Wait 1/10th of a second - this sets the overall speed of the timing loop in normal operation

  }                                                           // End of heat cycle loop

  loopCount = 160;                                            // 160 iterations done ... so reset the counter and get back into the loop
            
}                                                             // End of Main Code Loop

Hi, I tried your sketch in Wokwi simulation, but the heater seems to be on all the time. The start button is in the upper-middle of the screen.

A new notes:

  • The order "const int" is more common, instead of "int const".
  • digitalWrite() [EDIT] digitalRead() does not return a '1', it returns HIGH or LOW. In the if-statement, you could compare it with HIGH.
  • There is no output to a display or serial monitor to tell what is going on.

Did you mean digitalRead() ?

Oops, yes, I will fix it. Thanks.

Thanks for that.

  1. I'd never really thought about it - but now that I have, I agree. Thanks for that.

  2. I could do, but in binary terms I suspect that the compiler treats "1" and "HIGH" as being synonymous in that context. Probably "no biggie" either way.

  3. I copy/pasted most of the code from a project I developed to control the grip heaters on my motorbike which uses a 16x2 LCD display, but I stripped out that code for this project. As this is the final code I don't need the output sent to anything; it's a simple system -- if I'm too cold then I can just tap the up button a few times - or the down botton if I'm too hot. Holding either button for a few seconds will either turn it fully on or off. There are lights on the solid state relay board but they're more of a nuisance than anything because they're too bright at night; will probably remove them once the case arrives for it. I'll probably rewrite it once my RTC module arrives to turn my blanket on at a certain time.

One would think that setting the control pin output high would turn on the solid state relay, but I discovered in testing that it works the other way - so writing a LOW turns it on and HIGH turns it off. I tried substituting LOW and HIGH for declarations that made the code more readable/logical, but the compiler wouldn't accept it -- so I just stuck with doing it manually (it's only switched in 3 places in a short bit of code so probably not a big deal; "know when to hold'em, know when to fold'em, know when to walk away, and know when to run" comes to mind - I ran with the 3rd option there :wink:

Cheers,

Colin

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.