Go Down

Topic: How Can You Switch Between Two Codes? (Read 104 times) previous topic - next topic

TailChaser

Using Arduino UNO

Say I have a LED blinking once every second and another code that will make it blink very fast. Is there away to have it all on one chip, and switch between the two codes with a momentary switch? The only thing I can find on the web is about on-off switch. I guess I'm looking for a on-on switch to switch the flash pattern of the led.
If its possible could someone please show me example with the following two codes?

Code: [Select]

void setup() {

  pinMode(13, OUTPUT);
}


void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}


Code: [Select]

void setup() {

  pinMode(13, OUTPUT);
}


void loop() {
  digitalWrite(13, HIGH);
  delay(200);
  digitalWrite(13, LOW);
  delay(200);
}

AWOL

Just put a 5x or 1x multiplier in your delay() parameter, the value of which is toggled by the momentary switch.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UKHeliBob

Put each of the sets of code in their own function and call the appropriate one when required by reading the input in loop().

NOTE that there will only be one setup() and loop() function in the program.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Robin2

Something like this (not tested)
Code: [Select]
byte switchPin = 7;
boolean switchState;

void setup() {
    pinMode(switchPin, INPUT_PULLUP);
    pinMode(13, OUTPUT);
}

void loop() {
    switchState = digitalRead(switchPin);
    if (switchState == HIGH) {
        slowBlink();
    }
    else {
        fastBlink();
    }
}

void slowBlink() {
    digitalWrite(13, HIGH);
    delay(1000);
    digitalWrite(13, LOW);
    delay(1000);
}

void fastBlink() {
    digitalWrite(13, HIGH);
    delay(200);
    digitalWrite(13, LOW);
    delay(200);
}


For anything but a very simple program it is better to use millis() to manage timing without blocking. See Several Things at a Time

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

AWOL

The next question you're going to ask is "why does it take two seconds for the switch setting to take effect?", which is why it is really important not to use "delay()".

Useful hint:
Code: [Select]
pauseValue = 1200 - pauseValue;
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

BulldogLowell

#5
Apr 27, 2016, 04:25 pm Last Edit: Apr 27, 2016, 04:25 pm by BulldogLowell
prefer a non-blocking method to using delay()

you can play with this, which will switch to fast if you put an 'F' into the serial monitor and slow if anything else (not tested):

Code: [Select]
enum State{
  SLOW,
  FAST
};

State state = SLOW;
const byte ledPin = 13;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  if(Serial.available())
  {
    if(Serial.read() == 'F')
    {
      state = FAST;
    }
    else
    {
      state = SLOW;
    }
  }
  switch(state)
  {
    case FAST:
      fastToggleLED();
      break;
    case SLOW:
      slowToggleLED();
      break;
  }
}

void fastToggleLED()
{
  static unsigned long fastLedTimer;
  if (millis() - fastLedTimer >= 100UL)
  {
    digitalWrite(ledPin, !digitalRead(ledPin));
    fastLedTimer = millis();
  }
}
//
void slowToggleLED ()
{
  static unsigned long slowLedTimer;
  if (millis() - slowLedTimer >= 1250UL)
  {
    digitalWrite(ledPin, !digitalRead(ledPin));
    slowLedTimer = millis();
  }
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy