Were to start with a Arduino timer

I want to build a timer that with arduino.

Basically I want it to run for 5 or more minutes and then switch off a relay.

I want to use a hall effects switch to increase the time increments by 5 minutes at a time and have a led for every 5 minutes (4 leds for 20 minutes etc). Then when the time is set turn on a latching hall effects switch to power a relay to run a motor and start the countdown. When the time reaches zero open the circuit and stop the motor.

Can anyone help with ideas or sites to start this project,

Sounds like someone is working on a fishing kontiki and want’s to program it’s motor run time on w/a magnet so as to prevent seawater intrusion into the hull.

If that’s true you want to put this portion of code in the setup() function and it want’s to have a default time if there’s no input at the proper time. And that means there’s got to be a way of telling the user “Hey, it’s time to program the motor run time … Now”, perhaps via an LED or ???

You might consider using a reed switch, which closes in the presence of a magnetic field and is open other-wise. It’s simpler than a Hall device. Then look at this tutorial as to how to read that switch into determining if a magnet is present or not present.

http://arduino.cc/en/Tutorial/InputPullupSerial

https://www.sparkfun.com/products/8642

Now you have to decide how you want to implement the “setting”. You can hold the magnet in place and wait while the software increments the motor run time every second or so, taking the magnet away when the time has finally got to the desired value. Or you can count the number of swipes of the magnet and use each swipe to increment (?decrement?) the run time. Your choice. In either case you need some way of indicating to the user just what run time is presently being set so the use knows when to stop swiping or to take the magnet away.

Yep that’s exactly what im up to. I want to use a magnet to set it and leds to show time increments.

What timer library would be best to use.

beretta:
What timer library would be best to use.

I don't know if there is one for this purpose. I would use the hold method vs the swipe method as it's easier to code. Then I'd write code that does what the following "pseudo-code" says to do.
setup(){
  //other code goes here
  blah blahh blah;
  //now set the motor run time
  //turn on LEDs that indicate run time
  digitalWrtite(.....;
  //delay some time for user to get magnet into place
  delay(X secs);
  //now slowly increment run time and update LEDs while magnet is in place
  while(magnetPin == LOW){
    //use a for loop to time up to N run time incrments
    for(x = 1; x <= N ; x++){
      runTime++;
      runtime = min(runTime, maxTime);
      //update LEDs
      digitalWrite(..... ;
      //delay to slow down incrmenting
      delay(1 sec);
    }
  }
  //remainder of setup code goes here
  more blah blah blah;
}

From the PM it sounds like you’re getting back to this. Is it the same concept as originally posted, that is a simple unguided (no compass, no GPS) 'tiki ? All that’s needed is some power on switch (presumably inside the 'tiki) then some way to set the motor run time, then start the motor and then stop the motor when the timer is done. They’ll be some user interface; to allow the user to set the time and start the timer & motor (via magnetic “wand”) and some way to show the time setting and perhaps other “stuff” ? And the plan was to have some type of Arduino to be the controller/timer ?

What parts do you have presently ? If you have a motor, how many amps does it draw ? A battery ? If so what voltage ? Some power relay to switch the motor on/off ? If so, what is it (part #, link if you can). An Arduino, and if so what type ?

Thanks for your reply.

I am using Arduino Uno. I have a 12volt motor drawing 40 amps. I will use a relay like this.

http://www.kiatronics.com/kontiki-parts … 70405.html

I have a compass guidance system which is working ok. I want to set the time using a magnet utilizing hall effect or reed switches. The timer will need to be set in 5 minute increments up to 30 minutes and have a led light up for each increment then a separate switch to start the motor. Here is video of on working

https://www.youtube.com/watch?v=VMyf8yzgdHU.

I have some hall effect switches OH1881 which should work for the on off

Thanks again

I’ve watched the video and I like the way they implemented the timer, time reset, etc functions. Is it your intention to mimic how they did those functions ? Do you want to do the voltage measuring function ? This is all pretty simple stuff.

There’s one gotcha in using the Hall sensor you have. It’s a latching type, as described here …

http://bildr.org/2011/04/various-hall-effect-sensors/

The Melexis US1881 (available from sparkfun at $0.95) is a Latching Hall Effect Sensor meaning that once it is triggered it latches and will not unlatch until a magnetic force of reverse polarity and strength is sensed. So If the north pole of a magnet turned it on, the south pole of a magnet is then needed to turn it off. When the US1881 is triggered the output will be equal to the source voltage (3.5 to 24V) and unlatched will output 0v/ground.

That means you’ll need to flip the magnet so as to present it’s other pole to the sensor instead of just removing the sensor. Is that possible w/the magnet you intend to use ? It will probably mean a slower time in between the actions, in order to giver time to reverse the magnet and swipe it to turn off the sensor.

Do you have code to do the compass based navigation ? If so do you want to post it so the timer stuff can be added to it ?

#include <Wire.h>
#include <Serial.h>
#include <Servo.h>
#include <hmc6352.h>
#define compassAddress 0x41 >> 1

Servo rudder;                     //Rudder servo object

const float minRudder = -45.0;
const float maxRudder = 45.0;
const float centerRudder = 90.0;
const float offsetRudder = 0.0;      //Deg to correct prop torque
const unsigned int smallWiggle = 10;
const unsigned long testDelay = 1000;
const unsigned long loopDelay = 50;

byte compassReadingBytes[2];      //Heading read bytes from compass
unsigned int rudderCommand = 90;  //Command for rudder servo
float compassReading = 0.0;       //Reading from compass
float desiredHeading = 0.0;       //Heading to be followed
float errorDeg = 0.0;             //Error btw course and desired course
float Gain =1.0;                //Gain for proportional loop
float rudderPosition = 0.0;       //Desired position of rudder
unsigned int loopCount = 0;       //Running loop count
unsigned long initTime = 0;       //Time when compass was initialized

void setup() {
  Serial.begin(38400);            //Start the serial port for testing
  Serial.println("Initializing the system");
  Wire.begin();                   //Start i2c
  // put any needed compass init code below this line
  
  // Wait for the compass to initialise
  initTime = millis();
  rudder.attach(9);               //Rudder servo on pin 9
  Serial.println("Doing a rudder servo test");
  //Command hard over
  rudderCommand = word(minRudder + 90);
  rudder.write(rudderCommand);
  delay(testDelay);
  //Command hard over the other way
  rudderCommand = word(maxRudder + 90);
  rudder.write(rudderCommand);
  delay(testDelay);
  //Command back to center
  rudderCommand = 90;
  rudder.write(rudderCommand);
  delay(testDelay);
  //check that enough time has passed for compass to be ready
  while(millis() - initTime < 500 ){
    delay(10);
  }
  //get an average of 10 readings to set desired heading
  for(int i = 0; i < 10; i++){
    readCompass();                 //Get heading from compass
    desiredHeading += compassReading/10.0;
    delay(210);
  }
  Serial.print("Desired heading is ");
  Serial.println(desiredHeading);
  Serial.println(" ");
  //Now do a quick rudder wiggle to let the operator
  //know that the tiki is ready for launch
  //Command -10 deg
  rudderCommand = 90 - smallWiggle;
  rudder.write(rudderCommand);
  delay(testDelay/4);
  //Command +10 deg
  rudderCommand = 90 + smallWiggle;
  rudder.write(rudderCommand);
  delay(testDelay/2);
  //Command back to center
  rudderCommand = 90;
  rudder.write(rudderCommand);
  delay(testDelay/4);
}

void loop(){
  loopCount++;
  readCompass();
   // compute how many deg offcourse tiki is
  errorDeg = compassReading - desiredHeading;
  if(errorDeg >= 180.0){
    errorDeg = errorDeg - 360.0;
  }
  if(errorDeg <= -180.0){
    errorDeg = errorDeg + 360.0;
  }

  // compute rudder position needed to correct error
    rudderPosition = constrain((Gain * errorDeg), minRudder, maxRudder);
  // translate that position into a servo command
  //rudderCommand = 90 - word(rudderPosition);
  rudderCommand = word(rudderPosition + centerRudder + offsetRudder);
  rudder.write(rudderCommand);
  Serial.print("Loop count is ");
  Serial.println(loopCount);
  Serial.print("Measured heading is ");
  Serial.println(compassReading);
  Serial.print("Rudder position is ");
  Serial.println(rudderPosition);
  Serial.print("Servo command is ");
  Serial.println(rudderCommand);
  Serial.println(" ");
  delay(loopDelay);
}

void readCompass(){
  //Instruct compass to read echoes
  Wire.beginTransmission(0x21);  // transmit to device
  // the address specified in the datasheet is 66 (0x42)
  // but i2c adressing uses the high 7 bits so it's 33
  Wire.write(0x41);                       // Send a "Post Heading Data" (0x50) command to the HMC6343 
  Wire.endTransmission();                 // stop transmitting

  //Wait for readings
  delay(2);                               // datasheet suggests at least 1 ms

  //Request heading reading from compass
  Wire.requestFrom(0x21, 2);    // request 2 bytes from slave device #33

  //Receive heading reading from compass
  if(2 <= Wire.available())               // if 2 bytes were received
  {
    for(int i = 0; i<2; i++) {
      compassReadingBytes[i] = Wire.read();
    }
  }
  compassReading = ((int)compassReadingBytes[0]<<8) | ((int)compassReadingBytes[1]);  // heading MSB and LSB
  compassReading = compassReading * 0.1;    //Translate heading to degress
  if(compassReading == 360.0){
    compassReading = 0.0;
  }
}

This is the code I have that works fine. I think you had a hand in this a few years back.

I am not worried really about the battery voltage check. The latching hall effect sensor would be fine for turning on and off but would need something other for setting the time increments.

At the moment I’ve written code for the timer function but not melded it into the code above. I’ve got some questions as to how you’d prefer the LEDs to light up and some other stuff.

  1. In the video one LED illuminates at a time to indicate the motor run time setting. Would you like that or a bargraph type display, one where all the LEDs at and below the time setting are illuminated ? FWIW I though it might be nice to also have the LED display also show the run time remaining, as the timer counts down, via the LEDs. No that anyone is likely to see it when the tiki is in use.

  2. I thought it would be nice to have the most commonly used time preset. That way when the tiki is powered up, you may not have to go through the process of changing the time. I note that the timer in the vid starts at zero time (who’d ever want zero time ?) and once the motor is turned on and then back to off, the timer setting is lost. That seems to be a PITA if you want to test the motor prior to launch. Instead I propose to simply reset the timer setting if/when the motor is turned off after being turned on.

  3. I note that the LED indicating the timer setting is constantly on when being set. When the motor is turned on, the LED blinks. Is this OK ? Would you like a separate motor on/off LED ? See the below section.

  4. The code above did a rudder movement test as part of it’s pre-launch check out process. It then waited a short bit and then assumed you had pointed the tiki in the proper direction and so memorized the compass heading to hold. Then it did a little rudder wiggle to let the user know the motor was about to come on. Of course that latter part was because there was no user interface to do anything. Now you’ll have one so I propose a slightly different pre-launch process.

As before it’ll do the large rudder test after power up but then enter an idle mode, with some timer preset and no compass heading setting. A wave of the magnet over Hall sensor 1 will move the tiki from idle to timer setting mode, allowing you to hold the magnet over (the other) Hall sensor #2 to increment/change the setting (if desired). Another wave (over #1) will push the tiki into compass setting mode. Holding the magnet over Hall sensor #2 will trigger the compass setting procedure. A final wave (over #1) starts the motor and timer. Another wave (over #1) would send the system back to idle and shut off the motor. Unless you want to change either the timer setting or the compass setting to hold, you can just go through their states (via waves over #1) to get to the motor on state (keeping the prior settings).

Now all off the above cries for some LEDs to indicate what state the tiki timer is in (Idle, timer setting, compass setting, motor on) but perhaps there’s some combo using the timer setting LEDs that will work ?

Your thoughts ?

FWIW I would need non-latching Hall sensors for the above (as was used in the vid).

Wow mate you are on to it.

  1. Sound perfect

2)The timer can start at 5 minutes as there really isn’t any “most common time used”. It depends where fishing and time of year as to time required.

  1. blinking is ok but really only to show the timer functioning I suppose. No separate motor on led is necessary

4)Sound good but perhaps something like

Wave magnet over sensor #1 to start timer setting mode wave magnet over sensor #2 to set timer. Wave magnet over sensor #3 to set compass heading and a let lights up to show heading set. Wave magnet over sensor #1 to start motor and timer. Wave magnet over sensor #1 again to stop motor and rest all settings to default and start again.

Or as you suggested with a different color led to show timer mode and compass mode. I would like a led to show compass head obtained and ready to go. No motor on led needed.

Many thanks for your help awesome.

OK, I used to be quite the surf fisherman before I moved to the Midwest. But I never heard of a kontiki before (besides the raft :slight_smile: ) How did you figure out that was what he was building from his description?

lyndon:
How did you figure out that was what he was building from his description?

There was another long thread before this one, that discussed the whole kontiki concept. It was new to me then. Apparently it's all the rage down under but I've never seen one here in the States. Given the vendors there are selling them for 4k$-5k$ a pop, I wonder if there's not a market here ? If not for ones that run out 1km+, then for shorter distances. Then again we probably have some laws against that type of fishing.

beretta:
4)Sound good but perhaps something like

Wave magnet over sensor #1 to start timer setting mode wave magnet over sensor #2 to set timer. Wave magnet over sensor #3 to set compass heading and a let lights up to show heading set. Wave magnet over sensor #1 to start motor and timer. Wave magnet over sensor #1 again to stop motor and rest all settings to default and start again.

Or as you suggested with a different color led to show timer mode and compass mode. I would like a led to show compass head obtained and ready to go. No motor on led needed.

You'd be OK w/3 Hall sensors ?

I note the timer in the vid used one for motor on/off and the other for mode/state change and setting the time. I could keep that philosophy as well. It really all comes down to how easy and intuitive whichever makes the user interface.

Perhaps, given your 'tiki memorizes a heading, you need a “better” display than an line of LEDs ? Perhaps some alpha-numeric LED (?or LCD?) display ? Just 3 digits would do, I think. This is the time to think about it. Might as well make your tiki as custom and as “trick” as you like, just so long as it works in the real world. Whatever display is used, it’ll still have to be viewed through a clear watertight panel or hatch.

https://www.sparkfun.com/categories/170

Mee_n_Mac:
I note the timer in the vid used one for motor on/off and the other for mode/state change and setting the time. I could keep that philosophy as well.

To explain how that might work, the timer would have 3 states; ready, timer setting and compass setting. It would power up in ready and stay there until the magnet is placed and held over the state sensor. The timer would then go to timer setting and increment (w/wrap around, just as in the video) the timer setting until the magnet is removed. The display would show whatever timer setting is current.

When the magnet is removed the timer goes into compass setting state and stays there until the magnet is again held over the state sensor. That action triggers the compass to take a bunch of readings and memorize the heading to be held. The display will show that is in process and when it’s complete … somehow. Ideally show the heading memorized. Removal of the magnet causes the timer to now go back into the ready state, where the circular process could repeat if desired.

The other Hall sensor is devoted to motor on/off control. Waving the magnet over it will turn the motor on (if off) and also place the state = ready (if not already). If the motor is on, the magnet will cause it to go off. All the settings are retained and can only be changed using the state sensor, as described above.

This interface is more like the one in the video and I do like the idea of having a motor on/off switch.

It all comes down to which you think makes the most sense to you.

I have been giving it more thought and have decided it would be easier to work like this.

Plug in batteries and charge status is indicated buy lighting the leds ( I changed my mind about this and think it will be usefull for second sets).

Hold magnet over separate sensor to set time.

Hold magnet over separate sensor to activate compass starting a led flashing and led going solid when direction it memorized instead of a servo wiggle…

Start motor with separate sensor. No led is necessary.

When the timer counts down to zero it will need to power off the board or the servo operating the rudder at least as well as stopping the motor.

I think it is much more intuitive to separate the functions

I have attacked a pfd of how the timer will be layed out on pcb.

I think this the easiest way.

Kewl. I’ll look at it on Saturday as the snow falls.

What does the compass lock LED indicate ? Once the timer and/or compass are set, do you want to be able to reset them to new settings w/o having to cycle the power ?

The compass lock led is to show when the compass has had enough time memorize the heading. It might be handy to be able to reset without having to cycle the power but not necessary if too difficult.

Mee_n_Mac how is the progress with the timer.

Sorry for the late reply. I’ve written all the code but not tested any of it. I’m on my mobile ATM but when I get back to my PC I’ll post it. If you want to be the alpha and beta tester … well I’ll warn you I’m a bad typist.