Using Millis with switch state cases

Hi all.
I've been attempting to put together a programme. This has two outputs one for turning on and off an electro magnet and one for an alternating polarity magnetic coil (clock).
Ive put this together but I need a little more control within each case.
Please see my code-


// Define the pins being used for the states on the LN298N

#define magnet_pin_one  7     //int1 L298N Motor Control Magnet pins from Arduino D8
#define magnet_pin_two  8 
#define dial_pin_three  9     //int3 L298N Motor Control pilot dial pins Arduino from D9          
#define dial_pin_four   10    //int4 L298N Motor Control pilot dial pins from Arduino D10         
#define hallpin         2     //Hall sensor output to Arduino D2
int state = 0;
int old = 0;
int hallSwitchpoll = 0;



void setup(){ 

 

//Set the pin directions
 pinMode(hallpin,INPUT);
 pinMode(magnet_pin_one,OUTPUT);    
 pinMode(magnet_pin_two,OUTPUT);
 pinMode(dial_pin_three, OUTPUT);     
 pinMode(dial_pin_four, OUTPUT);     

 digitalWrite(magnet_pin_one, LOW);
 digitalWrite(magnet_pin_two, LOW);
 digitalWrite(dial_pin_three, HIGH);
 digitalWrite(dial_pin_four, LOW); 
 
}

void loop() {
 
 hallSwitchpoll = digitalRead(hallpin);
 if(hallSwitchpoll == 1){
   delay (50);
   hallSwitchpoll = digitalRead (hallpin);
   if (hallSwitchpoll == 0){
     state = old + 1;

     
   }}
else{
 delay (100);

}
switch (state) {
case 1:
    digitalWrite(magnet_pin_one, LOW);
    digitalWrite(magnet_pin_two, LOW);
    digitalWrite(dial_pin_three, LOW);   
    digitalWrite(dial_pin_four, HIGH);
    old = state;
    break;

case 2:
    
    digitalWrite(magnet_pin_one, HIGH);
    digitalWrite(magnet_pin_two, LOW);
    digitalWrite(dial_pin_three, HIGH);
    digitalWrite(dial_pin_four, LOW);
    old = state;

  default:
    digitalWrite(magnet_pin_one, HIGH);
    digitalWrite(magnet_pin_two, LOW);
    digitalWrite(dial_pin_three, HIGH);
    digitalWrite(dial_pin_four, LOW);
    old = 0;
    break;
}

}

What this code does is Ready the input pin on a Nano (2) conected to the output of an Hall effect sensor. This triggers off the sequance I require. Basically each time the hall switch is triggered there are two action 1) it switches the output high/low to the electro magnet and 2) switches high/low, low/high to the electromagnet clock.
Any help would greatly appretiated, see below.

At this point need the digitalWrite(magnet_pin_one, LOW); to go LOW after 500ms or so.

case 1:
    digitalWrite(magnet_pin_one, LOW);
    digitalWrite(magnet_pin_two, LOW);
    digitalWrite(dial_pin_three, LOW);   
    digitalWrite(dial_pin_four, HIGH);
    old = state;
    break;

I'm very new so please be gentle and I'm not asking for the work to be done just some guidance so I understand more about my options and how best to impliment them. The code at this point is working but without the required (go LOW after millis)
Any help would definatly be welcome, I'm beggining to not be able to see the woods for the trees.
Many thanks

A little more information please.

Using delay( ) is almost never recommended.


You don’t have to.


Where are your comments in the sketch ?


Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

Here are some tutorials on using millis() for timing.

Several things at a time.
Beginner's guide to millis().
Blink without delay().

Hi LarryD
Yes I do understand that DELAY isn't the way forwards for this. I think I need to had in some millis control?
I'll comment out the code if it helps you understand my requirements.

Please give the information as requested.

Hi groundFungus.
Thanks for that to be honest I've read that much like Ive said I cant see the wood for the trees at the moment.
Am I able to add some sort of Millis code within each case ?

will do..

BTW

Editing your previous posts for other than punctuation and spelling makes the thread difficult to follow.

Just add more information to a new post.

Please find commented out code. I hope you get the drift of what Im looking for.
I appoligise for bad spelling, it is an issue for me I realise.

Photo's to follow.

/*
This code is 'working progress' required to enable the controlling of two pieces of hardware.
1).An electro magnet on pins 7,8 on the Nano.
2).An alternating polarity electro magnet used in an clock motor.
The switch on pin 2 of the Nano is an Hall swicth via a magnet.
Driving the hardware by usining a L298N Motor Controler.
*/


// Define the pins being used for the states on the LN298N
#define magnet_pin_one  7     //int1 L298N Motor Control Magnet pins from Arduino D8
#define magnet_pin_two  8 
#define dial_pin_three  9     //int3 L298N Motor Control pilot dial pins Arduino from D9          
#define dial_pin_four   10    //int4 L298N Motor Control pilot dial pins from Arduino D10         
#define hallpin         2     //Hall sensor output to Arduino D2

// variables to hold the new and old switch states
int state = 0;
int old = 0;
int hallSwitchpoll = 0;



void setup(){ 

  
 
//Set the pin directions
  pinMode(hallpin,INPUT);
  pinMode(magnet_pin_one,OUTPUT);    // pin 7 on the Nano output
  pinMode(magnet_pin_two,OUTPUT);    // pin 8 on the Nano output
  pinMode(dial_pin_three, OUTPUT);   // pin 9 on the Nano output 
  pinMode(dial_pin_four, OUTPUT);    // pin 10 on the Nano output
  
// driving the pins to their initial state
  digitalWrite(magnet_pin_one, LOW);   // pin 7 on the Nano low
  digitalWrite(magnet_pin_two, LOW);   // pin 8 on the Nano low
  digitalWrite(dial_pin_three, HIGH);  // pin 9 on the Nano High
  digitalWrite(dial_pin_four, LOW);    // pin 10 on the Nano low
  
}

void loop() {

  //reads the vallue of the hall sensor at pin 2 of the nano.
  
  hallSwitchpoll = digitalRead(hallpin);
  if(hallSwitchpoll == 1){
    delay (50);
    hallSwitchpoll = digitalRead (hallpin);
    if (hallSwitchpoll == 0){
      state = old + 1;

      
    }}
 else{
  delay (100);

 }
switch (state) {

//In this case when pin 2 of the Nano goes Low (via the hall sensor) the sequance of events are has follows-
  
case 1:
     digitalWrite(magnet_pin_one, LOW);  //pin 7 on the Nano goes low
     digitalWrite(magnet_pin_two, LOW);  //pin 8 on the Nano goes low
     digitalWrite(dial_pin_three, LOW);  //pin 9 on the Nano goes low 
     digitalWrite(dial_pin_four, HIGH);  //pin 10 on the Nano goes high
     old = state;
     break;
     
//In this case when pin 2 of the Nano goes Low (via the hall sensor) the sequance of events are has follows-
case 2:
     
     digitalWrite(magnet_pin_one, HIGH);  //pin 7 on the Nano goes high
     digitalWrite(magnet_pin_two, LOW);   //pin 8 on the Nano goes low
     digitalWrite(dial_pin_three, HIGH);  //pin 9 on the Nano goes high
     digitalWrite(dial_pin_four, LOW);    //pin 10 on the Nano goes low
     old = state;

   default:
     digitalWrite(magnet_pin_one, HIGH);
     digitalWrite(magnet_pin_two, LOW);
     digitalWrite(dial_pin_three, HIGH);
     digitalWrite(dial_pin_four, LOW);
     old = 0;
     break;
}

}
    case 2:

      digitalWrite(magnet_pin_one, HIGH);  //pin 7 on the Nano goes high
      digitalWrite(magnet_pin_two, LOW);   //pin 8 on the Nano goes low
      digitalWrite(dial_pin_three, HIGH);  //pin 9 on the Nano goes high
      digitalWrite(dial_pin_four, LOW);    //pin 10 on the Nano goes low
      old = state;

Did you want a break after this case ?





Sorry about the large images. The test circuit is't complete by no means at the moment. I have two LED's to stand in for the alternating magnet clock movement and the other is a stand in for the electro magnet.

What is that old thing ?

I think I do yes.. thanks for that pointer.

That old thing LarryD is a movement from an old electromagnet slave clock. While it is't what I will be acctually driving when complete it serves for what I need. the Actual electro magnet is two coils in series that will trurn on when the hall sensor is activated by a magnet.

    //In this case when pin 2 of the Nano goes Low (via the hall sensor) the sequance of events are has follows-
    case 2:

      digitalWrite(magnet_pin_one, HIGH);  //pin 7 on the Nano goes high
      digitalWrite(magnet_pin_two, LOW);   //pin 8 on the Nano goes low
      digitalWrite(dial_pin_three, HIGH);  //pin 9 on the Nano goes high
      digitalWrite(dial_pin_four, LOW);    //pin 10 on the Nano goes low
      old = state;

    default:
      digitalWrite(magnet_pin_one, HIGH);
      digitalWrite(magnet_pin_two, LOW);
      digitalWrite(dial_pin_three, HIGH);
      digitalWrite(dial_pin_four, LOW);

These have the same output, is that by design ?

To explain a little further.
I have a clock with a pendulum, the pendulum is drivven by an electro magnet. The Pendulum has an iron bar at the bottom and a magnet about half way up. The magnet passes the Hall switch and thus the circuit runs through it's course of events.

  1. Pass the hall sensor via a pendulum and magnet,
  2. The circuit is triggered,
  3. & 4) the circuit drives the electro magnet to drive the pendulum and also the electro magnet in the clock dial.

Looks like fun !

Please fill in the 3rd sequence you want to happen:

1st
magnet_pin_one = 0
magnet_pin_two = 0
dial_pin_three = 0
dial_pin_four = 1

2nd
magnet_pin_one = 1
magnet_pin_two = 0
dial_pin_three = 1
dial_pin_four = 0

3rd
magnet_pin_one = ?
magnet_pin_two = ?
dial_pin_three = ?
dial_pin_four = ?

Here is a first kick at the cat:

// Define the pins being used for the states on the LN298N

#define magnet_pin_one      7     //int1 L298N Motor Control Magnet pins from Arduino D8
#define magnet_pin_two      8
#define dial_pin_three      9     //int3 L298N Motor Control pilot dial pins Arduino from D9          
#define dial_pin_four       10    //int4 L298N Motor Control pilot dial pins from Arduino D10         
#define hallpin             2     //Hall sensor output to Arduino D2

const byte heartbeatLED   = 13;

byte FSMstate             = 0;
byte lastHallSwitchState  = 0;

unsigned long heartbeatMillis;
unsigned long hallSensorMillis;

//**********************************************************
void setup()
{
  pinMode(heartbeatLED, OUTPUT);

  //Set the pin directions
  pinMode(hallpin, INPUT);
  pinMode(magnet_pin_one, OUTPUT);
  pinMode(magnet_pin_two, OUTPUT);
  pinMode(dial_pin_three, OUTPUT);
  pinMode(dial_pin_four, OUTPUT);

  digitalWrite(magnet_pin_one, LOW);
  digitalWrite(magnet_pin_two, LOW);
  digitalWrite(dial_pin_three, HIGH);
  digitalWrite(dial_pin_four, LOW);

}

//**********************************************************
void loop()
{
  //*****************************
  //flash the heartbeat LED every 250ms
  if (millis() - heartbeatMillis >= 250)
  {
    //retart the TIMER
    heartbeatMillis = millis();
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //*****************************
  //check the hall sensor every 10 milliseconds
  if (millis() - hallSensorMillis > 10)
  {
    //restart the TIMER
    hallSensorMillis = hallSensorMillis + 10;

    checkHallSensor();
  }

  //*****************************
  //check the FSM (finite state machine)
  checkFSM();

} //END of loop()


//**********************************************************
void checkFSM()
{
  switch (FSMstate)
  {
    //*************
    case 0:
      digitalWrite(magnet_pin_one, LOW);
      digitalWrite(magnet_pin_two, LOW);
      digitalWrite(dial_pin_three, LOW);
      digitalWrite(dial_pin_four, HIGH);

      break;

    //*************
    case 1:

      digitalWrite(magnet_pin_one, HIGH);
      digitalWrite(magnet_pin_two, LOW);
      digitalWrite(dial_pin_three, HIGH);
      digitalWrite(dial_pin_four, LOW);

      break;

    //*************
    case 2:
      digitalWrite(magnet_pin_one, HIGH);
      digitalWrite(magnet_pin_two, LOW);
      digitalWrite(dial_pin_three, HIGH);
      digitalWrite(dial_pin_four, LOW);

      break;
  }
}

//**********************************************************
void checkHallSensor()
{
  byte hallSwitchState;

  //**************
  //change state detected ?
  hallSwitchState = digitalRead(hallpin);

  if (lastHallSwitchState != hallSwitchState)
  {
    //update to the new state
    lastHallSwitchState = hallSwitchState;

    if (hallSwitchState == LOW) //use LOW or HIGH as you need
    {
      //next state in FSM
      FSMstate++;
      
      FSMstate = FSMstate % 2;

    }
  }

} //END of checkHallSensor()


1 Like