Start/stop a Loop

Hi everyone can someone help me please
i am a complete beginner in arduino environment and i need help
i have a arduino mega and i want to write a program that will do the following:
digital input high pin 25 start the loop digital input high pin 23 stop(reset) the loop , (in my case the loop is: 8 relay digital output 39-41-...53 which go on from relay 1 to 8 with 1 second break between wait 3 seconds and off from 1 to 8 ) .
Can someone please give me a code that will do that?

Can someone please give me a code that will do that?

What is the loop supposed to do?

void loop()
{
   if(digitalRead(25) == HIGH)
   {
      startTheLoop();
   }
}

void startTheLoop()
{
   while(digitalRead(23) != HIGH)
   {
       // do whatever needs doing
   }
}

Now, this assumes that you have outline ALL of your requirements. Which seems unlikely.

PaulS:

Can someone please give me a code that will do that?

What is the loop supposed to do?

void loop()

{
  if(digitalRead(25) == HIGH)
  {
     startTheLoop();
  }
}

void startTheLoop()
{
  while(digitalRead(23) != HIGH)
  {
      // do whatever needs doing
  }
}




Now, this assumes that you have outline ALL of your requirements. Which seems unlikely.

the loop :
digital output 39 high
break 1 sec
digital output 41 high
break 1 sec
digital output 43 high
break 1 sec
digital output 45 high
break 1 sec
digital output 47 high
break 1 sec
digital output 49 high
break 1 sec
digital output 51 high
break 1 sec
digital output 53 high
break 3 sec
digital output 39 low
break 1 sec
digital output 41 low
break 1 sec
digital output 43 low
break 1 sec
digital output 45 low
break 1 sec
digital output 47 low
break 1 sec
digital output 49 low
break 1 sec
digital output 51 low
break 1 sec
digital output 53 low
break 3 sec

So have you actually tried to code this loop?
This is not a write code for you forum. It is a help you with code forum.

Grumpy_Mike:
So have you actually tried to code this loop?
This is not a write code for you forum. It is a help you with code forum.

this is a simple code which will help me to understand the logic of programing overall will help me to create a complex program to command 4 heaters and 2 fans in a oven by my own algorithm

so thank all of you for any help

this is the code which i made

const int onbuttonPin = 25;     // buton start
const int offbuttonPin = 23;     // buton stop
const int ledPin =  37;      // iesire led
const int can1 =  39;      // releu-1
const int can2 =  41;      // releu-2
const int can3 =  43;      // releu-3
const int can4 =  45;      // releu-4
const int can5 =  47;      // releu-5
const int can6 =  49;      // releu-6
const int can7 =  51;      // releu-7
const int can8 =  53;      // releu-8

// variables will change:
int onbuttonState = 0;         // variabla pentru starea butonului on
int offbuttonState = 0;         // variabla pentru starea butonului off
int stare = 0;     // stare program


void setup() {
   pinMode(can1, OUTPUT);
    pinMode(can2, OUTPUT);
     pinMode(can3, OUTPUT);
      pinMode(can4, OUTPUT);
       pinMode(can5, OUTPUT);
        pinMode(can6, OUTPUT);
         pinMode(can7, OUTPUT);
          pinMode(can8, OUTPUT);
   pinMode(ledPin, OUTPUT);      
   pinMode(onbuttonPin, INPUT);  
   pinMode(offbuttonPin, INPUT);    
}

void loop(){
  // citeste valoarea butoanelor
  onbuttonState = digitalRead(onbuttonPin);
  offbuttonState = digitalRead(offbuttonPin);
  
  if (onbuttonState == HIGH) {     
    stare = 1;  
  } 
  if (offbuttonState == HIGH) {
    stare = 0;
  }
      if (stare==1){
    digitalWrite(ledPin, HIGH);
    digitalWrite(can1, HIGH);
    digitalWrite(can2, HIGH);
    digitalWrite(can3, HIGH);
    digitalWrite(can4, HIGH);
    digitalWrite(can5, HIGH);
    digitalWrite(can6, HIGH);
    digitalWrite(can7, HIGH);
    digitalWrite(can8, HIGH);
if (offbuttonState == HIGH) {
    stare = 0;
  } 
  delay(1000); 
if (offbuttonState == HIGH) {
    stare = 0;
  }  
  digitalWrite(ledPin, LOW);  
if (offbuttonState == HIGH) {
    stare = 0;
  }  
  delay(1000);
if (offbuttonState == HIGH) {
    stare = 0;
  }
  }
  else {
    // stinge ledul
    digitalWrite(ledPin, LOW);
    digitalWrite(can1, LOW);
    digitalWrite(can2, LOW);
    digitalWrite(can3, LOW);
    digitalWrite(can4, LOW);
    digitalWrite(can5, LOW);
    digitalWrite(can6, LOW);
    digitalWrite(can7, LOW);
    digitalWrite(can8, LOW); 
  } 
}

but my problem is that stop button state is verified only at the end of the loop and i have to push the button for 2 seconds
not the 2 seconds is the problem in this moment but when the program will get more complex the time will increase

please someone with more experience than me , have a look and suggest the solution for my problem

i have to push the button for 2 seconds
not the 2 seconds is the problem in this moment but when the program will get more complex the time will increase

So, get rid of all the calls to "delay()".
Have a look at the blink without delay example provided with the IDE

At the moment you have:-

const int can1 =  39;      // releu-1
const int can2 =  41;      // releu-2
const int can3 =  43;      // releu-3
const int can4 =  45;      // releu-4
const int can5 =  47;      // releu-5
const int can6 =  49;      // releu-6
const int can7 =  51;      // releu-7
const int can8 =  53;      // releu-8

Followed by

    digitalWrite(can1, HIGH);
    digitalWrite(can2, HIGH);
    digitalWrite(can3, HIGH);
    digitalWrite(can4, HIGH);
    digitalWrite(can5, HIGH);
    digitalWrite(can6, HIGH);
    digitalWrite(can7, HIGH);
    digitalWrite(can8, HIGH);

Why not do:-

for(int can = 39; can < 54; can++){
 digitalWrite(can, HIGH);
}

In place of all your lines.

AWOL:

i have to push the button for 2 seconds
not the 2 seconds is the problem in this moment but when the program will get more complex the time will increase

So, get rid of all the calls to "delay()".
Have a look at the blink without delay example provided with the IDE

very good idea, thank you

Grumpy_Mike:
At the moment you have:-

const int can1 =  39;      // releu-1

const int can2 =  41;      // releu-2
const int can3 =  43;      // releu-3
const int can4 =  45;      // releu-4
const int can5 =  47;      // releu-5
const int can6 =  49;      // releu-6
const int can7 =  51;      // releu-7
const int can8 =  53;      // releu-8



Followed by


digitalWrite(can1, HIGH);
    digitalWrite(can2, HIGH);
    digitalWrite(can3, HIGH);
    digitalWrite(can4, HIGH);
    digitalWrite(can5, HIGH);
    digitalWrite(can6, HIGH);
    digitalWrite(can7, HIGH);
    digitalWrite(can8, HIGH);



Why not do:-


for(int can = 39; can < 54; can++){
digitalWrite(can, HIGH);
}



In place of all your lines.

another good idea , is much shorter , but first i have to check if i can replace "can++" with "can=can+2" because i do not want the chanel 40 to go on the same time with channel 39

thank you

if i can replace "can++" with "can=can+2"

Yes.

Grumpy_Mike:

if i can replace "can++" with "can=can+2"

Yes.

thank you for interest , i have tried it myself and it really works

the new version and improved code

const int onbuttonPin = 25;     // buton start
const int offbuttonPin = 23;     // buton stop
const int ledPin =  37;      // iesire led

// variable care se schimba:
int onbuttonState = 0;         // variabla pentru starea butonului on
int offbuttonState = 0;         // variabla pentru starea butonului off
int program = 0;     // stare program
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 1000;           // interval at which to blink (milliseconds)

void setup()
{
  for(int can = 39; can < 54; can=can+2)
    {
 digitalWrite(can, OUTPUT);
    }
   pinMode(ledPin, OUTPUT);      
   pinMode(onbuttonPin, INPUT);  
   pinMode(offbuttonPin, INPUT);    
}
void loop()
{
 onbuttonState = digitalRead(onbuttonPin);
 offbuttonState = digitalRead(offbuttonPin); 
  if (onbuttonState == HIGH)
  {     
    program = 1;  
  } 
  if (offbuttonState == HIGH)
  {
    program = 0;
  }
      if (program==1)
      {
  blinkL();
  onrelay();
      }
else 
  {
    digitalWrite(ledPin, LOW);
    for(int can = 39; can < 54; can=can+2)
    {
 digitalWrite(can, LOW);
    }
  }
}
void blinkL()
{
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) 
  {
    previousMillis = currentMillis; 
      if (ledState == LOW)
        ledState = HIGH;
      else
        ledState = LOW;
  } 
  digitalWrite(ledPin, ledState);
}
void onrelay()
{
 for(int can = 39; can < 54; can=can+2)
  {
 digitalWrite(can, HIGH);
  }
}

any suggestions are well appreciated

The IDE's auto format feature would improve the layout of your code somewhat. Losing any repeated blank lines would help too. Moving the brackets for the loop function definition would leave the code looking less strange too.

Formatting aside, an offRelay function would be an obvious improvement. You could have another to do your pinmode commands too.

wildbill:
The IDE's auto format feature would improve the layout of your code somewhat. Losing any repeated blank lines would help too. Moving the brackets for the loop function definition would leave the code looking less strange too.

Formatting aside, an offRelay function would be an obvious improvement. You could have another to do your pinmode commands too.

look now over it , i have made some adjustments

 for(int can = 39; can < 54; can=can+2)
    {
 digitalWrite(can, OUTPUT);
    }

HIGh or LOW are the more usual parameters for digital write

Did you try using auto format?

AWOL:

 for(int can = 39; can < 54; can=can+2)

{
digitalWrite(can, OUTPUT);
    }



HIGh or LOW are the more usual parameters for digital write

Did you try using auto format?

sorry but i don't understand , could you be more specific?

Did you mean pinMode, and not digitalWrite?

Or were you asking about auto format?

When you post new code, please post it again rather than editing what you had - it's a lot less confusing.

AWOL:
Did you mean pinMode, and not digitalWrite?

Or were you asking about auto format?

in this FOR is pinMode but i don't know how to use auto format