Help me with tish code !

Hello!

I would like to help me with this code below.

Trying to do this on another button in the same way, but then my code does not work! Please help me with whatever you.

const int switchPin = 30;                  
const int switchPin2 = 32;                   
int led = 14;
int led2 = 9;
int breakpoint = 2000;
long startTime; 
long duration;  

int val = 0;
int val2 = 0;

void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(switchPin2, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  digitalWrite(switchPin, HIGH);
  digitalWrite(switchPin2, HIGH);
  Serial.begin(9600);
}


void loop()
{
  val = digitalRead(switchPin);
  val2 = digitalRead(switchPin2);

  if (val == LOW) { 
  digitalWrite(led2, LOW);
  } 
  if (val2 == HIGH) { 
  digitalWrite(led2, LOW);
  }
   
   
  if(digitalRead(switchPin) == LOW)
  {
    digitalWrite(led, HIGH);
     startTime = millis();
     while(digitalRead(switchPin) == LOW){
     if (millis() - startTime == breakpoint){
     digitalWrite(led, LOW);
     delay(2000);
     digitalWrite(led2, HIGH);
         }
    }
        
  } 
     long duration = millis() - startTime;
     Serial.println(duration);
}

if (millis() - startTime == breakpoint){

Might want to use > instead of == here.

Your global variables val and val2 are not required - you might just as well use the results of the digitalRead() directly.

If you want to time the two LEDs independently then (1) you need separate startTime variables for each LED, and (2) you need to restructure your sketch to be non-blocking so that the two LEDs can be managed independently and concurrently.

The way I'd approach this would be to put the input pin numbers in an array, put the LED pin numbers in an array, put the startTime values in an array. I don't see that you need any other global data.

Write a piece of code that reads the input switch state, the output LED state and the startTime to decide whether it is necessary to turn the LED on or off. Design the code to be non-blocking (do not call delay(), do not stop and wait for anything to happen). Call that in a FOR loop for each array index.

What is tish code?

Possibly a typo…this?

But it would be a lot easier to help him if he told us what his code was supposed to do instead of keeping us guessing. Maybe it’s a secret?

I want with this code make this :

Example: I have one button. When the button is LOW turn on one PIN for 5 seconds and after 5 seconds turn on other pin for unlimited time.

And another question:

How to make One BUTTON when is HIGH or LOW turn on ones pin after 5 seconds for 5 seconds.

Ask for sample code. Thank you in advance !!!

The code you already posted uses the appropriate non-blocking approach in some places, it's just that it also includes some blocking code.

You need to use the non-blocking approach throughout if you want to be able to apply this behaviour for multiple inputs. I've already outlined the data and code you'd need for the non-blocking implementation.

I understand everyone says that the code is blocked!
My question is, if so tell me where is the error and how to fix it. Please as a fail because my English is poor and I can not understand much what you say to me a sample code.

This delay(2000);
is an example of blocking code.
When the processor hits this, nothing except interrupts get executed for at least two seconds.
If you want to do multiple things "at once", you have to get rid of any calls to "delay".

rebrand:
My question is, if so tell me where is the error and how to fix it.

I tried to do that in Reply #2. Which part of that don't you understand? I'm not going to write your code for you, but if there's something you aren't understanding perhaps somebody can clarify it for you.

The problem is that this code runs perfectly but when I started to add it to some other logical task she did not run and the above-mentioned code go.
For example, add the code.

const int switchPin = 30;                  
const int switchPin2 = 32;                   
int led = 14;
int led2 = 9;
int breakpoint = 2000;
long startTime; 
long duration;  

int val = 0;
int val2 = 0;

void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(switchPin2, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  digitalWrite(switchPin, HIGH);
  digitalWrite(switchPin2, HIGH);
  Serial.begin(9600);
}


void loop()
{
  val = digitalRead(switchPin);
  val2 = digitalRead(switchPin2);

  if (val == LOW) { 
  digitalWrite(led2, LOW);
  } 
  if (val2 == HIGH) { 
  digitalWrite(led2, LOW);
  }
   
   
  if(digitalRead(switchPin) == LOW)
  {
    digitalWrite(led, HIGH);
     startTime = millis();
     while(digitalRead(switchPin) == LOW){
     if (millis() - startTime == breakpoint){
     digitalWrite(led, LOW);
     delay(2000);
     digitalWrite(led2, HIGH);
         }
    }
        
  } 
     long duration = millis() - startTime;
     Serial.println(duration);
}

and when I add something else regardless of what it is not working
example.

int touch = 10;
int outTouch = 9;

void setup(){
pinMode(touch, INPUT);
pinMode(outTouch, OUTPUT);
}

void loop(){

if (digitalRead(touch) == HIGH){
digitalWrite(outTouch, HIGH);
}
else{
digitalWrite(outTouch, LOW);
}

and when I add something else regardless of what it is not working

Proof? That code does something. You expect it to do something. "Not working" implies nothing more than that the two somethings are not the same thing, but says nothing about what either something is.

When someone posts code that reads a switch, without saying how the switch is wired, we have to ask "So, how IS the switch wired?". You could have saved a round...

Different versions of the code you posted do different things and I'm not clear what your desired behaviour is.

So far you have used two switch inputs and two LED outputs. You've mentioned that when the first input is activated, you want the first LED to turn on. If the input remains activated for longer than two seconds then you want to switch the first LED off and the second LED on. I guess you want to turn all LEDs off when the first input is deactivated.

Is that the gist of it? None of this involves the second switch input so far. What behaviour is associated with the second switch input?

I have 2 buttons
Button 1 and Button 2 ....

when button 1 and 2 is LOW turn ON PIN for 5 skundi and after this 5 seconds turn ON another pin for unlimited time. (This should happen when the button 1 and button 2 are LOW)

I Try the code that I have written the above when I decide to add the following logic to other pins do not work anyway so the code works without his adding another.
With this code I'm trying to manage my time relays with one button or more times with different start and stop.

example:

int relay1 = 10;  //I want This relay turned on for 5 seconds when button1 and button2 is LOW

int relay2 = 11;  // I want This relay turned on after 5 seconds for unlimited time when button1 and 
button2 is LOW.

int relay3 = 12;  // I want tish relay turn on when button3 is HIGH for 5

int relay4  = 16; //I want this relay turn on after 5 seconds for 5 seconds

int button1 = 13;  // I want when the button1 and button2 is LOW turn on relay1 for 5 seconds and after this 5 seconds turn on relay2 for unlimited time.

int button2 = 14; //

int button3 = 15; // I want this button when is HIGH turn on relay3 for 5 seconds and relay4 after 5 seconds for 5 seconds.

I haven't understood what you mean by different start and stop times - so far, you've only described the behaviour when BOTH buttons are pressed, so they act conceptually as a single input. Can't you just connect the two switches in series and use your original sketch?

I think I understand. Can you just clarify what you want to happen when the button is released - do you want the sequence to continue until both 5-second intervals have elapsed, or do you want everything to stop immediately?

PeterH:
I think I understand. Can you just clarify what you want to happen when the button is released - do you want the sequence to continue until both 5-second intervals have elapsed, or do you want everything to stop immediately?

When button1 is HIGH menage 3 relays (relay1 and relay2 and relay3) with different times for start and stop.
And only One button menage 3 different pins with different times for start and stop.

I want example code for :
One button menage 3 pins with different time for start and stop.

Example:

1.When Button is HIGH Then menage relay1 HIGH for 5 seconds
2.And relay2 HIGH after 5 seconds, pause, start 5 seconds HIGH on relay3 . STOP. Be repeated only when the button is HiGH
This 2 exemples be performed with a one button when is HIGH. One INPUT with 3 OUTPUT .
Thank you for your attention.

So it’s three relays now, not two? In that case I’d be inclined to use a simple state machine with the state representing the relay that is current being activated. I assume you also want to be able to do something similar for a different set of relays controlled by a different switch. The code below assumes two switch inputs each controlling three relays:

// untested
const int INPUT_COUNT = 2; // number of input switches
const int RELAY_COUNT = 3; // number of relays controlled by each input
const unsigned long OUTPUT_DURATION = 5000; // duration of each relay activation in ms

const int inputPins[INPUT_COUNT] = { 3, 4 }; // put your actual switch input pin numbers here
const int relayPins[INPUT_COUNT][RELAY_COUNT] = { { 5, 6, 7}, { 8, 9, 10 } }; // put your actual relay pin numbers here
int currentRelay[INPUT_COUNT] = { -1, -1 };
unsigned long startTime[INPUT_COUNT];
	
void setup()
{
	//set pin modes etc 
}

void loop()
{
	handleSwitches();
	
	// do whatever else your sketch needs to do
}

void handleSwitches()
{
	for(int i = 0; i < INPUT_COUNT; i++)
	{
		if(currentRelay[i] < 0)
		{
			// no sequence currently in progress
			if(digitalRead(inputPins[i] == HIGH))
			{
				// start output sequence
				currentRelay[i] = 0;
				startTime[i] = millis();
				digitalWrite(relayPins[i][currentRelay[i]], HIGH); // activate the first relay for this input
			}
		}
		else
		{
			// sequence currently in progress
			if(millis() - startTime[i] >= OUTPUT_DURATION)
			{
				digitalWrite(relayPins[i][currentRelay[i]], LOW); // deactivate the previous relay
				currentRelay[i]++;
				if(currentRelay[i] < RELAY_COUNT)
				{
					digitalWrite(relayPins[i][currentRelay[i]], HIGH); // activate the next relay for this input
					startTime[i] += OUTPUT_DURATION; // reset the time for the next step
				}
				else
				{
					// sequence is complete
					currentRelay[i] = -1;
				}
			}
		}
	}
}