First Arduino Program

Hey guys. I finally was able to setup all the components to my Arduino project now im just stuck on the actual programming side. So basically what im trying to perform is this function. When the push button is toggled on, send a signal to my relay for 2 seconds on 1 second off for a 6000 times cycle. and if the push button is toggled off stop the program. and when turned on again start the program from the beginning. if what im asking is too vague, hopefully i can provide more information. So far this is what I have for my program.

// Constants, They Will not change
 const int pushbutton = 9; // pushbutton pin # 
const int relay = 3; // relay pin # 
const long interval = 2000; // relay on/off interval 
const long cycle = 12000000 // Running the the full on off cycle 6000 times

// Variables They Will change 
int relaystate = LOW; // relaystate used to set the relay 
unsigned long previousMillis = 0; // will store last time relay was updated

void setup() { 
pinMode (pushbutton,INPUT);
pinMode (relay, OUTPUT); 
}

void loop() 
{ 
    if (digitalRead(pushbutton) == HIGH) 
     {
       unsigned long currentMillis = millis();
       if (currentMillis - previousMillis >= interval) 
        { 
         previousMillis = currentMillis; 
        } 
      }
}

First, if your pushbutton is wired between 5V and input pin, you need a 10k pulldown resistor from input pin to GND, without that the pin will be “floating” when the button is not pressed and might read HIGH or LOW or anything in between, easier way is connect the button between pin and GND, and in setup(),

pinMode(pushbutton,INPUT_PULLUP);

Then the pin will read HIGH when not pressed and LOW when pressed.
Second, I see no code in loop() to switch a relay.
6vbduK2_d.jpg
Hook up your button like S3.

// Constants, They Will not change
const int pushbutton = 9; // pushbutton pin #
const int relay = 3; // relay pin #
const long interval = 2000; // relay on/off interval
const long cycle = 12000; // Running the the full on off cycle 6000 times

// Variables They Will change
int relaystate = LOW; // relaystate used to set the relay
unsigned long previousMillis = 0; // will store last time relay was updated
int Counter;
void setup() {
  pinMode (pushbutton, INPUT);
  pinMode (relay, OUTPUT);
}

void loop()
{
  if (digitalRead(pushbutton) == HIGH)
  {

    static  unsigned long currentMillis = millis();
    if ((Counter < cycle) && ( currentMillis - previousMillis >= interval))
    {
      previousMillis = currentMillis;

      digitalWrite(relay , !digitalRead(relay));
      Counter++;
    }
  }
  else{
    Counter = 0;
  }
}

added a counter to meet your 6000 cycle limit and added a simple toggle for the relay pin.
hope this helps.
Nice job using blink without delay

Z

Thank you guys! yeah I'm all over the place with this.

outsider:
First, if your pushbutton is wired between 5V and input pin, you need a 10k pulldown resistor from input pin to GND, without that the pin will be "floating" when the button is not pressed and might read HIGH or LOW or anything in between, easier way is connect the button between pin and GND, and in setup(),

pinMode(pushbutton,INPUT_PULLUP);

Then the pin will read HIGH when not pressed and LOW when pressed.
Second, I see no code in loop() to switch a relay.
Hook up your button like S3.

in regards to this i would have my switch between 9 and gnd. But have yet to actually run the system itself with the board it would just be setup that way. i Feel like i made a mistake with that. did I?

zhomeslice:
added a counter to meet your 6000 cycle limit and added a simple toggle for the relay pin.
hope this helps.
Nice job using blink without delay

thanks again! now im curious will this havethe relay on for x amount of time and off for x amount of time?

Smileys_:
Thank you guys! yeah I'm all over the place with this.

thanks again! now im curious will this havethe relay on for x amount of time and off for x amount of time?

Your timer program triggers every "interval" 2 Seconds .
The toggle program I added looks at the current digital output and toggles it using the ! NOT boolean operator that is described here:
https://www.arduino.cc/reference/en/language/structure/boolean-operators/logicalnot/

digitalWrite(relay , !digitalRead(relay));

Z

I too was impressed by a beginner using millis()
Well done.
BTW, is there any relationship between the title, an FAP ? (!)

Hey i was able to get everything up and running after adjusting my code. But its nor performing what i wanted. The code isn’t sending a looping signal to switch on and off. When i turn it on its on indefinitely. If i press my push button it turns off. But when i press the button again it doesn’t turn on. The code is below.

// Constants, They Will not change
const int pushbutton = 9; // pushbutton pin #
const int relay = 3; // relay pin #
const long interval = 2000; // relay on/off interval
const long cycle = 12000; // Running the the full on off cycle 6000 times

// Variables They Will change
int relaystate = LOW; // relaystate used to set the relay
unsigned long previousMillis = 0; // will store last time relay was updated
int counter;

void setup() {
  pinMode(pushbutton,INPUT_PULLUP);
  pinMode (relay, OUTPUT);
}

void loop()
{
  if (digitalRead(pushbutton) == HIGH)
  {

    static  unsigned long currentMillis = millis();
    if ((counter < cycle) && ( currentMillis - previousMillis >= interval))
    {
      previousMillis = currentMillis;

      digitalWrite(relay , !digitalRead(relay));
      counter++;
    }
  }
  else{
    counter = 0;
  }
}
else{
    counter = 0;
    digitalWrite(relay , LOW);
}

Add this
Z

im starting to think its how i have everything setup since i feel like i did it wrong lol. In the picture below the relay that im using is actually this RELAY. And for the power supply for the relay i have a 5v power supply.