Show Posts
Pages: [1]
1  Using Arduino / Project Guidance / Re: Kitchen Timer with Relay and Magnetic Switch on: April 07, 2013, 10:59:55 pm
I removed some unnecessary code for switching between minutes_hold and minutes and the same with the seconds_hold and seconds. What I was trying to do there was store the current variables and pause the timer. When the user presses the encoder in, I wanted the timer to resume from that time. For some reason, the timer seems to continue counting in the background even when the Pause function is selected. This is the main issue in the code right now. In addition, when the button is pressed in to first start the timer, the time displayed jumps down about 5 seconds or so.

I tried to stop the timer from counting down by putting a boolean "ispaused" and having the timer only run if "ispaused" = false. When the pause function is called, I would set "ispaused" = true. This did not fix anything.

Regarding the delays: I put them in to help with some responsiveness issues with the encoder. Without the delays, the encoder is too sensitive to rotation and the button presses are registered too frequently. I am sure they are not the ideal solution, and have been looking for a better solution.

I moved the encoders to pins 2 and 3 to allow me to try out interrupts. These did not make the encoder any more responsive and would have required a full rewrite of my SetTimer() function.

Thanks again, and sorry about the poor formatting and sloppy code.
2  Using Arduino / Project Guidance / Re: Kitchen Timer with Relay and Magnetic Switch on: April 07, 2013, 10:59:32 pm
First off, thanks for the help!
I am very new to Arduino and pieced this code together from other examples I have found online. This may explain any inconsistencies you saw. I went through the program and tried to clean things up a bit.
Here is the revised code:
Code:
#include <SoftwareSerial.h>

//CONFIGURATION:
/// Pins From Components to Arduino:///
const int LCD_TX = 9;          //TX On LCD Board
const int LCD_RX = 8;           //RX On LCD Board
const int Encoder_A = 3;       //3rd Pin on the Top of Potentiometer
const int Encoder_B = 2;       //1st Pin on the Top of Potentiometer
const int Encoder_Press = 4;   //1st Pin on Bottom of Potentiometer
const int MagSwitch= 13;        //Either Lead from Magnet Switch
const int Relay = 16;            //CTRL from Relay Board

/*
Other Setup Information:
- VCC from LCD Board to 5v                        
- GND from LCD Board to Ground
- 1st Pin on Bottom of Potentiometer to Ground
- 2nd Pin of Potentiometer to Ground              
- Other Lead of Magnet Switch to Ground
- 5V of Relay Board to 5V
- GND of Relay Baord to Ground
*/

// VARIABLES:
const int increment = 30; //change this value to change the seconds increment when setting the timer
const int Encoder_Sensitivity = 150;
const int Press_Sensitivity = 150;

//DONT CHANGE THIS STUFF!////////////////////////////////////////////////////////////////////////////////////////////////////
SoftwareSerial Serial7Segment(LCD_TX, LCD_RX);
#define COLON       4
char tempString[4];

long millisTimer;
byte seconds = 00;
byte minutes = 00;
byte seconds_hold;
byte minutes_hold;

int mode = 0;
#define Set_Timer   0
#define Run_Timer   1
#define Pause_Timer 2
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void _ResetPins(){
  pinMode(Encoder_A, INPUT);
  digitalWrite(Encoder_A, HIGH);

  pinMode(Encoder_B, INPUT);
  digitalWrite(Encoder_B, HIGH);

  pinMode(Encoder_Press, INPUT);
  digitalWrite(Encoder_Press, HIGH);

  Serial7Segment.write(0x77);  // Decimal, colon, apostrophe control command
  Serial7Segment.write(1<<COLON);
}
///READ ENCODER:///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void _lowlevel_ReadEncoder(int &rotate, int& press){
  rotate = (digitalRead(Encoder_B) * 2) + digitalRead(Encoder_A);
  press = digitalRead(Encoder_Press);
}
///SET TIMER://///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void SetTimer(){ //Reads the State of the Encoder and adjust the Display accordingly. CW rotation = + "increment" seconds, CCW = - "increment" seconds
  int Position, Press;
  int Position2, Press2;

    _ResetPins();
  _lowlevel_ReadEncoder(Position, Press);

  do  {
    _lowlevel_ReadEncoder(Position2, Press2);
  }
  while ((Position2 == Position) && (Press2 == Press));

  if (Position2 != Position){ //if there is a poisition change:
    // "Forward" is shown by the position going from (0 to 1) or (1 to 3)
    // or (3 to 2) or (2 to 0).  Anything else indicates that the user is
    // turning the device the other way.  Remember: this is Gray code, not
    // binary.
    int isFwd = ((Position == 0) && (Position2 == 1)) ||
      ((Position == 1) && (Position2 == 3)) ||
      ((Position == 3) && (Position2 == 2)) ||
      ((Position == 2) && (Position2 == 0));
    delay(Encoder_Sensitivity);
    if (isFwd == 1) {//if Encoder is moved forwards (CW), advance seconds by defined increment value
      seconds_hold += increment;
    }
    if (!isFwd){ // if encoder is moved backwards(CCW):
      if (seconds_hold == 0){// if we are already at zero seconds, check to make sure we can reduce a minute
        if (minutes_hold > 0){// if the minutes can be reduced (i.e. not zero) then remove one minute and reset seconds to 59      
          minutes_hold --;
          seconds_hold = 60-increment;
        }
      }
      else      {
        seconds_hold = seconds_hold - increment;//if seconds were not = 0, then decrease seconds value by the increment value
      }
    }
  }

  if (seconds_hold>59) {   // When seconds = 60:
    minutes_hold++;          // Add one minute
    seconds_hold -= 60;      // Reset seconds
  }

  //if the encoder is pressed down:
  if (digitalRead(Encoder_Press) == LOW)   {
    delay(Press_Sensitivity);
    seconds = seconds_hold;
    minutes = minutes_hold;
    mode = Run_Timer;
  }
  Position = Position2;
  Press = Press2;

  Serial7Segment.print('v');
  sprintf(tempString, "%02d%02d", minutes_hold, seconds_hold);
  Serial7Segment.print(tempString); //Send serial string out the soft serial port to the S7S
  //  Serial.println(tempString);
}

//COUNT DOWN///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void RunTimer() {    //this will start counting down from the time set using the encoder. This begins when the encoder button is pressed.

  if( (millis() - millisTimer) >= 1000)  {
    millisTimer += 1000;   //Adjust the timer forward 1 second
    if (seconds == 0){// if we are already at zero seconds, check to make sure we can reduce a minute
      if (minutes > 0){// if the minutes can be reduced (i.e. not zero) then remove one minute and reset seconds to 59  
        minutes --;
        seconds = 59;
      }
    }
    else{
      seconds--;//if seconds were not = 0, then decrease seconds value by the increment value
    }

    sprintf(tempString, "%02d%02d", minutes, seconds);
    Serial7Segment.write(0x77);  // Decimal, colon, apostrophe control command
    Serial7Segment.write(1<<COLON); // Turns on colon, apostrophoe, and far-right decimal
    Serial7Segment.print(tempString); //Send serial string out the soft serial port to the S7S
    //    Serial.println(tempString);
  }

  if (minutes == 0 & seconds == 0)  {
    digitalWrite(Relay, LOW);
    Serial7Segment.print("0000"); //Send the hour and minutes to the display
    mode = Set_Timer;
  }

  if (digitalRead(Encoder_Press)== LOW)  {
    delay(Press_Sensitivity);
    if (digitalRead(Encoder_Press)== LOW){
      mode = Pause_Timer;
    }
  }
}
//PAUSE:////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void PauseTimer()
{
  sprintf(tempString, "%02d%02d", minutes, seconds);
  Serial7Segment.write(0x77);  // Decimal, colon, apostrophe control command
  Serial7Segment.write(1<<COLON); // Turns on colon, apostrophoe, and far-right decimal
  Serial7Segment.write('v');  // clear screen
  Serial7Segment.print(tempString); //Send serial string out the soft serial port to the S7S

  if (digitalRead(Encoder_Press) == LOW)  {
    delay(Press_Sensitivity);
    if (digitalRead(Encoder_Press) == LOW) {
      mode = Run_Timer;
      return;
    }
  }
  mode = Set_Timer;
}

//SETUP:////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup(){
  // configure the pins
  _ResetPins();

  //  Serial.begin(9600);
  Serial7Segment.begin(57600); //Talk to the Serial7Segment at 9600 bps
  Serial7Segment.write(0x7A);  // Brightness control command
  Serial7Segment.write((byte) 80);  // Brightness control command

  pinMode(MagSwitch, INPUT);

  Serial7Segment.write('v'); //Reset the display - this forces the cursor to return to the beginning of the display
  Serial7Segment.print("0000"); //Send the hour and minutes to the display
}

void loop(){
  if (digitalRead(MagSwitch) == LOW) {
    switch(mode){
//      digitalWrite(Relay, LOW); //Relay off

      case Set_Timer: //reads encoder and sets time
      SetTimer();
      break;

      case Run_Timer: //counts down timer
      digitalWrite(Relay, HIGH); //relay on
      RunTimer(); //run countdown script (above)
      break;

      case Pause_Timer:
      digitalWrite(Relay, LOW);
      PauseTimer();
      break;  

    default:
      break;
    }
  }

  else {
    digitalWrite (Relay, LOW);
    
    Serial7Segment.write(0x77);  // Decimal, colon, apostrophe control command
    Serial7Segment.write(1<<COLON); // Turns on colon, apostrophoe, and far-right decimal
    Serial7Segment.print("OFF "); //Send the hour and minutes to the display
    mode = Set_Timer;
  }  
}

3  Using Arduino / Project Guidance / Re: Pausing a Countdown Timer using Switch Case and A Rotary Encoder on: March 12, 2013, 08:49:21 pm
And here is my entire code:
Code:
#include <SoftwareSerial.h>

//CONFIGURATION:
/// Pins From Components to Arduino:///
const int LCD_SS = 8;           //SS On LCD Board
const int LCD_RX = 7;           //RX On LCD Board
const int Encoder_A = 14;       //3rd Pin on the Top of Potentiometer to A0 on Arduino
const int Encoder_B = 15;       //1st Pin on the Top of Potentiometer to A1 on Arduino
const int Encoder_Press = 12;   //1st Pin on Bottom of Potentiometer
const int MagSwitch= 13;        //Either Lead from Magnet Switch
const int Relay = 2;            //CTRL from Relay Board

/// Other Setup Information:_________________________
// - VCC from LCD Board to 5v                        //
// - GND from LCD Board to Ground                    //
// - 1st Pin on Bottom of Potentiometer to Ground    //
// - 2nd Pin of Potentiometer to Ground              //
// - Other Lead of Magnet Switch to Ground           //
// - 5V of Relay Board to 5V                         //
// - GND of Relay Baord to Ground                    //
//___________________________________________________//

// VARIABLES:
const int increment = 30; //change this value to change the seconds increment when setting the timer
const int Encoder_Sensitivity = 150;
const int Press_Sensitivity = 150;

//////////////////////////////////////////////////////////////////////////////////////////////////////
SoftwareSerial Serial7Segment(LCD_SS, LCD_RX);
#define COLON       4
char tempString[4];

long millisTimer;
byte seconds = 00;
byte minutes = 00;

int mode = 0;
#define Set_Timer   0
#define Run_Timer   1
#define Pause_Timer 2

int seconds_hold;
int minutes_hold;

//---------------------------------------------------------------------------------------
static void _ResetPins()
{
  pinMode(Encoder_A, INPUT);
  digitalWrite(Encoder_A, HIGH);

  pinMode(Encoder_B, INPUT);
  digitalWrite(Encoder_B, HIGH);

  pinMode(Encoder_Press, INPUT);
  digitalWrite(Encoder_Press, HIGH);

  Serial7Segment.write(0x77);  // Decimal, colon, apostrophe control command
  Serial7Segment.write(1<<COLON);
}

//---------------------------------------------------------------------------------------
void _lowlevel_ReadEncoder(int &rotate, int& press)
{
  rotate = (digitalRead(Encoder_B) * 2) + digitalRead(Encoder_A);
  press = digitalRead(Encoder_Press);
}

//-----------------------------------------------------------------------------------------
void SetTimer() //Reads the State of the Encoder and adjust the Display accordingly. CW rotation = + "increment" seconds, CCW = - "increment" seconds
{
  int Position, Press;
  seconds = constrain (seconds, -1, 60); //constrains the seconds value between -1 and 60

    _ResetPins();
  _lowlevel_ReadEncoder(Position, Press);
  int Position2, Press2;
  do
  {
    _lowlevel_ReadEncoder(Position2, Press2);
  }
  while ((Position2 == Position) && (Press2 == Press));
  if (Position2 != Position) //if there is a poisition change:
  {
    // "Forward" is shown by the position going from (0 to 1) or (1 to 3)
    // or (3 to 2) or (2 to 0).  Anything else indicates that the user is
    // turning the device the other way.  Remember: this is Gray code, not
    // binary.
    int isFwd = ((Position == 0) && (Position2 == 1)) ||
      ((Position == 1) && (Position2 == 3)) ||
      ((Position == 3) && (Position2 == 2)) ||
      ((Position == 2) && (Position2 == 0));
    delay(Encoder_Sensitivity);
    if (isFwd == 1) //if Encoder is moved forwards (CW), advance seconds by defined increment value
    {
      seconds= seconds + increment;
    }
    if (isFwd == 0) // if encoder is moved backwards(CCW):
    {
      if (seconds == 0)// if we are already at zero seconds, check to make sure we can reduce a minute
      {
        if (minutes > 0)// if the minutes can be reduced (i.e. not zero) then remove one minute and reset seconds to 59
        {       
          minutes --;
          seconds = 60-increment;
        }
      }
      else
      {
        seconds = seconds - increment;//if seconds were not = 0, then decrease seconds value by the increment value
      }
    }
  }

  if (seconds>59)    // When seconds = 60:
  {
    minutes++;          // Add one minute
    seconds -= 60;      // Reset seconds
  }

  if (Press2 != Press) //if the encoder is pressed down:
  {
    delay(Press_Sensitivity);
    mode = Run_Timer;
  }
  Position = Position2;
  Press = Press2;

  Serial7Segment.print('v');
  sprintf(tempString, "%02d%02d", minutes, seconds);
  Serial7Segment.print(tempString); //Send serial string out the soft serial port to the S7S
  Serial.println(tempString);
}

//-------------------------------------------------------------------------------------------------
void countdownTimer() //this will start counting down from the time set using the encoder. This begins when the encoder button is pressed.
{
  seconds = constrain (seconds, -1, 60);
  if( (millis() - millisTimer) > 1000)
  {
    millisTimer += 1000; //Adjust the timer forward 1 second
    if (seconds == 0)// if we are already at zero seconds, check to make sure we can reduce a minute
    {
      if (minutes > 0)// if the minutes can be reduced (i.e. not zero) then remove one minute and reset seconds to 59
      {       
        minutes --;
        seconds = 59;
      }
    }
    else{
      seconds--;//if seconds were not = 0, then decrease seconds value by the increment value
    }
    sprintf(tempString, "%02d%02d", minutes, seconds);
    Serial7Segment.write(0x77);  // Decimal, colon, apostrophe control command
    Serial7Segment.write(1<<COLON); // Turns on colon, apostrophoe, and far-right decimal
    Serial7Segment.print(tempString); //Send serial string out the soft serial port to the S7S

    Serial.println(tempString);
  }

  if (minutes == 0 & seconds == 0)
  {
    digitalWrite(Relay, LOW);
    Serial7Segment.print("0000"); //Send the hour and minutes to the display
    mode = Set_Timer;
  }

  if (digitalRead(Encoder_Press)== LOW)
  {
    delay(Press_Sensitivity);
    mode = Pause_Timer;
  }
}

//-----------------------------------------------------------------------------------------
void PauseTimer()
{
  seconds_hold = seconds;
  minutes_hold = minutes;

  sprintf(tempString, "%02d%02d", minutes_hold, seconds_hold);
  Serial7Segment.write(0x77);  // Decimal, colon, apostrophe control command
  Serial7Segment.write(1<<COLON); // Turns on colon, apostrophoe, and far-right decimal
  Serial7Segment.print(tempString); //Send serial string out the soft serial port to the S7S

  if (digitalRead(Encoder_Press) == LOW)
  {
    delay(Press_Sensitivity);
    seconds = seconds_hold;
    minutes = minutes_hold; 
    mode = Run_Timer;
  }

  mode = Set_Timer;
}
//-------------------------------------------------------------------------------------------
void setup()
{
  // configure the pins
  _ResetPins();

  Serial.begin(9600);

  Serial7Segment.begin(9600); //Talk to the Serial7Segment at 9600 bps
  Serial7Segment.write('v'); //Reset the display - this forces the cursor to return to the beginning of the display
  Serial7Segment.print("0000"); //Send the hour and minutes to the display
}

void loop()
{
  if (digitalRead(MagSwitch) == LOW) //if magnetic switch is detected
    // switches modes depending on button press
  {
    switch(mode){
      digitalWrite(Relay, LOW); //Relay off
      case (Set_Timer): //reads encoder and sets time
      SetTimer();
      break;

      case (Run_Timer): //counts down timer
      digitalWrite(Relay, HIGH); //relay on
      //      minutes = minutes_hold;
      //      seconds = seconds_hold;
      countdownTimer(); //run countdown script (above)
      break;

      case (Pause_Timer):
      digitalWrite(Relay, LOW);
      PauseTimer();
      break;     
    }
  }

  else if (digitalRead(MagSwitch) == HIGH) //if no magnetic switch detected
  {
    digitalWrite (Relay, LOW);
    minutes = 0;
    seconds = 0;
    Serial7Segment.print("OFF "); //Send the hour and minutes to the display
    mode = Set_Timer;
  } 
}
4  Using Arduino / Project Guidance / Pausing a Countdown Timer using Switch Case and A Rotary Encoder on: March 12, 2013, 08:48:29 pm
Hello, I am working on making a countdown timer using a rotary encoder and a 7 segment 4 digit led display.
The program works as follows:
Case 1: Set Time
     - Rotate encoder to change seconds and minutes on display
     - If the encoder is pressed in, switch to the Run Timer Case
Case 2: Run Timer
     - Counts down from the time set in Case 1
     - If the encoder is pressed in, switch to the Pause Timer Case
Case 3: Pause Timer
     - Stop the timer from counting down.
     - Rotate encoder to change seconds and minutes on display
     - If the encoder is pressed in, switch to the Run Timer Case

Case 1 and 2 work perfectly. Pausing the timer allows me to change the values of the timer. When I press the encoder in again, however, the timer jumps to how many seconds have elapsed.
ie. If I pause the timer at 1:30 and wait 25 seconds before pressing the encoder again, the display quickly counts down from 1:30 to 1:05 and then continues to count as usual. If I wait more than 1:30, the timer speeds all the way down to 0.

I have tried to store my seconds and minutes values as new integers while the encoder is pressed, and then reassign the values when I press the encoder again. This does nothing though.
Below is the snippet of code that's not working. "mode = " is my switch case command.
Code:
void ReadEncoder() //Reads the State of the Encoder and adjust the Display accordingly. CW rotation = + "increment" seconds, CCW = - "increment" seconds
{
  int Position, Press;
  seconds = constrain (seconds, -1, 60); //constrains the seconds value between -1 and 60

    _ResetPins();
  _lowlevel_ReadEncoder(Position, Press);
  int Position2, Press2;
  do
  {
    _lowlevel_ReadEncoder(Position2, Press2);
  }
  while ((Position2 == Position) && (Press2 == Press));
  if (Position2 != Position) //if there is a poisition change:
  {
    // "Forward" is shown by the position going from (0 to 1) or (1 to 3)
    // or (3 to 2) or (2 to 0).  Anything else indicates that the user is
    // turning the device the other way.  Remember: this is Gray code, not
    // binary.
    int isFwd = ((Position == 0) && (Position2 == 1)) ||
      ((Position == 1) && (Position2 == 3)) ||
      ((Position == 3) && (Position2 == 2)) ||
      ((Position == 2) && (Position2 == 0));
    delay(Encoder_Sensitivity);
    if (isFwd == 1) //if Encoder is moved forwards (CW), advance seconds by defined increment value
    {
      seconds= seconds + increment;
    }
    if (isFwd == 0) // if encoder is moved backwards(CCW):
    {
      if (seconds == 0)// if we are already at zero seconds, check to make sure we can reduce a minute
      {
        if (minutes > 0)// if the minutes can be reduced (i.e. not zero) then remove one minute and reset seconds to 59
        {       
          minutes --;
          seconds = 60-increment;
        }
      }
      else
      {
        seconds = seconds - increment;//if seconds were not = 0, then decrease seconds value by the increment value
      }
    }
  }

  if (seconds>59)    // When seconds = 60:
  {
    minutes++;          // Add one minute
    seconds -= 60;      // Reset seconds
  }

  if (Press2 != Press) //if the encoder is pressed down:
  {
    delay(Press_Sensitivity);
    mode = Run_Timer;
  }
  Position = Position2;
  Press = Press2;

  Serial7Segment.print('v');
  sprintf(tempString, "%02d%02d", minutes, seconds);
  Serial7Segment.print(tempString); //Send serial string out the soft serial port to the S7S
  Serial.println(tempString);
}

//-------------------------------------------------------------------------------------------------
void countdownTimer() //this will start counting down from the time set using the encoder. This begins when the encoder button is pressed.
{
  seconds = constrain (seconds, -1, 60);
  if( (millis() - millisTimer) > 1000)
  {
    millisTimer += 1000; //Adjust the timer forward 1 second
    if (seconds == 0)// if we are already at zero seconds, check to make sure we can reduce a minute
    {
      if (minutes > 0)// if the minutes can be reduced (i.e. not zero) then remove one minute and reset seconds to 59
      {       
        minutes --;
        seconds = 59;
      }
    }
    else{
      seconds--;//if seconds were not = 0, then decrease seconds value by the increment value
    }
    sprintf(tempString, "%02d%02d", minutes, seconds);
    Serial7Segment.write(0x77);  // Decimal, colon, apostrophe control command
    Serial7Segment.write(1<<COLON); // Turns on colon, apostrophoe, and far-right decimal
    Serial7Segment.print(tempString); //Send serial string out the soft serial port to the S7S

    Serial.println(tempString);
  }

  if (minutes == 0 & seconds == 0)
  {
    digitalWrite(Relay, LOW);
    Serial7Segment.print("0000"); //Send the hour and minutes to the display
    mode = Set_Timer;
  }

  if (digitalRead(Encoder_Press)== LOW)
  {
    delay(Press_Sensitivity);
    mode = Pause_Timer;
  }
}

//-----------------------------------------------------------------------------------------
void PauseTimer()
{
  seconds_hold = seconds;
  minutes_hold = minutes;

  sprintf(tempString, "%02d%02d", minutes_hold, seconds_hold);
  Serial7Segment.write(0x77);  // Decimal, colon, apostrophe control command
  Serial7Segment.write(1<<COLON); // Turns on colon, apostrophoe, and far-right decimal
  Serial7Segment.print(tempString); //Send serial string out the soft serial port to the S7S

  if (digitalRead(Encoder_Press) == LOW)
  {
    delay(Press_Sensitivity);
    seconds = seconds_hold;
    minutes = minutes_hold; 
    mode = Run_Timer;
  }

  mode = Set_Timer;
}

Any thoughts?
Thanks!
5  Using Arduino / Project Guidance / Re: Kitchen Timer with Relay and Magnetic Switch on: February 17, 2013, 07:46:10 pm
Quote
Still can't see your code.
It's posted above.... Not sure what's going on there.

Quote
However, your functions are doing too much. ReadEncoder() should do exactly that and NOTHING more.
 ReadEncoderAndDoABunchOfOtherCrap() would read the encoder and do a bunch of other crap.

So you can see my code? Not sure what you mean here either. Does it matter? Is this causing problems with my program?

Quote
Of course, if you really had a function like that, I'd just hit the back button and move onto the next post.
What does this mean?

Quote
Besides, it's hard to see the function of an encoder in a post titled "Kitchen Timer with Relay and Magnetic Switch", so some comments in the code would be helpful.
There are comments all over my code... Read the first post for some background information. The encoder is used as an input device for incrementing the seconds on a kitchen timer. When the button on the encoder is pressed in, the timer starts and the relay turns on. My problem is that the timer sometimes counts down way faster than 1 second each second. Some times it races down 45 seconds and resumes counting normally. Sometimes is races all the way down to zero. I think this has to do with the millis() command, as I wrote above.

Quote
You have very few Serial.print() statements to learn what the code is really doing.
I removed them after I set up my code and got it working, thinking that it could be the reason why the numbers are counting so fast sometimes. The only issue right now is the counter, and a serial.println of that mirrors what the 7 segment display is showing me.

Here's the updated code:

Code:
#include <SoftwareSerial.h>

//CONFIGURATION:////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Connect 7 Segment LED with RX at Pin 7 and SS at Pin 8
SoftwareSerial Serial7Segment(8, 7);

//Configures the pins for the Encoder:
//Connect Pin A of encoder to Pin 14 (A0) and Pin C of encoder to Pin 15 (A1), Pin B goes to ground
//Connect Button to either encoder pins, other goes to ground
enum enDigitalPins
{
  dpInEncoderA=14,
  dpInEncoderB=15,
  dpInEncoderPress=12,
};

#define MagSwitch  13
#define Relay       2

// this value determines how many seconds are incremented each time the encoder is rotated
int increment = 10;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
long millisTimer;
byte seconds = 00;
byte minutes = 00;
byte hours = 00;

int mode = 0;
char tempString[100]; //Used for sprintf

#define COLON       4
#define Set_Timer   0
#define Run_Timer   1
#define Pause_Timer 2

boolean colonOn = false;

static void _ResetPins()
{
  // Rotary encoder input lines
  // Configure as input, turn on pullup resistors
  pinMode(dpInEncoderA, INPUT);
  digitalWrite(dpInEncoderA, HIGH);

  pinMode(dpInEncoderB, INPUT);
  digitalWrite(dpInEncoderB, HIGH);

  pinMode(dpInEncoderPress, INPUT);
  digitalWrite(dpInEncoderPress, HIGH);

  digitalWrite(MagSwitch, INPUT);
  pinMode(MagSwitch, LOW);

  pinMode(Relay, OUTPUT);

  Serial7Segment.write(0x77);  // Decimal, colon, apostrophe control command
  Serial7Segment.write(1<<COLON);
}


void _lowlevel_ReadEncoder(int &rotate, int& press)
{
  rotate = (digitalRead(dpInEncoderB) * 2) + digitalRead(dpInEncoderA);
  press = digitalRead(dpInEncoderPress);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ReadEncoder() //Reads the State of the Encoder and adjust the Display accordingly. CW rotation = + "increment" seconds, CCW = - "increment" seconds
{
  int Position, Press;
  int isForward = 0;
  seconds = constrain (seconds, -1, 60); //constrains the seconds value between -1 and 60

    _ResetPins();
  _lowlevel_ReadEncoder(Position, Press);
  int Position2, Press2;
  do
  {
    _lowlevel_ReadEncoder(Position2, Press2);
  }
  while ((Position2 == Position) && (Press2 == Press));
  if (Position2 != Position) //if there is a poisition change:
  {
    // "Forward" is shown by the position going from (0 to 1) or (1 to 3)
    // or (3 to 2) or (2 to 0).  Anything else indicates that the user is
    // turning the device the other way.  Remember: this is Gray code, not
    // binary.
    int isFwd = ((Position == 0) && (Position2 == 1)) ||
      ((Position == 1) && (Position2 == 3)) ||
      ((Position == 3) && (Position2 == 2)) ||
      ((Position == 2) && (Position2 == 0));
    delay(100);
    if (isFwd == 1) //if Encoder is moved forwards (CW), advance seconds by defined increment value
    {
      //      delay(200); // prevents bad readings
      seconds= seconds + increment;
    }
    if (isFwd == 0) // if encoder is moved backwards(CCW):
    {
      //      delay(200);
      if (seconds == 0)// if we are already at zero seconds, check to make sure we can reduce a minute
      {
        if (minutes > 0)// if the minutes can be reduced (i.e. not zero) then remove one minute and reset seconds to 59
        {       
          minutes --;
          seconds = 50;
        }
      }
      else
      {
        seconds = seconds - increment;//if seconds were not = 0, then decrease seconds value by the increment value
      }
    }
  }

  if (seconds>59)    // When seconds = 60:
  {
    minutes++;          // Add one minute
    seconds -= 60;      // Reset seconds
  }

  if (Press2 != Press) //if the encoder is pressed down:
  {
    delay(250);
    mode = Run_Timer;
  }
  Position = Position2;
  Press = Press2;

  Serial7Segment.print('v');
  sprintf(tempString, "%02d%02d", minutes, seconds);
  Serial7Segment.print(tempString); //Send serial string out the soft serial port to the S7S
  Serial.println(tempString);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void countdownTimer() //this will start counting down from the time set using the encoder. This begins when the encoder button is pressed.
{
  seconds = constrain (seconds, -1, 60);
  if( (millis() - millisTimer) > 1000)
  {
    millisTimer += 1000; //Adjust the timer forward 1 second
    if (seconds == 0)// if we are already at zero seconds, check to make sure we can reduce a minute
    {
      if (minutes > 0)// if the minutes can be reduced (i.e. not zero) then remove one minute and reset seconds to 59
      {       
        minutes --;
        seconds = 59;
      }
    }
    else{
      seconds--;//if seconds were not = 0, then decrease seconds value by the increment value
    }
    sprintf(tempString, "%02d%02d", minutes, seconds);
    Serial7Segment.write(0x77);  // Decimal, colon, apostrophe control command
    Serial7Segment.write(1<<COLON); // Turns on colon, apostrophoe, and far-right decimal
    Serial7Segment.print(tempString); //Send serial string out the soft serial port to the S7S
   
    Serial.println(tempString);
  }

  if (minutes == 0 & seconds == 0)
  {
    digitalWrite(Relay, LOW);
    Serial7Segment.print("0000"); //Send the hour and minutes to the display
    mode = Set_Timer;
  }

  if (digitalRead(dpInEncoderPress)== LOW)
  {
    delay(250);
    mode = Pause_Timer;
  }
}

void PauseTimer()
{
  int seconds_hold;
  int minutes_hold;
  seconds_hold = seconds;
  minutes_hold = minutes;

  minutes = minutes_hold;
  seconds = seconds_hold;
  sprintf(tempString, "%02d%02d", minutes, seconds);
  Serial7Segment.write(0x77);  // Decimal, colon, apostrophe control command
  Serial7Segment.write(1<<COLON); // Turns on colon, apostrophoe, and far-right decimal
  Serial7Segment.print(tempString); //Send serial string out the soft serial port to the S7S

  if (digitalRead(dpInEncoderPress)== LOW)
  {
    delay(75);
    mode = Run_Timer;
  }

  mode = Set_Timer;
}

void setup()
{
  // configure the pins
  _ResetPins();
 
  Serial.begin(9600);

  Serial7Segment.begin(9600); //Talk to the Serial7Segment at 9600 bps
  Serial7Segment.write('v'); //Reset the display - this forces the cursor to return to the beginning of the display
  Serial7Segment.print("0000"); //Send the hour and minutes to the display
}

void loop()
{
  if (digitalRead(MagSwitch) == LOW) //if magnetic switch is detected
    // switches modes depending on button press
  {
    switch(mode){
      digitalWrite(Relay, LOW); //Relay off
      case (Set_Timer): //reads encoder and sets time
      ReadEncoder();
      break;

      case (Run_Timer): //counts down timer
      digitalWrite(Relay, HIGH); //relay on
      countdownTimer(); //run countdown script (above)
      break;

      case (Pause_Timer):
      digitalWrite(Relay, LOW);
      PauseTimer();
      break;     
    }
  }

  else if (digitalRead(MagSwitch) == HIGH) //if no magnetic switch detected
  {
    digitalWrite (Relay, LOW);
    minutes = 0;
    seconds = 0;
    Serial7Segment.print("OFF "); //Send the hour and minutes to the display
    mode = Set_Timer;
  } 
}


Hope this solves any issues you may have...
6  Using Arduino / Project Guidance / Re: Kitchen Timer with Relay and Magnetic Switch on: February 17, 2013, 04:49:18 pm
UPDATE:
It seems as though the fast counting down occurs mostly after I let the device sit while powered on for a while. Could it be an issue with the millis() command?

7  Using Arduino / Project Guidance / Re: Read Wacom Tablet information on Arduino on: February 17, 2013, 04:46:27 pm
Apparently it is only USB compatible, which limits my options. I really just want to dump any data from the board to an SD card to read later.
I'm not sure if there is any handshaking or anything going on behind the scenes before data is exchanged to the PC, so hopefully I can just read and dump the data with an Arduino
8  Using Arduino / Project Guidance / Re: Read Wacom Tablet information on Arduino on: February 11, 2013, 10:47:14 am
It is a Su-13W02E-03A digitizer from a tablet pc. It does support both USB and Serial data, so I can use whatever is easier. I am going to build a breakout board for the digitizer today to try and read data with Arduino. Would a Serial.println (DigitalRead(7)) work?

I am pretty new to Arduino and Serial communication, so any help is appreciated
9  Using Arduino / Project Guidance / Read Wacom Tablet information on Arduino on: February 08, 2013, 04:02:56 pm
Hello,
I am trying to read the USB data from a Wacom tablet on my Arduino Mega ADK. I have no idea where to start and was looking for guidance. Essentially, the tablet acts as a Human Interface Device (HID) and I want to Serial.println the data for debugging. How can I get my arduino to read from the USB port? Is it better to go about this another way? The tablet also has serial output, so I could communicate with the Arduino using serial if that is easier.

Thanks,
Sam
10  Using Arduino / Project Guidance / Re: Kitchen Timer with Relay and Magnetic Switch on: February 06, 2013, 08:44:20 pm
Is there a better way to do this?

I'm not familiar with that relay board, but I assume it takes a digital output and uses it to control the relay. In that case you would call pinMode(Relay, OUTPUT); once within setup(), and then call digitalWrite(Relay, HIGH); and digitalWrite(Relay, LOW); to turn the relay on and off as required.

So I was messing up and used pinMode(Relay, LOW) before and after fixing it, everything works out! There is still a problem though: when I start the timer, it quickly jumps a few seconds. Usually 3, but sometimes 15, and sometimes it counts really fast all the way down.
11  Using Arduino / Project Guidance / Re: Kitchen Timer with Relay and Magnetic Switch on: February 06, 2013, 06:54:12 pm
I don't know whether this is causing your problem, but you seem to be confusing pinMode and digitalWrite:

Code:
digitalWrite(Relay, OUTPUT);
pinMode(Relay, LOW);

Do you really mean to change the pin modes during the sketch?

I don't want to do it, but if I use digitalWrite(Relay, HIGH); or LOW, then the relay doesn't turn on or off at all. I'm using a Sparkfun Beefy Relay kit and can see the status LED quickly blink but that's it...

Is there a better way to do this? I tried making another if statement in my loop that uses a boolean Relay_On and would adjust the relay accordingly, but it doesn't work either.
12  Using Arduino / Project Guidance / Re: Kitchen Timer with Relay and Magnetic Switch on: February 06, 2013, 06:45:16 pm
So I have worked some more on this project and can control the Relay now by using pinMode(Relay, LOW) and HIGH.
I noticed that sometimes when I turn on the timer, it is much faster than 1 second between counting down seconds. It quickly drops to zero. This only happens after I disconnect the switch and reconnect it a couple of times... Any ideas why this is happening?

Thanks!

Code:
#include <SoftwareSerial.h>



//CONFIGURATION:////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Connect 7 Segment LED with RX at Pin 7 and SS at Pin 8
SoftwareSerial Serial7Segment(8, 7);

//Configures the pins for the Encoder:
//Connect Pin A of encoder to Pin 14 (A0) and Pin C of encoder to Pin 15 (A1), Pin B goes to ground
//Connect Button to either encoder pins, other goes to ground
enum enDigitalPins
{
  dpInEncoderA=14,
  dpInEncoderB=15,
  dpInEncoderPress=12,
};

#define MagSwitch  13
#define Relay       2

// this value determines how many seconds are incremented each time the encoder is rotated
int increment = 10;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

long millisTimer;
byte seconds = 00;
byte minutes = 00;
byte hours = 00;

int mode = 0;
char tempString[100]; //Used for sprintf

#define COLON       4
#define Set_Timer   0
#define Run_Timer   1

boolean colonOn = false;
boolean Timer_Pause = false;
boolean Relay_On = false;

static void _ResetPins()
{
  // Rotary encoder input lines
  // Configure as input, turn on pullup resistors
  pinMode(dpInEncoderA, INPUT);
  digitalWrite(dpInEncoderA, HIGH);

  pinMode(dpInEncoderB, INPUT);
  digitalWrite(dpInEncoderB, HIGH);

  pinMode(dpInEncoderPress, INPUT);
  digitalWrite(dpInEncoderPress, HIGH);

  digitalWrite(MagSwitch, INPUT);
  pinMode(MagSwitch, LOW);

  digitalWrite(Relay, OUTPUT);
  //  pinMode(Relay, LOW);

  Serial7Segment.write(0x77);  // Decimal, colon, apostrophe control command
  Serial7Segment.write(1<<COLON);
}


void _lowlevel_ReadEncoder(int &rotate, int& press)
{
  rotate = (digitalRead(dpInEncoderB) * 2) + digitalRead(dpInEncoderA);
  press = digitalRead(dpInEncoderPress);
}

//////////////////////////////////////////////////////////////////////////////////////////////
void ReadEncoder() //Reads the State of the Encoder and adjust the Display accordingly. CW rotation = + "increment" seconds, CCW = - "increment" seconds
{
  int Position, Press;
  int isForward = 0;
  seconds = constrain (seconds, -1, 60); //constrains the seconds value between -1 and 60

    _ResetPins();
  _lowlevel_ReadEncoder(Position, Press);
  int Position2, Press2;
  do
  {
    _lowlevel_ReadEncoder(Position2, Press2);
  }
  while ((Position2 == Position) && (Press2 == Press));
  if (Position2 != Position) //if there is a poisition change:
  {
    // "Forward" is shown by the position going from (0 to 1) or (1 to 3)
    // or (3 to 2) or (2 to 0).  Anything else indicates that the user is
    // turning the device the other way.  Remember: this is Gray code, not
    // binary.
    int isFwd = ((Position == 0) && (Position2 == 1)) ||
      ((Position == 1) && (Position2 == 3)) ||
      ((Position == 3) && (Position2 == 2)) ||
      ((Position == 2) && (Position2 == 0));

    if (isFwd == 1) //if Encoder is moved forwards (CW), advance seconds by defined increment value
    {
      delay(75); // prevents bad readings
      seconds= seconds + increment;
    }
    if (isFwd == 0) // if encoder is moved backwards(CCW):
    {
      delay(75);
      if (seconds == 0)// if we are already at zero seconds, check to make sure we can reduce a minute
      {
        if (minutes > 0)// if the minutes can be reduced (i.e. not zero) then remove one minute and reset seconds to 59
        {       
          minutes --;
          seconds = 50;
        }
      }
      else
      {
        seconds = seconds - increment;//if seconds were not = 0, then decrease seconds value by the increment value
      }
    }
  }

  if (seconds>59)    // When seconds = 60:
  {
    minutes++;          // Add one minute
    seconds -= 60;      // Reset seconds
  }

  if (Press2 != Press) //if the encoder is pressed down:
  {
    mode = Run_Timer;
  }
  Position = Position2;
  Press = Press2;

  Serial7Segment.print('v');
  sprintf(tempString, "%02d%02d", minutes, seconds);
  Serial7Segment.print(tempString); //Send serial string out the soft serial port to the S7S
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
void countdownTimer() //this will start counting down from the time set using the encoder. This begins when the encoder button is pressed.
{
  seconds = constrain (seconds, -1, 60);
  if (Timer_Pause == false)
  {
    if( (millis() - millisTimer) > 1000)
    {
      millisTimer += 1000; //Adjust the timer forward 1 second
      if (seconds == 0)// if we are already at zero seconds, check to make sure we can reduce a minute
      {
        if (minutes > 0)// if the minutes can be reduced (i.e. not zero) then remove one minute and reset seconds to 59
        {       
          minutes --;
          seconds = 59;
        }
      }
      else{
        seconds--;//if seconds were not = 0, then decrease seconds value by the increment value
      }

      sprintf(tempString, "%02d%02d", minutes, seconds);
      Serial7Segment.write(0x77);  // Decimal, colon, apostrophe control command
      Serial7Segment.write(1<<COLON); // Turns on colon, apostrophoe, and far-right decimal
      Serial7Segment.print(tempString); //Send serial string out the soft serial port to the S7S
      //Serial.println(tempString);   
    }

    if (minutes == 0 & seconds == 0)
    {
      pinMode(Relay, LOW);
      Serial7Segment.print("----"); //Send the hour and minutes to the display
      mode = Set_Timer;
    }
  }
  else
  {
    mode = Set_Timer;
  }
}


void setup()
{
  // configure the pins
  _ResetPins();

  // init serial communication
  //  Serial.begin(9600);
  //  Serial.println("Ready to begin");
  Serial7Segment.begin(9600); //Talk to the Serial7Segment at 9600 bps
  Serial7Segment.write('v'); //Reset the display - this forces the cursor to return to the beginning of the display
  Serial7Segment.print("----"); //Send the hour and minutes to the display
}


void loop()
{
  //    if (Relay_On == true)
  //  {
  //    pinMode(Relay, HIGH);
  //  }
  //  else
  //  {
  //    pinMode(Relay, LOW);
  //  }
  if (digitalRead(MagSwitch) == LOW){ //if magnetic switch is detected
    // switches modes depending on button press
    Relay_On = false;

    switch(mode){
      pinMode(Relay, LOW);
      case (Set_Timer): //reads encoder and sets time
      ReadEncoder();
      break;

      case (Run_Timer): //counts down timer
      delay(175);
      pinMode(Relay, HIGH);
      countdownTimer();
      break;
    }
  }
  else if (digitalRead(MagSwitch) == HIGH) //if no magnetic switch detected
  {
    pinMode (Relay, LOW);
    minutes = 0;
    seconds = 0;
    Serial7Segment.print("----"); //Send the hour and minutes to the display
    mode = Set_Timer;
  } 
}


13  Using Arduino / Project Guidance / Re: Kitchen Timer with Relay and Magnetic Switch on: January 31, 2013, 08:24:05 pm
Quote
I added some Serial.println() statements to debug earlier, making sure the switch was working and that my timer was correct. Is it bad to keep it in the setup right now?
Is it bad to keep what in the setup()? The Serial.print() statement? No. In fact, you need more of them, in loop() and other functions to see what is happening.

Okay, thanks
14  Using Arduino / Project Guidance / Re: Kitchen Timer with Relay and Magnetic Switch on: January 31, 2013, 08:16:15 pm
Code:
    if (minutes == 0 & seconds == 0)
    {
      //do nothing
    }
Do nothing? I thought you wanted to turn the relay off.


Yeah, I do, but it wasn't doing anything earlier when it was part of a loop statement. When I changed the HIGH to LOW's, I saw that the relay LED was flickering occasionally.


Code:
  if (digitalRead(MagSwitch) == LOW){ //if magnetic switch is detected
  else if (digitalRead(MagSwitch) == HIGH) //if no magnetic switch detected
If the output from digtialRead() isn't HIGH and it isn't LOW, what is it?

No Serial.print() statements outside of setup(). I'm sure that there is a reason for that, but what it could possibly be escapes me.

I added some Serial.println() statements to debug earlier, making sure the switch was working and that my timer was correct. Is it bad to keep it in the setup right now?


Thanks for the advice
15  Using Arduino / Project Guidance / Kitchen Timer with Relay and Magnetic Switch on: January 31, 2013, 06:20:57 pm
Hello,
I am working on a program for my Arduino UNO R3.
My goals are the following:
- detect presence of magnetic switch (https://www.adafruit.com/products/375)
- IF switch is detected, allow user to set the timer using an encoder (https://www.adafruit.com/products/377) and 4 x 7 segment LED display (https://www.sparkfun.com/products/11442)
- IF the encoder button is pressed down, begin counting down  AND enable the Relay (https://www.sparkfun.com/products/11042?) for a given time period (would alternate between on and off)
- IF the button is pressed again, allow the user to again set the time and turn off the Relay
- When the timer expires, turn off the Relay
- IF the magnetic switch does not detect a magnet, PAUSE everything and wait until it does.

my code looks like this so far:

Code:
#include <SoftwareSerial.h>



//CONFIGURATION:////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Connect 7 Segment LED with RX at Pin 7 and SS at Pin 8
SoftwareSerial Serial7Segment(8, 7);

//Configures the pins for the Encoder:
//Connect Pin A of encoder to Pin 14 (A0) and Pin C of encoder to Pin 15 (A1), Pin B goes to ground
//Connect Button to either encoder pins, other goes to ground
enum enDigitalPins
{
  dpInEncoderA=14,
  dpInEncoderB=15,
  dpInEncoderPress=12,
};

#define MagSwitch  13
#define Relay       9

// this value determines how many seconds are incremented each time the encoder is rotated
int increment = 10;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //DEFINITIONS:

long millisTimer;
byte seconds = 00;
byte minutes = 00;
byte hours = 00;

int mode = 0;
char tempString[100]; //Used for sprintf

#define COLON       4
#define Set_Timer   0
#define Run_Timer   1

boolean colonOn = false;
boolean Timer_Pause = false;

static void _ResetPins()
{
  // Rotary encoder input lines
  // Configure as input, turn on pullup resistors
  pinMode(dpInEncoderA, INPUT);
  digitalWrite(dpInEncoderA, HIGH);

  pinMode(dpInEncoderB, INPUT);
  digitalWrite(dpInEncoderB, HIGH);

  pinMode(dpInEncoderPress, INPUT);
  digitalWrite(dpInEncoderPress, HIGH);

  digitalWrite(MagSwitch, INPUT);
  pinMode(MagSwitch, LOW);

  digitalWrite(Relay, OUTPUT);
  pinMode(Relay, LOW);

  Serial7Segment.write(0x77);  // Decimal, colon, apostrophe control command
  Serial7Segment.write(1<<COLON);
}


void _lowlevel_ReadEncoder(int &rotate, int& press)
{
  rotate = (digitalRead(dpInEncoderB) * 2) + digitalRead(dpInEncoderA);
  press = digitalRead(dpInEncoderPress);
}

//////////////////////////////////////////////////////////////////////////////////////////////
void ReadEncoder() //Reads the State of the Encoder and adjust the Display accordingly. CW rotation = + "increment" seconds, CCW = - "increment" seconds
{
  int Position, Press;
  int isForward = 0;
  seconds = constrain (seconds, -1, 60); //constrains the seconds value between -1 and 60

    _ResetPins();
  _lowlevel_ReadEncoder(Position, Press);
  int Position2, Press2;
  do
  {
    _lowlevel_ReadEncoder(Position2, Press2);
  }
  while ((Position2 == Position) && (Press2 == Press));
  if (Position2 != Position) //if there is a poisition change:
  {
    // "Forward" is shown by the position going from (0 to 1) or (1 to 3)
    // or (3 to 2) or (2 to 0).  Anything else indicates that the user is
    // turning the device the other way.  Remember: this is Gray code, not
    // binary.
    int isFwd = ((Position == 0) && (Position2 == 1)) ||
      ((Position == 1) && (Position2 == 3)) ||
      ((Position == 3) && (Position2 == 2)) ||
      ((Position == 2) && (Position2 == 0));

    if (isFwd == 1) //if Encoder is moved forwards (CW), advance seconds by defined increment value
    {
      delay(50); // prevents bad readings
      seconds= seconds + increment;
    }
    if (isFwd == 0) // if encoder is moved backwards(CCW):
    {
      delay(50);
      if (seconds == 0)// if we are already at zero seconds, check to make sure we can reduce a minute
      {
        if (minutes > 0)// if the minutes can be reduced (i.e. not zero) then remove one minute and reset seconds to 59
        {      
          minutes --;
          seconds = 50;
        }
      }
      else
      {
        seconds = seconds - increment;//if seconds were not = 0, then decrease seconds value by the increment value
      }
    }
  }

  if (seconds>59)    // When seconds = 60:
  {
    minutes++;          // Add one minute
    seconds -= 60;      // Reset seconds
  }

  if (Press2 != Press) //if the encoder is pressed down:
  {
    mode = Run_Timer;
  }
  Position = Position2;
  Press = Press2;

  Serial7Segment.print('v');
  sprintf(tempString, "%02d%02d", minutes, seconds);
  Serial7Segment.print(tempString); //Send serial string out the soft serial port to the S7S
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
void countdownTimer() //this will start counting down from the time set using the encoder. This begins when the encoder button is pressed.
{
  seconds = constrain (seconds, -1, 60);
  if (Timer_Pause == false)
  {
    if( (millis() - millisTimer) > 1000)
    {
      millisTimer += 1000; //Adjust the timer forward 1 second
      if (seconds == 0)// if we are already at zero seconds, check to make sure we can reduce a minute
      {
        if (minutes > 0)// if the minutes can be reduced (i.e. not zero) then remove one minute and reset seconds to 59
        {      
          minutes --;
          seconds = 59;
        }
      }
      else{
        seconds--;//if seconds were not = 0, then decrease seconds value by the increment value
      }

      sprintf(tempString, "%02d%02d", minutes, seconds);
      Serial7Segment.print('v'); //Send serial string out the soft serial port to the S7S
      Serial7Segment.write(0x77);  // Decimal, colon, apostrophe control command
      Serial7Segment.write(1<<COLON); // Turns on colon, apostrophoe, and far-right decimal
      Serial7Segment.print(tempString); //Send serial string out the soft serial port to the S7S
      //    Serial.println(tempString);    
    }

    if (minutes == 0 & seconds == 0)
    {
      //do nothing
    }
  }
  else
  {

  }
}


void setup()
{
  // configure the pins
  _ResetPins();

  // init serial communication
  Serial.begin(9600);
  Serial.println("Ready to begin");
  Serial7Segment.begin(9600); //Talk to the Serial7Segment at 9600 bps
  Serial7Segment.write('v'); //Reset the display - this forces the cursor to return to the beginning of the display
  Serial7Segment.print("0000"); //Send the hour and minutes to the display
}


void loop()
{
  if (digitalRead(MagSwitch) == LOW){ //if magnetic switch is detected
    // switches modes depending on button press
    switch(mode){
      case (Set_Timer): //reads encoder and sets time
      ReadEncoder();
      break;

      case (Run_Timer): //counts down timer
      Timer_Pause = false;
      countdownTimer();
      digitalWrite(Relay, HIGH); //SHOULD turn on the Relay (loops too fast?)
      break;
    }
  }
  else if (digitalRead(MagSwitch) == HIGH) //if no magnetic switch detected
  {
    Timer_Pause = true; //pause the timer
  }  
}


I pieced it together with various bits of code I found online. I am pretty new to Arduino programming, and would love some help.

Currently, my program works well to set the time and count down. I am having issues with the timer stopping when the magnetic switch isn't there. In addition, my Relay won't turn off!

Any help is appreciated!

Thanks,
Sam


Moderator edit: [code] [/code] tags added.
edit: added links to parts
Pages: [1]