I want to pulse a relay on/off to start a timer.

I have a micro switch that is connected to D2 with a 10k pulldown resistor.
A relay is connected to D4.

My project is an automatic switch to start the timer on my heat press.
The issue im facing is having to press the button once to start and once to stop.
So what i want to do is quickly pulse the relay on/off when the switch is pressed.
the timer will count down anywhere from 30 seconds to 8 minutes depending on the medium
the timer needs to be stopped by pressing the button in again.
so i would need to pulse and on/off once it detects a release of the switch.
i have tried about 5 different ways of going about it, but keeps looping or not activating the relay at all.

13887024_550572491820413_4090674192215234724_n.jpg

i have tried about 5 different ways of going about it,

Show us your attempts.

read how to use this forum, located at the top of every forum.
read #7
then post your code using code tags.

it is easy to have a relay turn on and off with any momentary button press.

schematics matter much more than pictures of machine. in point of fact, we cannot gleam any information from the machine.

if you press a button
you can start a timing cycle.

assuming you have read blink without delay, you would know that you can have a timer count for any period of time.

just make timingPeriod=millis() when the button is pressed, for as long as it is pressed.
then when you release, the actual timing starts.

const int relay = 4;  //Relay
const int Microswitch = 2;  //Microswitch

int StartButton ; // golbal variable for start button
unsigned long timingPeriod ; // global variable
unsigned long ElapsedTime ;

void setup() {
  pinMode(relay, OUTPUT);
  pinMode(Microswitch, INPUT);
  digitalWrite(relay, HIGH);
}

void loop() {

  StartButton = digitalRead(Microswitch);

  if (StartButton == HIGH) { // assuming it goes high when pressed.
    timingPeriod = millis();   // resets timingPeriod to = millis()
  }

  ElapsedTime = (millis() - timingPeriod);

  if (ElapsedTime  >= 100  && ElapsedTime <=   5000) {  // 1000 = 1 second
    digitalWrite(13, LOW);
    digitalWrite(relay, LOW);
    // then do that thing.
  }

  if (ElapsedTime  >=  5000) {
    digitalWrite(13, HIGH);
    digitalWrite(relay, HIGH);
    // turn off and wait for another time.
  }

}
}

how you determine your 30 seconds to 8 minutes is up to you. a pot ? set a value with some buttons ?

the problem with this is that once you release the button, the counter will keep counting.
After some 50 days, it will come back and re-set
In just under 2 months time, your heater will turn on again without warning. for 30 seconds to 8 minutes.

You could put in a loop that kills the timing after say, 20 minutes.

does this make sense ? or do you need it to be more formally structured ?

I didn't add any code as i'm not sure i'm structuring it right, hence approx 5 different ways of going about it. I set the timer on the controller itself, the arduino and switch are purely there for start stop reasons. The timing thing isnt an issue as i turn the press off after i use it, a few hours at the most. Ive tried putting case statements with and without debounce counters

if (reading == HIGH && millis() - time > debounce) { if (state == HIGH) state = LOW; delay(50); state = HIGH; } else state = HIGH; delay(50); state = LOW;

Its been a while since ive tackled any coding, and most of my attempts have been chopped and changed and now ive muddled alot of them up haha.

Basically i need this;

const int relay = 4; //Relay const int pin1 = 2; //Microswitch

void setup(){ pinMode(relay, OUTPUT); pinMode(pin1, INPUT); digitalWrite(relay, HIGH); }

void loop(){ if(digitalRead(pin1) == HIGH) { digitalWrite(relay, LOW); delay(500); } if (digitalRead(relay) == LOW); { digitalWrite(relay, HIGH); delay(500); } }

to run once when it is pushed to HIGH and once when released to LOW, then wait until i do it again.

coupule of things.

first, you did not read ' how to use this forum'

if you went to the to of any post and read the thread on ' how to use this forum'

the thread 'how to use this forum' would listed a few things. specifically #7 about code tags.

code tags put your code in a box that makes it easy to copy and read.

second, there is an auto-format feature that will make your program easier to follow. third, the beauty of C++ is that you can use meaningful names. pin1 = pin2 is not easy to follow

int myFavoriteBlueColoredMicroSwitch= 2 ; // microswitch

may be a bit much to write over and over, but when you add another dozen things and you are trying to figure out why the switch on pin2 is not working, and have to search the program for pin1, well, it makes you go krazy and makes it really hard for others to help.

you say microswitch. a microswitch is almost always a momentary switch. now it appears that your switch is a toggle switch, either in the ON position, or the OFF position.

as for your program, what do you mean by 'case' ?

take a minute and describe what your switches are like and every step.

just a note :

 if(digitalRead(pin1) == HIGH)
  {
    digitalWrite(relay, LOW);
    delay(500);
  }

on the first scan, the program sees the switch low.... you flip the switch....

the program sees the switch HIGH, then executes the if() statement as true it does the digitalwrite to the relay counts for 1/2 second. contines on after that 1/2 second.

comes back on teh next loop. the stattement is true, so it digitalwrites to the relay delays for 1/2 second

and will repeat this until you change the switch. the only timing you have is that when the switch is high, it delays for 1/2 second. there is no timing period that changes to some other state or does any other thing.

have you downloaded and tried to run my sketch ? since I do not know what is high and what is low, things might be backwards, the LED is ON when you want it off or the relay is OFF when you want it ON....

but the time-out is 5 seconds. should show you something.