Go Down

Topic: controlling loops with separate pushbuttons (Read 770 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).

technic6490

#5
Feb 13, 2013, 04:30 am Last Edit: Feb 13, 2013, 05:20 am by technic6490 Reason: 1
Haha I enabled those right before I read your post bubulindo, I rewired the switch to give a ground to any one of the inputs but it's stuck on Sequence still
edit* I also changed the triggers on the while loops from HIGH to LOW. It seems like I need to have a way to have each loop check the switch and jump back to the main loop

MisterResistor

How about incorporating a "switch" statement and wrapping your sequencer functions?

switch (position of switch or 2 pushbuttons to give you '00' '01' '10' '11' values )
{
       case '1':
       Sequence1();
       break;

       case '2':
       Sequence2();
       break;

       case '3'
       Sequence3();
       break;

   }

Arrch

Code: [Select]

const int inputSequence = 10;            //yellow wire
const int inputDual = 11;                //blue wire
const int inputOn = 12;                  //green wire
...
void loop(){
  while (inputSequence == HIGH){
    Sequence();
  }
  while (inputDual == HIGH){
    Dual();
  }
  while (inputOn == HIGH){
    On();
  }


Why are you comparing the pin numbers of your switch to a state?

This is a perfect example of why you should be using more descriptive names like inputSequencePin, inputDualPin, inputOnPin.

technic6490

I renamed my pin designations and tried a switch statement but now all it does is skip to the last sequence. It looks like it should work to 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 inputSequencePin = 10;                //yellow wire 10
const int inputDualPin = 11;                    //blue wire 11
const int inputOnPin = 12;                      //green wire 12
int var = 0;

void setup(){
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(inputSequencePin, INPUT);
  digitalWrite(inputSequencePin, HIGH);          //enable pull-up resistor
  pinMode(inputDualPin, INPUT);
  digitalWrite(inputDualPin, HIGH);              //enable pull-up resistor
  pinMode(inputOnPin, INPUT);
  digitalWrite(inputOnPin, HIGH);                //enable pull-up resistor
}

void loop(){
  if (digitalRead(inputSequencePin == LOW))      //yellow wire 10
  { 
    var = 1;
  }
  if (digitalRead(inputDualPin == LOW))          //blue wire 11
  {
    var = 2;
  }
  if (digitalRead(inputOnPin == LOW))            //green wire 12
  {
    var = 3;
  }
  switch (var){
    case 1:
      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);
      break;
   
    case 2:
      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);
      break;
   
    case 3:
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, HIGH);
      digitalWrite(ledPin4, HIGH);
      digitalWrite(ledPin5, HIGH);
      digitalWrite(ledPin6, HIGH);
      break;
  }
   
}

PeterH

Code: [Select]

if (digitalRead(inputSequencePin == LOW))      //yellow wire 10

var = 1;
}
if (digitalRead(inputDualPin == LOW))          //blue wire 11
{
var = 2;
}
if (digitalRead(inputOnPin == LOW))            //green wire 12
{
var = 3;
}


Should be:

Code: [Select]

if (digitalRead(inputSequencePin) == LOW)      //yellow wire 10

var = 1;
}
if (digitalRead(inputDualPin) == LOW)          //blue wire 11
{
var = 2;
}
if (digitalRead(inputOnPin) == LOW)            //green wire 12
{
var = 3;
}
I only provide help via the forum - please do not contact me for private consultancy.


Go Up