Go Down

Topic: controlling loops with separate pushbuttons (Read 723 times) previous topic - next topic

technic6490

Hello, I was wondering if I could get some help with my project. I made nerf bars for my truck and I'm trying to set up a system with 3 momentary switches to control light patterns. there are 5 lights on each side of the truck and I want to do a back and forth scanner, a scanner that goes from both ends and meets in the middle, and a setting where they stay on steady. I have the circuitry built with transistors and everything and I have chunks of code set up to sequence the lights, but I can't figure out how to structure the whole thing. I was messing around with if then statements, but I think the program will just get stuck on whichever button is pressed first until the power is reset. I know each pattern will need a loop to keep repeating, but how do I interrupt the loop with one of the other switches and switch to a different loop?

Arrch

You need to re-structure your code so that you aren't performing any blocking type functions like delay(). There is an example called Blink Without Delay that you should study. Also, when asking for help in this section, it's a good idea to read the thread entitled "Read this before posting a programming question...", specifically the section 6.

MisterResistor

Congratulations on your very first post!  Send a copy of what you have put together so far.

technic6490

Okay so I changed my setup to use a 3 position rotary switch and I am trying to use while loops to run different functions but when I hook it all up nothing happens. The functions all work when separately uploaded. Can you set up multiple while loops like this? Only one input will be HIGH at a time. ( I know there's simpler ways to sequence the leds but I can't follow the code yet, this is simpler for me)
Code: [Select]
const int ledPin2 = 2;
const int ledPin3 = 3;
const int ledPin4 = 4;
const int ledPin5 = 5;
const int ledPin6 = 6;
const int wait = 45;
const int wait1 = 45;
const int wait2 = 1000;
const int inputSequence = 10;            //yellow wire
const int inputDual = 11;                //blue wire
const int inputOn = 12;                  //green wire

void setup(){
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(inputSequence, INPUT);
  pinMode(inputDual, INPUT);
  pinMode(inputOn, INPUT);
}

void loop(){
  while (inputSequence == HIGH){
    Sequence();
  }
  while (inputDual == HIGH){
    Dual();
  }
  while (inputOn == HIGH){
    On();
  }
}

void Sequence(){
      digitalWrite(ledPin2, HIGH);
      delay(wait1);
      digitalWrite(ledPin2, LOW);
      delay(wait);
      digitalWrite(ledPin3, HIGH);
      delay(wait1);
      digitalWrite(ledPin3, LOW);
      delay(wait);
      digitalWrite(ledPin4, HIGH);
      delay(wait1);
      digitalWrite(ledPin4, LOW);
      delay(wait);
      digitalWrite(ledPin5, HIGH);
      delay(wait1);
      digitalWrite(ledPin5, LOW);
      delay(wait);
      digitalWrite(ledPin6, HIGH);
      delay(wait1);
      digitalWrite(ledPin6, LOW);
      delay(wait);
      digitalWrite(ledPin5, HIGH);
      delay(wait1);
      digitalWrite(ledPin5, LOW);
      delay(wait);
      digitalWrite(ledPin4, HIGH);
      delay(wait1);
      digitalWrite(ledPin4, LOW);
      delay(wait);
      digitalWrite(ledPin3, HIGH);
      delay(wait1);
      digitalWrite(ledPin3, LOW);
      delay(wait);
      digitalWrite(ledPin2, HIGH);
      delay(wait1);
      digitalWrite(ledPin2, LOW);
      delay(wait2);

void Dual(){
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin6, HIGH);
      delay(wait1);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin6, LOW);
      delay(wait);
      digitalWrite(ledPin3, HIGH);
      digitalWrite(ledPin5, HIGH);
      delay(wait1);
      digitalWrite(ledPin3, LOW);
      digitalWrite(ledPin5, LOW);
      delay(wait);
      digitalWrite(ledPin4, HIGH);
      delay(wait1);
      digitalWrite(ledPin4, LOW);
      delay(wait);
      digitalWrite(ledPin3, HIGH);
      digitalWrite(ledPin5, HIGH);
      delay(wait1);
      digitalWrite(ledPin3, LOW);
      digitalWrite(ledPin5, LOW);
      delay(wait);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin6, HIGH);
      delay(wait1);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin6, LOW);
      delay(wait2);
}
void On(){
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, HIGH);
      digitalWrite(ledPin4, HIGH);
      digitalWrite(ledPin5, HIGH);
      digitalWrite(ledPin6, HIGH);
}

bubulindo

What should happen if you push two buttons at the same time?

Is there a priority to the sequences that you want to implement, or do you just want to run if there is a single selection made?

If that is the case...

Code: [Select]

while ((inputSequence == HIGH) && (inputDial == LOW) && (inputOn == LOW)){
    Sequence();
  }


This guarantees that for any lights to turn on you need to only have one selection made. But if this is a selection switch... this isn't useful.

How did you hook up your selection switch? Any Pull-up resistors there?

Try this:
Code: [Select]

  pinMode(inputSequence, INPUT);
  pinMode(inputDual, INPUT);
  pinMode(inputOn, INPUT);
  digitalWrite(inputSequence, HIGH);       // turn on pullup resistors
  digitalWrite(inputDual, HIGH);       // turn on pullup resistors
  digitalWrite(inputOn, HIGH);       // turn on pullup resistors


Apart from the pull-up resistors can't think of anything that would cause this not to work.
You must understand that once you change the selection, the function has to end running before the new sequence can start. That can take a couple of seconds.
Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Go Up