Coding Assistance required :(

HI all J

Bought my first Arduino Uno board , I bought this board to be able to control 4 devices

My equipment

  • UNO
  • C4 channel Relay
  • Cheese Board
  • 4 switches

I managed to code the system to switch the relay off and on by pushing the button ( video attached)

But I need the system work in a logical process: as explained below

*all devices must on at all times

***** when button is pushed it must turn off device for 10sec and the turn the device back on***

Device A off ( Device B and D on cannot be put off )

Device B off (Device A and Device C cannot be put off)

Device C off (Device B and Device D cannot be put off)

Gate D off (Device A and Device C cannot be put off )

*all devices are on at all times

***** when button is pushed it must turn off device for 10sec and the turn the device back on***

I just cant seem to get the code correct L

Can anyone assist?

Welcome to the forum

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE


const int pushButton[] = {2, 3, 4, 5}; // define push button inputs
const int relayPin[] = {11, 10, 9, 8}; // output pins where 4 relays will be connected
String relayNames[] = {"CH1", "CH2", "CH3", "CH4"}; // Just put name for 4 relays
int pushed[] = {0, 0, 0, 0}; // status of each buttons
int relayStatus[] = {LOW, LOW, LOW, LOW}; // initial status of relay


void setup() {
  Serial.begin(9600);// initialize serial monitor
  for (int i = 0; i < 4; i++)
  {
    pinMode(pushButton[i], INPUT_PULLUP);
    pinMode(relayPin[i], OUTPUT);
    digitalWrite(relayPin[i], LOW);// initial relay status to be ON

  }
}

void loop() {
  for (int i = 0; i < 4; i++)
  {
    int  val = digitalRead(pushButton[i]);
    if (val == HIGH && relayStatus[i] == LOW) {

      pushed[i] = 1 - pushed[i];
      delay(100);
    }// if

    relayStatus[i] = val;

    if (pushed[i] == HIGH) {
      Serial.print(relayNames[i]);
      Serial.println(" ON");
      digitalWrite(relayPin[i], LOW);

    } else {
      Serial.print(relayNames[i]);
      Serial.println(" OFF");
      digitalWrite(relayPin[i], HIGH);

    }// else

  }// for
  Serial.println("==");
  delay(100);
}// loop end
1 Like

thank you

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

1 Like

After you have read the forum guidelines that tell you where and how to post your questions and code, you can edit your post using the :pencil2: down here :point_down: and highlight code and put in tags <|> with the button here :point_up_2:

You need to look up state machines and switch case

1 Like

Is there a practical difference between a Gate and a Device? From the code point of view, that is.

Also, why is

 C missing form A exclusion list
 D missing from B exclusion list
 A missing from C exclusion
 B missing from D exclusion list

??

What happens in the 10 second window if a second button is pressed?

Nothing in your code times 10 seconds. Yet.

If it truly is as simple as making 1 of 4 thing do something for 10 seconds whilst everything else stays the same and buttons are ignored, you can have a very simple program.

When you see a button, turn off the associate relay and delay ten seconds, turn it back on, rinse and repeat.

Perhaps your full specifications would require a more sophisticated approach.

I could invent very more complicated requirements, like pushing a button again would extend the time, or pushing another button would short cut the current 10 second item and begin a new one…

So the first step is getting it all down, what you really need to do with this. So we can understand and advice.

a7

1 Like

Really? Where?

thank you for your response. please bear in mind this is completely new territory for me :slight_smile:

  1. the device is a 12v magnetic lock that fits onto the gate ( already installed )

  2. when the 12V maglock is powered on the gate is locked ,
    when powered off the gate is released

so I basically have 4 gates, each gate has a maglock, I need them to work as explained below.

  • all devices must on at all times : meaning gates are locked
  • when a button is pushed it must turn off device for 10sec( allowing the gate to be opened) and the turn the device back on ( with below rules)

Rules
Device/Gate A off ( Device B and D on cannot be put off )
Device/Gate B off (Device A and Device C cannot be put off)
Device/Gate C off (Device B and Device D cannot be put off)
Device/Gate D off (Device A and Device C cannot be put off )

does this help?

sorry bro , I tried uploading the video but it does not allow me to
i could email to you.

To post images etc you need trust level 1, you can get there by:

  • Entering at least 5 topics
  • Reading at least 30 posts
  • Spend a total of 10 minutes reading posts

Users at trust level 1 can…

  • Use all core Discourse functions; all new user restrictions are removed
  • Send PMs
  • Upload images and attachments
1 Like

makes sense :slight_smile:

Initially, all four gates are locked.

Each gate has an unlock button.

When the button is pressed and the gate is allowed, the gate is unlocked for 10 seconds.

If Gate-A is unlocked, Gate-B and Gate-D cannot be unlocked.
If Gate-B is unlocked, Gate-A and Gate-C cannot be unlocked.
If Gate-C is unlocked, Gate-B and Gate-D cannot be unlocked.
If Gate-D is unlocked, Gate-A and Gate-C cannot be unlocked.

You have the 'one button per gate' stuff. What you need is the 'ten second' part and the 'forbidden gates' part.

For the 'forbidden gates' part you need to keep track of which gates are unlocked. For the 'ten seconds' part you keep track of when each unlocked gate was unlocked.

boolean unlocked[4] = {false, false, false, false};
unsigned long unlockTime[4];

Note: Conflicting gate indexes are ((index+1) mod 4) and ((index+3) mod 4):
0 conflicts with 1 and 3
1 conflicts with 2 and 0
2 conflicts with 3 and 1
3 conflicts with 0 and 2

For 'forbidden gates' you just check if the other gates are already unlocked before unlocking a gate.

  for (int i = 0; i < 4; i++)
  {
    if (digitalRead(pushButton[i]) == LOW) // Pushed
    && !unlocked[(i+1)%4] && !unlocked[(i+3)%4]) // No conflicting gate unlocked
   {
      unlocked[i] = true;
      unlockTime[i] = millis();
      digitalWrite(relayPin[i], HIGH); // Turn off relay to unlock
    }
  }

At the top of loop() you can check your gates to see if it is time to re-lock any.

  for (int i = 0; i < 4; i++)
  {
    if (unlocked[i] && millis() - unlockTime[i] >= 10000)
    {
      unlocked[i] = false; // Re-lock
      digitalWrite(relayPin[i], LOW); // Turn on relay to Lock
    }
  }
1 Like

Yes except:

What happens in the 10 second window if the same button is pressed again?

Also, it may not be necessary but I wouldn't mind hearing about why these requirements arise.

a7

after 10 seconds the power should be restored so when the gate is manually closed it will lock.

the reason for this is because of theft :frowning:

If you don't plan, something will or won't happen, which may or may not be what you expected.

For exapmle, @johnwasser's code looks like it will take ten seconds after the last time the switch is seen to be down. So pressing the button in the middle restarts the 10 second period.

And that's way too few words of description about why you need these four, and the peculiar lockout pagtern. Why does A lock out D and B, or whatever.

Again, we don't need to know, but I for one am curious.

a7

1 Like

Sounds like he may be building a man trap of sorts; like an airlock. That may be the reason to disable unlocking multiple locks at once. Just guessing.

Sometimes I just like to brute force it to make the behavior clear when learning.

const int DevA = 2;
const int DevB = 3;
const int DevC = 4;
const int DevD = 5;
// Connect Normally Open pushbutton between pin 6 & GND
const int pushbutton = 6;

void setup()
{
  pinMode(DevA, OUTPUT);
  pinMode(DevB, OUTPUT);
  pinMode(DevC, OUTPUT);
  pinMode(DevD, OUTPUT);

  pinMode(pushbutton, INPUT_PULLUP);
}

void loop()
{
  // All on
  digitalWrite(DevA, HIGH);
  digitalWrite(DevB, HIGH);
  digitalWrite(DevC, HIGH);
  digitalWrite(DevD, HIGH);

  // ***Device A off***
  while( digitalRead(pushbutton) );
  digitalWrite(DevA, LOW);
  delay(10000);
// ***Device A on***
  digitalWrite(DevA, HIGH);

I'm bored now, so fill out the rest for the other devices
}

Your code so far does not appear to be on track to accomplish the stated requirements.

My understanding so far is that:

Both A and C can be off at the same time, during that time B and D must remain on.

Both B and D can be off at the same time, during that time A and C must remain on.

Perhaps you could expand the idea a bit before you throw it over to some copying and repeating the logic you are willing to post.

a7

Ah, 4 separate switches. Missed that. My understanding was that there was one switch and as it was pressed, the system went through the sequence shown.