Categories
Arduino control Uncategorized

Arduino Programming: About time

Time is probably the most commonly controlled process variable. Timers are all over the place in industrial control. Odds are, if you need some type of timer, no matter how strange, you can find it off the shelf.

Now, many of these timers used a chip usually referred to as the “555.” The LM555 originally made by (I think; someone will correct me) National Semiconductor was a very versatile device, but it was at the heart of many time-delay relays, short timing circuits, etc.

So once upon a time, if you wanted to build a basic timer, odds are you would wire up a 555 into a circuit. To build a handful, or just one, you’d use a perf board,

perfboardmaybe you might use wire wrap or even dead-bug construction (my favorite!)

deadbug

It would be time consuming,but maybe you had no choice because the timer had some weird requirement that no off the shelf timer had, or needed to fit into an oddly shaped space.

What does this have to do with Arduinos? Well, you can program any timing sequence into an Arduino. Say you want the heater on a commercial ironing board to come on for five seconds when the operator lowers it, a 555 does it easily. If you want the heater to come on for five seconds and when the board is raised again, a fan to blow for 10 seconds to cool the clothing, the 555 can still be used. Maybe you need two of them. But now, the Arduino becomes an easier solution. Whether you need one time sequence, or dozens, a single Arduino can be programmed to do it. When you factor in the labor of wiring a circuit board with the 555, the low off the shelf price of the Arduino makes it even more attractive.

This is the wonder of the time we live in: an off the shelf microprocessor board is now inexpensive enough to be used for logic replacement.

Amazing




If you'd like to subscribe to this blog, please click here.

Categories
Arduino

Arduino Programming: Turn water on with Arduino and solenoid valve

Arduinos are popular small microcontroller boards that have many applications. However, they’re not designed to switch loads above a few milliamps: say a couple LEDs or so. While power-driver shields do provide this capability, they also can consume more resources than you may be able to give up.

We developed a high current driver to make it easy to control a solenoid valves with Arduino. It will also control pumps and motors. With an adapter cable, it can easily connect to your Arduino, BeagleBone, Raspberry Pi or other digital controller without soldering or crimping any connections. Doesn’t get any easier than that.

PwrDrvr1

The power driver board was born out of a need for controlling a 1 amp solenoid valve using an Arduino.  The solenoid valve was being used to control the water flow to fill a tank automatically. Now there’s a simple way to use your Arduino or compatible to switch up to 3A at 24VDC. Two output connections (the white wires shown above) connect directly the load (your solenoid, relay, motor, etc) and the power (red, black) go to the power supply (5 -24 volts). The orange lead is used to switch on and off. This is a low-voltage (5V) control that can connect directly to a microcontroller, or development board. An onboard LED indicates when the load is switched on.

Here’s some sample code that implements a timer with an Arduino. When the pushbutton is pressed, it turns on water flow for 3 seconds

// This sketch demonstrates a simple timer
// A load (motor, solenoid, relay, solenoid valve is on Pin 1
// A pushbutton to trigger the timer start is on pin 2
//
// When the pushbutton is held down for more than 0.1 second 
// then released, the timer starts
// and times out after 3 seconds
//
// Timer is retriggerable: if pushbutton pressed 
// during the timeout period, timer restarts
//
// Constant definitions
#define LOOP_INTERVAL 10
#define TIMEOUT 300 * LOOP_INTERVAL
#define TRIGGER_INTERVALS 10
#define TIMER_INACTIVE -1
#define TRIGGER_PIN 0
#define OUTPUT_PIN 1

void setup()
{
  pinMode(OUTPUT_PIN, OUTPUT);
  pinMode(TRIGGER_PIN, INPUT_PULLUP);
}

void loop()
{
  static int count = 0;
  static int timer = TIMER_INACTIVE;
  // Process loop periodically
  delay(LOOP_INTERVAL);
  
  // Check trigger input
  if (digitalRead(TRIGGER_PIN) == LOW)
  {
    // Must hold down pushbutton for the entire interval and 
    // then release to trigger
    count++;
  }
  else
  {
    // push button released. Check if we should start timing
    if (count >= TRIGGER_INTERVALS)
    {
      // Turn output ON (timeout is retriggerable)
      digitalWrite(OUTPUT_PIN, HIGH);
      timer = TIMEOUT;
    }
    count = 0;
  }
  
  // If timer active, count down
  if (timer != TIMER_INACTIVE)
  {
    timer -= LOOP_INTERVAL;
    if (timer == 0)
    {
      // Turn output OFF
      digitalWrite(OUTPUT_PIN, LOW);
      timer = TIMER_INACTIVE;
    }
  }
}      

Let’s find out what new applications you can come up with.




If you'd like to subscribe to this blog, please click here.

Categories
Arduino

Arduino Programming: hydraulic shock control from pushbutton

This was a response to an online request for help with code. A single  pushbutton cycles through three hydraulic damper valve settings. The code was too long to post on the site, so I’m showing it here.

As of today, I don’t know if it will work, but it explains the concept. I will get it working as time permits

// Digital output definitions
#define RED_OUTPUT 0
#define BLUE_OUTPUT 1
#define GREEN_OUTPUT 2

// Digital input definitions
#define SWITCH_INPUT 3

// Define states
#define RED 800
#define BLUE 801
#define GREEN 802

// Define message types
#define NONE 901
#define CLICK 902
#define PRESS 903
   
// Global state
int _state = GREEN;

// One-time call to initialize system
void setup()
{
   pinMode(SWITCH_INPUT, INPUT_PULLUP);
   pinMode(RED_OUTPUT, OUTPUT);
   pinMode(BLUE_OUTPUT, OUTPUT);
   pinMode(GREEN_OUTPUT, OUTPUT);
   // Enable GREEN state
   digitalWrite(GREEN_OUTPUT, HIGH);
}

// Continuously called by Arduino runtime
void loop()
{
   // Read pushbutton
   int message = readSwitch();
   // Update LEDs and hydraulic valve if state changed
   if (processState(message))
   {
      activateOutput(_state);
   }
}

// Read pushbutton and return the result
int readSwitch()
{
   const int CLICK_UPPER_LIMIT = 200;
   const int CLICK_LOWER_LIMIT = 25;
   const int PRESS_LOWER_LIMIT = 1000;
   const int PRESS_UPPER_LIMIT = 2000;

   int switchMessage = NONE;
   unsigned long start = micros();
   while (digitalRead(SWITCH_INPUT) == LOW);
   unsigned long duration = micros() - start;
   if (duration < CLICK_UPPER_LIMIT && duration > CLICK_LOWER_LIMIT)
   {
      switchMessage = CLICK;
   }
   else if (duration > PRESS_LOWER_LIMIT && duration < PRESS_UPPER_LIMIT)
   {
      switchMessage = PRESS;
   }
   return switchMessage;
}

bool processState(int message)
{
   static int lastState = GREEN;
   int retVal = 0;

   // PRESS overrides CLICK in all states
   if (message == PRESS)
   {
      _state = GREEN;
   }
   else
   {
      // Process states
      switch (_state)
      {
         case RED:
            if (message == CLICK)
            {
               _state = BLUE;
            }
         break;
            
         case BLUE:
            if (message == CLICK)
            {
               _state = RED;
            }
         break;
            
         case GREEN:
            if (message == CLICK)
            {
               _state = RED;
            }
         break;
            
         default:
         break;
      }
   }
   // Did state change?
   retVal = (lastState == _state);
   // Remember this state
   lastState = _state;
   // Return state changed information
   return retVal;
}

void activateOutput(int thisState)
{
   // Since this is only called on state change, OK to turn everything off first
   digitalWrite(RED_OUTPUT, LOW);
   digitalWrite(GREEN_OUTPUT, LOW);
   digitalWrite(BLUE_OUTPUT, LOW);
   // Turn on only the output corresponding to the state
   switch (thisState)
   {
       case RED:
         digitalWrite(RED_OUTPUT, HIGH);
         break;         
       case BLUE:
         digitalWrite(BLUE_OUTPUT, HIGH);
         break;      
       case GREEN:
         digitalWrite(GREEN_OUTPUT, HIGH);
         break;
      default:
         break;
   }   
}



If you'd like to subscribe to this blog, please click here.