element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Help with joining two pieces of code.
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 45 replies
  • Subscribers 416 subscribers
  • Views 4126 views
  • Users 0 members are here
  • led
  • rgb
Related

Help with joining two pieces of code.

e14 Contributor
e14 Contributor over 12 years ago

Hey Arduino Family!

 

I got y first arduino this week and i havent put it down since it arrived, I'm totally hooked.

I'm really a complete novice with coding but I am slowly starting to take it on board and can somewhat understand what the lines of code are doing (even thought I never thought that I would! lol)

 

So I am making this kind ambient lamp from RGB leds

 

I have these two pieces of code that work just fine on their own.

One cycles through colours nicely in a rainbow kinda effect and the other allows the colours to be set using 3 pots that are controlling the PWM signal.

 

What I would like to do is connect a momentary push button switch that takes it out of the rainbow cycle code and places it into 'manual' mode handing over control to the user.

I was thinking that this would perhaps look something like this:

The main loop for the rainbow effect is running, at the top of the loop check to see if the button is pressed and if it is then exit the loop and continue down the program, at which point it would hit the next loop which is the manual control section, perhaps in this loop it could look for the button being pressed again and switch back to the first loop putting it back into rainbow mode again'

 

The two sketches I have been using are pasted below.

any help that will start pointing me in the right direction would be really appreciated. thanks guys. image

 

RGB Rainbow

const int redPin = 11;

const int greenPin = 10;

const int bluePin = 9;

 

void setup() {

  // Start off with the LED off.

  setColourRgb(0,0,0);

}

 

void loop() {

  unsigned int rgbColour[3];

 

  // Start off with red.

  rgbColour[0] = 255;

  rgbColour[1] = 0;

  rgbColour[2] = 0;

 

  // Choose the colours to increment and decrement.

  for (int decColour = 0; decColour < 3; decColour += 1) {

    int incColour = decColour == 2 ? 0 : decColour + 1;

 

    // cross-fade the two colours.

    for(int i = 0; i < 255; i += 1) {

      rgbColour[decColour] -= 1;

      rgbColour[incColour] += 1;

    

      setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);

      delay(5);

    }

  }

}

 

void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {

  analogWrite(redPin, red);

  analogWrite(greenPin, green);

  analogWrite(bluePin, blue);

}

 

 

 

=================================================

 

RGB Color Chooser

 

// Init the Pins used for PWM

const int redPin = 9;

const int greenPin = 10;

const int bluePin = 11;

 

// Init the Pins used for 10K pots

const int redPotPin = 0;

const int greenPotPin = 1;

const int bluePotPin = 2;

 

// Init our Vars

int currentColorValueRed;

int currentColorValueGreen;

int currentColorValueBlue;

 

void setup()

{

  pinMode(redPin, OUTPUT);

  pinMode(greenPin, OUTPUT);

  pinMode(bluePin, OUTPUT);

}

 

void loop()

{

// Read the voltage on each analog pin then scale down to 0-255 and inverting the value for common anode

  currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) );

  currentColorValueBlue = (255 - map( analogRead(bluePotPin), 0, 1024, 0, 255 ) );

  currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );

 

// Write the color to each pin using PWM and the value gathered above

  analogWrite(redPin, currentColorValueRed);

  analogWrite(bluePin, currentColorValueBlue);

  analogWrite(greenPin, currentColorValueGreen);

 

}

  • Sign in to reply
  • Cancel
Parents
  • e14 Contributor
    0 e14 Contributor over 12 years ago

    Thanks Paul & Chris,

    Ok so I fused both your suggestions together but now it seems there are syntax errors. I tried moving curly braces and parenthesis around but didnt seem to have much luck.

    here's what I have right now, how is it looking to you?

     

    //===============================================================

    // Global Variables & Constants

    //===============================================================

     

    //Init the Pins used for PWM 

    const int redPin = 9; // LED1 ANODE

    const int greenPin = 10; // LED2 ANODE

    const int bluePin = 11; // LED3 ANODE

     

     

    // Init the Pins used for 10K pots

    const int redPotPin = 0;

    const int greenPotPin = 1;

    const int bluePotPin = 2;

     

     

    // Init our Vars

    int currentColorValueRed;

    int currentColorValueGreen;

    int currentColorValueBlue;

     

     

    //Init the Switch

    const int modePin = 13; // Active HIGH, held low by 4.7K

     

    int mode = 0; // Selector State (Initial state = ALL OFF)

    int val = 0; // Pin 7 HIGH/LOW Status

    int butState = 0; // Last Button State

    int modeState = 0; // Last Mode State

    boolean debug = 1; // 1 = Print Serial Enabled / 0 = disabled

     

    //===============================================================

    // SETUP

    //===============================================================

    void setup () {

    pinMode(redPin, OUTPUT);

    pinMode(greenPin, OUTPUT);

    pinMode(bluePin, OUTPUT);

     

     

    pinMode(modePin, INPUT);

      // Start off with the LED off.

      setColourRgb(0,0,0);

     

     

    if (debug){

    Serial.begin(9600);

    Serial.print("Initial Mode: ");

    Serial.println(mode);

    Serial.print("Setup Complete\n");

    }

    }

    void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {

      analogWrite(redPin, red);

      analogWrite(greenPin, green);

      analogWrite(bluePin, blue);

    }

     

     

     

     

    void loop(){

     

      If (digitalRead( modePin, HIGH) { 

         mode != mode // toggles mode on push 

       }

       

       If (mode==1) { 

         // code from loop in first example 

          // Read the voltage on each analog pin then scale down to 0-255 and inverting the value for common anode

      currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) );

      currentColorValueBlue = (255 - map( analogRead(bluePotPin), 0, 1024, 0, 255 ) );

      currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );

     

    // Write the color to each pin using PWM and the value gathered above

      analogWrite(redPin, currentColorValueRed);

      analogWrite(bluePin, currentColorValueBlue);

      analogWrite(greenPin, currentColorValueGreen);

          

       }

     

       else { 

         // code from loop in second example 

    unsigned int rgbColour[3];

     

     

      // Start off with red.

      rgbColour[0] = 255;

      rgbColour[1] = 0;

      rgbColour[2] = 0;

     

      // Choose the colours to increment and decrement.

      for (int decColour = 0; decColour < 3; decColour += 1) {

        int incColour = decColour == 2 ? 0 : decColour + 1;

     

        // cross-fade the two colours.

        for(int i = 0; i < 255; i += 1) {

          rgbColour[decColour] -= 1;

          rgbColour[incColour] += 1;

       

          setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);

          delay(5);

        }

      }

    }

        

      

    } 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to e14 Contributor

    SimpleOneButton.ino: In function 'void loop()':

    SimpleOneButton:59: error: 'If' was not declared in this scope

    SimpleOneButton:59: error: expected `;' before 'digitalRead'

    SimpleOneButton:63: error: expected `;' before '{' token

    SimpleOneButton:77: error: 'else' without a previous 'if'

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gadget.iom
    0 gadget.iom over 12 years ago in reply to e14 Contributor

    Code is added in the correct format by clicking the >> button on the toolbar.

    WWhen you get an error referring to an expected ';' have a look at the line above, more often than not it will be missing its closing semi-colon.

     

    FOr example

    mode != mode

    should be

    mode != mode;

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to gadget.iom

    Guess I'm getting there.. lol

    Added a semicolon and made sure they were there where they should be (I think)

     

    more errors now. haha

    impleOneButton.ino: In function 'void loop()':

    SimpleOneButton:48: error: 'If' was not declared in this scope

    SimpleOneButton:48: error: expected `;' before 'digitalRead'

    SimpleOneButton:53: error: expected `;' before '{' token

    SimpleOneButton:66: error: 'else' without a previous 'if'

     

     

    //===============================================================
    // Global Variables & Constants
    //===============================================================
    
    //Init the Pins used for PWM  
    const int redPin = 9; // LED1 ANODE
    const int greenPin = 10; // LED2 ANODE
    const int bluePin = 11; // LED3 ANODE
    
    
    // Init the Pins used for 10K pots
    const int redPotPin = 0;
    const int greenPotPin = 1;
    const int bluePotPin = 2;
    
    
    // Init our Vars
    int currentColorValueRed;
    int currentColorValueGreen;
    int currentColorValueBlue;
    
    
    //Init the Switch 
    const int modePin = 13; // Active HIGH, held low by 4.7K
    
    int mode = 0; // Selector State (Initial state = ALL OFF)
    int val = 0; // Pin 7 HIGH/LOW Status
    int butState = 0; // Last Button State
    int modeState = 0; // Last Mode State
    boolean debug = 1; // 1 = Print Serial Enabled / 0 = disabled
    
    //===============================================================
    // SETUP
    //===============================================================
    
    
    void setup () {
     pinMode(redPin, OUTPUT);
     pinMode(greenPin, OUTPUT);
     pinMode(bluePin, OUTPUT);
     pinMode(modePin, INPUT);
     setColourRgb(0,0,0);
     }
    
    
     void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
      analogWrite(redPin, red);
      analogWrite(greenPin, green);
      analogWrite(bluePin, blue);
    }
    
    void loop(){
    If digitalRead( modePin, HIGH) {  
      // toggles mode on push   
      mode != mode;   
       } 
        
       If (mode==1) {  
     // Here we hve chosen our first loop AKA 'Manual Control'  
     // Read the voltage on each analog pin then scale down to 0-255 and inverting the value for common anode
      currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) );
      currentColorValueBlue = (255 - map( analogRead(bluePotPin), 0, 1024, 0, 255 ) );
      currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );
     // Write the color to each pin using PWM and the value gathered above
      analogWrite(redPin, currentColorValueRed);
      analogWrite(bluePin, currentColorValueBlue);
      analogWrite(greenPin, currentColorValueGreen);
           
       }
      
       else {  
    // Here we have chosen out 'Rainbow Cycle'  
          unsigned int rgbColour[3];
    // Start off with red.
      rgbColour[0] = 255;
      rgbColour[1] = 0;
      rgbColour[2] = 0;
    // Choose the colours to increment and decrement.
      for (int decColour = 0; decColour < 3; decColour += 1) {
        int incColour = decColour == 2 ? 0 : decColour + 1;
    // cross-fade the two colours.
        for(int i = 0; i < 255; i += 1) {
          rgbColour[decColour] -= 1;
          rgbColour[incColour] += 1;
          setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
          delay(5);
        }
      }
    }
         
       
    } 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to gadget.iom

    Guess I'm getting there.. lol

    Added a semicolon and made sure they were there where they should be (I think)

     

    more errors now. haha

    impleOneButton.ino: In function 'void loop()':

    SimpleOneButton:48: error: 'If' was not declared in this scope

    SimpleOneButton:48: error: expected `;' before 'digitalRead'

    SimpleOneButton:53: error: expected `;' before '{' token

    SimpleOneButton:66: error: 'else' without a previous 'if'

     

     

    //===============================================================
    // Global Variables & Constants
    //===============================================================
    
    //Init the Pins used for PWM  
    const int redPin = 9; // LED1 ANODE
    const int greenPin = 10; // LED2 ANODE
    const int bluePin = 11; // LED3 ANODE
    
    
    // Init the Pins used for 10K pots
    const int redPotPin = 0;
    const int greenPotPin = 1;
    const int bluePotPin = 2;
    
    
    // Init our Vars
    int currentColorValueRed;
    int currentColorValueGreen;
    int currentColorValueBlue;
    
    
    //Init the Switch 
    const int modePin = 13; // Active HIGH, held low by 4.7K
    
    int mode = 0; // Selector State (Initial state = ALL OFF)
    int val = 0; // Pin 7 HIGH/LOW Status
    int butState = 0; // Last Button State
    int modeState = 0; // Last Mode State
    boolean debug = 1; // 1 = Print Serial Enabled / 0 = disabled
    
    //===============================================================
    // SETUP
    //===============================================================
    
    
    void setup () {
     pinMode(redPin, OUTPUT);
     pinMode(greenPin, OUTPUT);
     pinMode(bluePin, OUTPUT);
     pinMode(modePin, INPUT);
     setColourRgb(0,0,0);
     }
    
    
     void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
      analogWrite(redPin, red);
      analogWrite(greenPin, green);
      analogWrite(bluePin, blue);
    }
    
    void loop(){
    If digitalRead( modePin, HIGH) {  
      // toggles mode on push   
      mode != mode;   
       } 
        
       If (mode==1) {  
     // Here we hve chosen our first loop AKA 'Manual Control'  
     // Read the voltage on each analog pin then scale down to 0-255 and inverting the value for common anode
      currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) );
      currentColorValueBlue = (255 - map( analogRead(bluePotPin), 0, 1024, 0, 255 ) );
      currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );
     // Write the color to each pin using PWM and the value gathered above
      analogWrite(redPin, currentColorValueRed);
      analogWrite(bluePin, currentColorValueBlue);
      analogWrite(greenPin, currentColorValueGreen);
           
       }
      
       else {  
    // Here we have chosen out 'Rainbow Cycle'  
          unsigned int rgbColour[3];
    // Start off with red.
      rgbColour[0] = 255;
      rgbColour[1] = 0;
      rgbColour[2] = 0;
    // Choose the colours to increment and decrement.
      for (int decColour = 0; decColour < 3; decColour += 1) {
        int incColour = decColour == 2 ? 0 : decColour + 1;
    // cross-fade the two colours.
        for(int i = 0; i < 255; i += 1) {
          rgbColour[decColour] -= 1;
          rgbColour[incColour] += 1;
          setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
          delay(5);
        }
      }
    }
         
       
    } 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • gadget.iom
    0 gadget.iom over 12 years ago in reply to e14 Contributor

    Ok... A couple of small things, Your 'if' statements need to be in lowercase. You should find that if you replace the capital 'I' with a lowercase one, the syntax colouring will turn orange (a good sign).

    Your if conditions should also be wrapped in a pair of brackets, e.g:

    if (x==y) {

     

    also you need to change your digitalread command to this format:

    digitalRead(inPin)

    You don't need to specify wether the pin is HIGH or LOW because you are reading the value not setting it.


    Hopefully this will help.


    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to gadget.iom

    Thanks so much for the tip on capital letters Paul, I see that there were a few of those and I corrected them.

    The code now compiles! yay!

     

    however, on boot it just goes straight into the rainbow loop and stays there, pressing the switch doesnt change anything..

     

    Here's how it looks now...

    //===============================================================
    // Global Variables & Constants
    //===============================================================
    
    //Init the Pins used for PWM  
    const int redPin = 9; // LED1 ANODE
    const int greenPin = 10; // LED2 ANODE
    const int bluePin = 11; // LED3 ANODE
    
    
    // Init the Pins used for 10K pots
    const int redPotPin = 0;
    const int greenPotPin = 1;
    const int bluePotPin = 2;
    
    
    // Init our Vars
    int currentColorValueRed;
    int currentColorValueGreen;
    int currentColorValueBlue;
    
    
    //Init the Switch 
    const int modePin = 13; // Active HIGH, held low by 4.7K
     int mode = 0; // Selector State (Initial state = ALL OFF)
    
    //===============================================================
    // SETUP
    //===============================================================
    
    
    void setup () {
     pinMode(redPin, OUTPUT);
     pinMode(greenPin, OUTPUT);
     pinMode(bluePin, OUTPUT);
     pinMode(modePin, INPUT);
     setColourRgb(0,0,0);
     }
    
    
     void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
      analogWrite(redPin, red);
      analogWrite(greenPin, green);
      analogWrite(bluePin, blue);
    }
    
    void loop(){
    if (digitalRead(modePin)) {  
      // toggles mode on push   
      mode != mode;   
       } 
        
       if (mode==1) {  
     // Here we hve chosen our first loop AKA 'Manual Control'  
     // Read the voltage on each analog pin then scale down to 0-255 and inverting the value for common anode
      currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) );
      currentColorValueBlue = (255 - map( analogRead(bluePotPin), 0, 1024, 0, 255 ) );
      currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );
     // Write the color to each pin using PWM and the value gathered above
      analogWrite(redPin, currentColorValueRed);
      analogWrite(bluePin, currentColorValueBlue);
      analogWrite(greenPin, currentColorValueGreen);
           
       }
      
       else {  
    // Here we have chosen out 'Rainbow Cycle'  
       unsigned int rgbColour[3];
    // Start off with red.
      rgbColour[0] = 255;
      rgbColour[1] = 0;
      rgbColour[2] = 0;
    // Choose the colours to increment and decrement.
      for (int decColour = 0; decColour < 3; decColour += 1) {
        int incColour = decColour == 2 ? 0 : decColour + 1;
    // cross-fade the two colours.
        for(int i = 0; i < 255; i += 1) {
          rgbColour[decColour] -= 1;
          rgbColour[incColour] += 1;
          setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
          delay(5);
        }
      }
    }
         
       
    } 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gadget.iom
    0 gadget.iom over 12 years ago in reply to e14 Contributor

    Try adding a Serial.println("Pressed"); I underneath the mode != mode; line.

     

    This will enable you to see wether the button press is recognised in the serial monitor.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to gadget.iom

    AH! ok.

    After doing this, it thinks the button has been pressed. it outputs 'pressed' in the monitor on its own seemingly at the end of the rainbow loop before it starts again.

     

    So it looks like the if statement isnt being read as an if statement?

    Or it just thinks its permanantly pressed?

    I changed the input pin to number 8 (digital pin) to see if it made any difference, it did not.

    Totally stumped. lol

     

    //===============================================================
    // Global Variables & Constants
    //===============================================================
    
    //Init the Pins used for PWM  
    const int redPin = 9; // LED1 ANODE
    const int greenPin = 10; // LED2 ANODE
    const int bluePin = 11; // LED3 ANODE
    
    
    // Init the Pins used for 10K pots
    const int redPotPin = 0;
    const int greenPotPin = 1;
    const int bluePotPin = 2;
    
    
    // Init our Vars
    int currentColorValueRed;
    int currentColorValueGreen;
    int currentColorValueBlue;
    
    
    //Init the Switch 
    const int modePin = 8; // Active HIGH, held low by 4.7K
    int mode = 0; // Selector State (Initial state = ALL OFF)
    
    //===============================================================
    // SETUP
    //===============================================================
    
    
    void setup () {
     pinMode(redPin, OUTPUT);
     pinMode(greenPin, OUTPUT);
     pinMode(bluePin, OUTPUT);
     pinMode(modePin, INPUT);
     setColourRgb(0,0,0);
     Serial.begin(9600);
     }
    
    
     void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
      analogWrite(redPin, red);
      analogWrite(greenPin, green);
      analogWrite(bluePin, blue);
    }
    
    void loop(){
    if (digitalRead(modePin)) {  
      // toggles mode on push   
      mode != mode;  
     Serial.println("Pressed");  
       } 
        
       if (mode==1) {  
     // Here we hve chosen our first loop AKA 'Manual Control'  
     // Read the voltage on each analog pin then scale down to 0-255 and inverting the value for common anode
      currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) );
      currentColorValueBlue = (255 - map( analogRead(bluePotPin), 0, 1024, 0, 255 ) );
      currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );
     // Write the color to each pin using PWM and the value gathered above
      analogWrite(redPin, currentColorValueRed);
      analogWrite(bluePin, currentColorValueBlue);
      analogWrite(greenPin, currentColorValueGreen);
           
       }
      
       else {  
    // Here we have chosen out 'Rainbow Cycle'  
       unsigned int rgbColour[3];
    // Start off with red.
      rgbColour[0] = 255;
      rgbColour[1] = 0;
      rgbColour[2] = 0;
    // Choose the colours to increment and decrement.
      for (int decColour = 0; decColour < 3; decColour += 1) {
        int incColour = decColour == 2 ? 0 : decColour + 1;
    // cross-fade the two colours.
        for(int i = 0; i < 255; i += 1) {
          rgbColour[decColour] -= 1;
          rgbColour[incColour] += 1;
          setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
          delay(5);
        }
      }
    }
         
       
    } 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gadget.iom
    0 gadget.iom over 12 years ago in reply to e14 Contributor

    WWhat is your input wiring like?

     

    in the meantime change the code so that the if statement is (digitalread(modepin)==HIGH)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to gadget.iom

    I checked all wiring TWICE and everything is as it should be both sketches run fine on their own.

    i checked the switch on a multimeter for continuity when pressed and all good.

    here is the current code I am running in its entirety, maybe something will spring out?

    //===============================================================
    // Global Variables & Constants
    //===============================================================
    
    
    //Init the Pins used for PWM  
    const int redPin = 9; // LED1 ANODE
    const int greenPin = 10; // LED2 ANODE
    const int bluePin = 11; // LED3 ANODE
    
    
    
    
    // Init the Pins used for 10K pots
    const int redPotPin = 0;
    const int greenPotPin = 1;
    const int bluePotPin = 2;
    
    
    
    
    // Init our Vars
    int currentColorValueRed;
    int currentColorValueGreen;
    int currentColorValueBlue;
    
    
    
    
    
    
    //Init the Switch
    const int modePin = 13; // Active HIGH, held low by 4.7K
    int mode = 0; // Selector State (Initial state = ALL OFF)
    int val = 0; // Pin 7 HIGH/LOW Status
    int butState = 0; // Last Button State
    int modeState = 0; // Last Mode State
    boolean debug = 1; // 1 = Print Serial Enabled / 0 = disabled
    
    
    //===============================================================
    // SETUP
    //===============================================================
    
    
    
    
    void setup () {
     pinMode(redPin, OUTPUT);
     pinMode(greenPin, OUTPUT);
     pinMode(bluePin, OUTPUT);
     pinMode(modePin, INPUT);
     setColourRgb(0,0,0);
     Serial.begin(9600);
     }
    
    
    
    
     void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
      analogWrite(redPin, red);
      analogWrite(greenPin, green);
      analogWrite(bluePin, blue);
    }
    
    
    void loop(){
    if (digitalRead(modePin)==HIGH) {  
      // toggles mode on push   
      mode != mode;  
     Serial.println("Pressed");  
       } 
        
       if (mode==1) {  
     // Here we hve chosen our first loop AKA 'Manual Control'  
     // Read the voltage on each analog pin then scale down to 0-255 and inverting the value for common anode
      currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) );
      currentColorValueBlue = (255 - map( analogRead(bluePotPin), 0, 1024, 0, 255 ) );
      currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );
     // Write the color to each pin using PWM and the value gathered above
      analogWrite(redPin, currentColorValueRed);
      analogWrite(bluePin, currentColorValueBlue);
      analogWrite(greenPin, currentColorValueGreen);
           
       }
      
       else {  
    // Here we have chosen out 'Rainbow Cycle'  
       unsigned int rgbColour[3];
    // Start off with red.
      rgbColour[0] = 255;
      rgbColour[1] = 0;
      rgbColour[2] = 0;
    // Choose the colours to increment and decrement.
      for (int decColour = 0; decColour < 3; decColour += 1) {
        int incColour = decColour == 2 ? 0 : decColour + 1;
    // cross-fade the two colours.
        for(int i = 0; i < 255; i += 1) {
          rgbColour[decColour] -= 1;
          rgbColour[incColour] += 1;
          setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
          delay(5);
        }
      }
    }
         
       
    } 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gadget.iom
    0 gadget.iom over 12 years ago in reply to e14 Contributor

    Well the code 'seems' fine, If you are getting a constant demand then I would hint at wiring. Take a look at the following page and see if you can replicate this onto pin 13.

     

    http://arduino.cc/en/tutorial/button

     

    Paul

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to gadget.iom

    My bad, you were quite right, I had the switch wired between 3.3v and the input pin. I thought that was sufficient to take it high.

    The switch is now wired the same as the example you kindly pointed me to.

     

    Ok so now,at boot it enters 'rainbow' mode. if i hold my finger on the switch (i have to do this for a certain period of time, i presume until its finished the colour cycle and looks at the button) i get teh little green tx light on the board and the monitor reads 'pressed'

    however, no change to its behaviour. i.e it doesnt enter 'manual' mode

     

    here's the current code...

    //===============================================================
    // Global Variables & Constants
    //===============================================================
    
    
    //Init the Pins used for PWM  
    const int redPin = 9; // LED1 ANODE
    const int greenPin = 10; // LED2 ANODE
    const int bluePin = 11; // LED3 ANODE
    
    
    
    
    // Init the Pins used for 10K pots
    const int redPotPin = 0;
    const int greenPotPin = 1;
    const int bluePotPin = 2;
    
    
    
    
    // Init our Vars
    int currentColorValueRed;
    int currentColorValueGreen;
    int currentColorValueBlue;
    
    
    
    
    
    
    //Init the Switch
    const int modePin = 13; // Active HIGH, held low by 4.7K
    int mode = 0; // Selector State (Initial state = ALL OFF)
    int val = 0; // Pin 7 HIGH/LOW Status
    int butState = 0; // Last Button State
    int modeState = 0; // Last Mode State
    boolean debug = 1; // 1 = Print Serial Enabled / 0 = disabled
    
    
    //===============================================================
    // SETUP
    //===============================================================
    
    
    
    
    void setup () {
     pinMode(redPin, OUTPUT);
     pinMode(greenPin, OUTPUT);
     pinMode(bluePin, OUTPUT);
     pinMode(modePin, INPUT);
     setColourRgb(0,0,0);
     Serial.begin(9600);
     }
    
    
    
    
     void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
      analogWrite(redPin, red);
      analogWrite(greenPin, green);
      analogWrite(bluePin, blue);
    }
    
    
    void loop(){
    if (digitalRead(modePin)==HIGH) {  
      // toggles mode on push   
     mode != mode;  
    
     Serial.println("Pressed");  
       } 
        
       if (mode==1) {  
     // Here we hve chosen our first loop AKA 'Manual Control'  
     // Read the voltage on each analog pin then scale down to 0-255 and inverting the value for common anode
      currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) );
      currentColorValueBlue = (255 - map( analogRead(bluePotPin), 0, 1024, 0, 255 ) );
      currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );
     // Write the color to each pin using PWM and the value gathered above
      analogWrite(redPin, currentColorValueRed);
      analogWrite(bluePin, currentColorValueBlue);
      analogWrite(greenPin, currentColorValueGreen);
           
       }
      
       else {  
    // Here we have chosen out 'Rainbow Cycle'  
       unsigned int rgbColour[3];
    // Start off with red.
      rgbColour[0] = 255;
      rgbColour[1] = 0;
      rgbColour[2] = 0;
    // Choose the colours to increment and decrement.
      for (int decColour = 0; decColour < 3; decColour += 1) {
        int incColour = decColour == 2 ? 0 : decColour + 1;
    // cross-fade the two colours.
        for(int i = 0; i < 255; i += 1) {
          rgbColour[decColour] -= 1;
          rgbColour[incColour] += 1;
          setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
          delay(5);
        }
      }
    }
         
       
    } 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gadget.iom
    0 gadget.iom over 12 years ago in reply to e14 Contributor

    ok. nice to see progress image

     

    try changing

    Serial.println("Pressed");

    to

    Serial.println(mode);

     

    This will output the valuer of the mode to the serial monitor. You may observe it printing 1 and then 0 in quick succession before starting another colour cycle. If this is the case, then we will need to create some kind of debounce code.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to gadget.iom

    Hi Paul!

    thanks for all your help on this.

    So looking at the monitor when I hold the button down long enough for it to finish the loop and come back round to looking at it again it registers the press and outputs '0' only ever zero.

    So there must be a problem with the line 

     

    mode != mode;

     

    no? something need to make it add '1' to the mode variable until the button is pressed again to toggle it back to '0' ??

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube