1 input - 3 outputs.

Hi people, I'll try to be concise.

Essentially I need 1 switch (relay in a remote control receiver) to swap(1,2,3,2,1,2,3...) between 3 outputs (transistors switching relays switching 24vAC )and give a short pulse each time. For all intents and purposes it could be 1 pushbutton used to cycle between outputs to light a diode for 1 sec. This is the setup I have been using to try my code.

I've been playing around with code for a few days but I can't get it to work right. Then again I am new to this. Mostly I've been copying code from others and trying to adapt it to my own setup but it doesn't work. I feel like I might be ignoring some ridiculously simple way to solve this.

I'm sorry, I'm not asking for someone to write the code for me, just wondering if you could give me a shove in the right direction or tell me how you would go about solving this. I would post some of my code but I think it would just be more confusing.

Cheers

Hi Letingi,
Can you post your code?

I've been playing around with code for a few days but I can't get it to work right.

people could read it and suggest improvements or point out what is causing the trouble.

Then again I am new to this

Read some stuff on Arduino basics and try out the example codes on the site. It will help.

Hi Letingi.

The simplest algorithm I can think of would be to have 2 global variables. One keeps track of which of the outputs is currently activated. You could call that variable "currOutput". The second variable keeps track of which output was activated previous to the current one. You could call that variable "prevOutput".

You can then write a series of "if" statements to determine which output should be activated next, based on the current and previously activated outputs. For example, "if (currOutput == 2 and prevOutput == 3) nextOutput = 1;" The variable nextOutput temporarily holds the new output to be activated and can be local to loop().

So your loop() would need to read the input (you may need to debounce). If the input has been activated, go through your "if" statements to find the next output to be activated. Once that has been determined, action it using digitalWrite or whatever, and then finally update prevOutput and currOutput.

Paul

Hi both of you and thank you for your replies.

Sorry for not posting the old code, I just figured it was better to discard it. And yes I will keep checking out the examples, they help a lot.

I made a stab at what you suggested PaulRB and although I understand you on a "logic level" I'm not sure how to implement it. I've been playing around with the code since you posted and I keep running into problems. I just have a very poor understand of how everything in the loop works together. I've hardware debounced the pushbutton to simplify things for now.

If anyone has any pointers for me how to make this work it would be greatly appreciated.

Cheers

const int led1= 9; // LED pins chosen.
const int led2= 10;
const int led3= 11;
const int switchPin = 4; // Pushbutton pin chosen.
int val = 0; // Variable for reading pushbutton status.
int currentState = 0; // Current state of pushbutton.
int previousState = 0; // Previous state of pushbutton.
int currentOutput = 0; // Current output pin.
int previousOutput = 0; // Previous output pin.
int nextOutput = 1; // Next output pin.

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);// Ledpins declared as outputs.
  pinMode(led3, OUTPUT);
  pinMode(switchPin, INPUT); // Pushbutton pin declared as input.

}

void loop(){

  // Input from pushbutton given a value.

  val = digitalRead(switchPin);  
  if (val == HIGH) 
    currentState = 1; 

  else  
    currentState = 0; 

  // nextOutput determined from currentOutput and previousOutput.


  if (currentOutput == 1 && previousOutput == 0)
    int nextOutput = 2;

  else if (currentOutput == 2 && previousOutput == 1)
    int nextOutput = 3;

  else if (currentOutput == 3 && previousOutput == 2)
    int nextOutput = 2;

  else if (currentOutput == 2 && previousOutput == 3)
    int nextOutput = 1;

  else if (currentOutput == 1 && previousOutput == 2)
    int nextOutput = 2;

  // State of pushbutton and nextOutput read to write to output.

  if (currentState == 1 && nextOutput == 1)
    digitalWrite(led1, HIGH);

  else if (currentState == 1 && nextOutput == 2)
    digitalWrite(led2, HIGH);

  else if (currentState == 1 && nextOutput == 3)
    digitalWrite(led3, HIGH);


  // Output pins read and assigned as currentOutput.


  if(digitalRead(led1 == HIGH))
    currentOutput = 1;


  else if(digitalRead(led2 == HIGH))
    currentOutput = 2;


  else if(digitalRead(led3 == HIGH))
    currentOutput = 3;


  previousState = currentState; // State of pushbutton updated.

  previousOutput = currentOutput; // Previous output pin updated.

  currentOutput = nextOutput; // Current output pin updated.
}

You need to take a break judging by the code :slight_smile:

Simplify it, break it down.... what I'm confused over is this supposed to be a set pattern? Since there's only one button? Eg 123, 321, 123 looped over and over?

Hehehe. Yeah I think you're right.

Yes, it's supposed to be a cycle like you described. Each push of the button should switch an output, move to the next output and wait for the button to switch it. Or move to next output and switch it on each push of the button.

I guess I don't know how to simplify things. I keep adding layers of code that probably contradict each other. It's confusing, so yeah I'll take a break :slight_smile:

You need a current counter, and a flag which tells you which direction you are currently moving in. Then the code to do the increment becomes simply:

// definitions
const int MIN_POSITION = 1;
const int MAX_POSITION = 3;
int position = MIN_POSITION;
int direction = 1;

...

// this code executes once per button press
position += direction; // advance in the current direction

if((position == MIN_POSITION) or (position == MAX_POSITION))
{
    direction = (-direction); // reverse direction of travel
}

Hello good people!

I thank you immensely for your help, I finally got it working correctly. Again, thanks to you.

Here's my code if anyone wants to debug it further (any criticism welcome) or can use it.

Cheers

const int led1= 9; // LED pins chosen.
const int led2= 10;
const int led3= 11;
const int switchPin = 4; // Pushbutton pin chosen.
int val = 0; // Variable for reading pushbutton status.
int currentState = 0; // Current state of pushbutton.
int previousState = 0; // Previous state of pushbutton.
const int MIN_POSITION = 1; //Lower limit of position.
const int MAX_POSITION = 3; //Higher limit of position.
int position = 2;
int direction = 1;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT); // Ledpins declared as outputs.
  pinMode(led3, OUTPUT);
  pinMode(switchPin, INPUT); // Pushbutton pin declared as input.
  Serial.begin(9600); // Serial started (to read position/debug).

}

void loop(){

  // Input from pushbutton given a value.

  val = digitalRead(switchPin);  
  if (val == HIGH){ 
    currentState = 1;
  }
  else {
    currentState = 0; 
  }

  // Check to see if input is HIGH and was LOW, then increase/decrease position value and reverse direction when limit is reached.

  if(currentState != previousState){

    if(currentState == 1){ 
      position += direction; 

      Serial.println(position);
      if((position == MIN_POSITION) or (position == MAX_POSITION))
      {
        direction = (-direction);
      }

    }
  }


  // State of pushbutton and position read to write to output.

  if (currentState == 1 && position == 1)
    digitalWrite(led1, HIGH);

  else digitalWrite(led1, LOW);

  if (currentState == 1 && position == 2)
    digitalWrite(led2, HIGH);

  else digitalWrite(led2, LOW);

  if (currentState == 1 && position == 3)
    digitalWrite(led3, HIGH);

  else digitalWrite(led3, LOW);


  previousState = currentState; //State of pushbutton updated.
  delay(50); 

}