Merging Codes

Hi Guys, My first post

I've recently gotten into electronics as a new hobby that i can apply in school with design and technology

Right now, I'm just messing around with LED's and pots and stuff and attempted three beginner tutorials,
http://www.instructables.com/id/How-to-Make-an-LED-Ambient-Mood-Light-A-Beginner-/step3/Program-the-Arduino/Tutorial/

I have very limited knowledge with electronics, but i have a small understanding with variables and if statements, i think.

So basically my question is;
How can i combine those two projects i made in those tutorials?
I was thinking maybe using like, a push button to switch between the two codes.
So, i press a button and it goes into mood light mode, then again, then I'm able to freely change the colour with the pot?
I apologise if my terminology and wording could use some improvement
I'm open to all comments
Thanks

Grumpy_Mike has written a guide to merging code:

http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

This simple merge demo may be useful.

...R

hi again

I attempted to merge two codes
Arduino Ambient Mood Light - Pastebin.com & https://www.arduino.cc/en/Tutorial/Switch

I wanted it to work like the button is the switch that turns the mood light on and off, but when it uploaded, the green and blue are always on, and when i press the button, it just turns the red one on for about a second.

Attached is my code and picture of my circuit

int inPin = 2;         // the number of the input pin
int outPin = 9;
int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
int red = 0;
int green = 170;
int blue = 170;
int incR = 1;
int incG = 1;
int incB = 0;
 
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers
 
#define DELAY_TIME 500
#define MAX_BRIGHT 255
#define PIN_RED 9
#define PIN_GREEN 10
#define PIN_BLUE 11
 
void transition()
{
  if (red >= MAX_BRIGHT)
    incR = 0;
  else if (red <= 0)
    incR = 1;
  if (green >= MAX_BRIGHT)
    incG = 0;
  else if (green <= 0)
    incG = 1;
  if (blue >= MAX_BRIGHT)
    incB = 0;
  else if (blue <= 0)
    incB = 1;
 
  if (incR)
    red++;
  else
    red--;
  if(incG)
    green++;
  else
    green--;
  if(incB)
    blue++;
  else
    blue--;
}
 
void setColor()
{
  analogWrite(PIN_RED, red);
  analogWrite(PIN_GREEN, green);
  analogWrite(PIN_BLUE, blue);
}
 
void setup()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
}
 
void loop()
{
  reading = digitalRead(inPin);
 
  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      transition();
  setColor();
  delay(DELAY_TIME);
 
    time = millis();    
  }
 
  digitalWrite(outPin, state);
 
  previous = reading;
}

What's happening with my code?

Please save us time by posting your code here. And use the code button </> so it looks like this

...R

Hi,
Updated the post :slight_smile:

I can't tell from your program what were the two original programs that you tried to merge. I had thought you would post all three.

In any case, it is a short program and I suggest you get a few sheets of paper and a pencil and work through the program line by line writing down the results of each line as though your brain is the Arduino. If you look carefully at the program like that I think you will find the problem - whereas looking at it as a whole is not effective.

I suspect you will find that the delay() function gets in your way. You should use millis() to manage timing.

Have a look at planning and implementing a program which illustrates how to ordanize the code in functions as well as the use of millis().

...R

OK, so i looked at Robin2's post, and tried planning/implementing and writing my own code (with just a little bit of copy and paste). Looks good so far, no errors
Just having problems with the button.
Basically, i want the button to act as an on/off switch, where as of now it just works like, the lights stay on and when you hold the button they turn off
Heres a picture of my project and heres my code

//LED Mood Light
//Control RGB Led's, smoothly transition between colours
//Turn the function on and off with a button

int maxBright = 255; //Max Brightness the pins will reach

int delayTime = 20; //Delay Time, large delay time for longer transitions

int buttonPin = 2; //Pin With The Button

//Which pin for which colour
int redPin = 9;
int greenPin = 10;
int bluePin = 11;

//Initial colour value for each pin
int red = 0;
int green = 170;
int blue = 170;

//For setting the lights to off
int redOff = 0;
int greenOff = 0;
int blueOff = 0;

// Indicates whether a colour is incrementing (1) or decrementing (0).
int incR = 1;
int incG = 1;
int incB = 0;

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flicker

//Function for the blending of lights
void transition() { 
	if (red >= maxBright)
    incR = 0;
  else if (red <= 0)
    incR = 1;
  if (green >= maxBright)
    incG = 0;
  else if (green <= 0)
    incG = 1;
  if (blue >= maxBright)
    incB = 0;
  else if (blue <= 0)
    incB = 1;
 
  if (incR)
    red++;
  else
    red--;
  if(incG)
    green++;
  else
    green--;
  if(incB)
    blue++;
  else
    blue--;
	
}

//Sets colours to start of loop
void setColour() {
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
	
}

//Sets LEDs to off
void setOff() {
  analogWrite(redPin, redOff);
  analogWrite(greenPin, greenOff);
  analogWrite(bluePin, blueOff);
}

void setup()
{
 pinMode(buttonPin, INPUT);
}

void loop(){
 // read the pushbutton input pin:
  reading = digitalRead(buttonPin);

  // compare the reading to its previous state
  if (reading != previous) {
    // if the state has changed, increment the counter
    if (reading == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      transition();
      setColour();
      delay(delayTime);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      setOff(); 
    }
  }
 
  previous = reading;
}/code]

Try this

//LED Mood Light
//Control RGB Led's, smoothly transition between colours
//Turn the function on and off with a button

int maxBright = 255; //Max Brightness the pins will reach

int delayTime = 20; //Delay Time, large delay time for longer transitions

int buttonPin = 2; //Pin With The Button

//Which pin for which colour
int redPin = 9;
int greenPin = 10;
int bluePin = 11;

//Initial colour value for each pin
int red = 0;
int green = 170;
int blue = 170;

//For setting the lights to off
int redOff = 0;
int greenOff = 0;
int blueOff = 0;

// Indicates whether a colour is incrementing (1) or decrementing (0).
int incR = 1;
int incG = 1;
int incB = 0;

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are unsigned long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long time = 0;         // the last time the output pin was toggled
unsigned long debounce = 200;   // the debounce time, increase if the output flicker

//Function for the blending of lights
void transition() { 
	if (red >= maxBright)
    incR = 0;
  else if (red <= 0)
    incR = 1;
  if (green >= maxBright)
    incG = 0;
  else if (green <= 0)
    incG = 1;
  if (blue >= maxBright)
    incB = 0;
  else if (blue <= 0)
    incB = 1;
 
  if (incR)
    red++;
  else
    red--;
  if(incG)
    green++;
  else
    green--;
  if(incB)
    blue++;
  else
    blue--;
	
}

//Sets colours to start of loop
void setColour() {
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
	
}

//Sets LEDs to off
void setOff() {
  analogWrite(redPin, redOff);
  analogWrite(greenPin, greenOff);
  analogWrite(bluePin, blueOff);
}

void setup()
{
 pinMode(buttonPin, INPUT);
}

void loop(){
 // read the pushbutton input pin:
  reading = digitalRead(buttonPin);

  // compare the reading to its previous state
  if (reading != previous) {
	      if (reading == HIGH) {
		  state = !state; //toggle state
		  }
	  previous = reading;
  }
    // if the state has changed, increment the counter
    if (state == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      transition();
      setColour();
      delay(delayTime);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      setOff(); 
    }
 }

BillHo:
Try this

Where did you make changes ?

...R

Thanks Billho!!
And Robin, i looked through both codes

From what i could tell, he changed my longs to unsigned longs
and some stuff in the void loop, such as an added if statement and other things I'm unfamiliar with

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flicker/code]



[code]// the follow variables are unsigned long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long time = 0;         // the last time the output pin was toggled
unsigned long debounce = 200;   // the debounce time, increase if the output flicker/code]


and some stuff in the void loop, such as an added if and some other stuff im not familiar with


[code]void loop(){
 // read the pushbutton input pin:
  reading = digitalRead(buttonPin);

  // compare the reading to its previous state
  if (reading != previous) {
    // if the state has changed, increment the counter
    if (reading == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      transition();
      setColour();
      delay(delayTime);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      setOff(); 
    }
  }
 
  previous = reading;
}/code]


to


[code]void loop(){
 // read the pushbutton input pin:
  reading = digitalRead(buttonPin);

  // compare the reading to its previous state
  if (reading != previous) {
	      if (reading == HIGH) {
		  state = !state; //toggle state
		  }
	  previous = reading;
  }
    // if the state has changed, increment the counter
    if (state == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      transition();
      setColour();
      delay(delayTime);
    } 
    else {
      // if the current state is LOW then the button
      // went from on to off:
      setOff(); 
    }
 }
/code]

Idioteque:
and other things I'm unfamiliar with

Hopefully @BillHo will come along and explain what he did.

...R