Combining two sketches - "Not declared in this scope"

This is probably me trying to do something a dumb way (new to this), or missing something obvious (same reason)

I have taken code from http://www.electroschematics.com/9522/arduino-control-leds-ir-remote/ to get a working IR remote example, and extended it sucessfully to 6 LEDs with the further addition of an “all off” case and a “toggle case”.

I have also taken the code from Moving Light Display Arduino Project for Beginners and sucessfully extended that to 6 outputs with new patterns too.

The basic requirement is to be able to define different flash patterns, then select them by way of the IR controller.

When I first tried copying the obvious various defines, includes etc into the relevant area sketch 1 and void loop section of sketch 2 and inserting it in one of the case definitions in sketch 1 I get

/
In function ‘void loop()’:
ir_chase:114: error: ‘DisplayPattern’ was not declared in this scope

  • DisplayPattern(led_pattern, sizeof(led_pattern));*
  • ^*
    ir_chase:119: error: a function-definition is not allowed here before ‘{’ token
    {
    ^
    KEYPAD:57: error: expected ‘}’ at end of input
    }
    ^
    KEYPAD:57: error: expected ‘}’ at end of input
    KEYPAD:57: error: expected ‘}’ at end of input
    exit status 1
    ‘DisplayPattern’ was not declared in this scope
    /

Now both sketches work fine independently, but I just can’t get them to combine as I want.

#include <IRremote.h>
#define SPEED_MS  150
#define code1  26775 // code received from button 1
#define code2  39015 // code received from button 2
#define code3  45135 // code received from button 3
#define code4  12495 // code received from button 4
#define code5  6375  // code received from button 5
#define code6  31365 // code received from button 6
#define code7  4335  // code received from button 7
#define code8  14535 // code received from button 8
#define code9  23205 // code received from button 9
#define code0  19125 // code received from button 0
#define codeS  17805 // code received from button *
#define codeH  21165 // code received from button #
#define codeU  25245 // code received from button up
#define codeD  43095 // code received from button dowm
#define codeL  8925  // code received from button left
#define codeR  49725 // code received from button right
#define codeO  765   // code received from button OK

// change LED patterns here
unsigned char led_pattern[] = {
  //TD
  0x20, 0x00, 0x30, 0x00,
  0x10, 0x00, 0x18, 0x00,
  0x08, 0x00, 0x0c, 0x00,
  0x04, 0x00, 0x06, 0x00,
  0x02, 0x00, 0x03, 0x00, 
  0x01, 0x00, 0x01, 0x00,
  0x20, 0x00, 0x30, 0x00,
  0x10, 0x00, 0x18, 0x00,
  0x08, 0x00, 0x0c, 0x00,
  0x04, 0x00, 0x06, 0x00,
  0x02, 0x00, 0x03, 0x00, 
  0x01, 0x00, 0x01, 0x00,
  0x20, 0x00, 0x30, 0x00,
  0x10, 0x00, 0x18, 0x00,
  0x08, 0x00, 0x0c, 0x00,
  0x04, 0x00, 0x06, 0x00,
  0x02, 0x00, 0x03, 0x00, 
  0x01, 0x00, 0x01, 0x00,  
  //inner 2, outer 4
  0x33, 0x00, 0x33, 0x00,
  0x0c, 0x00, 0x0c, 0x00,
  0x33, 0x00, 0x33, 0x00,
  0x0c, 0x00, 0x0c, 0x00,
  //Left Right
  0x07, 0x00, 0x07, 0x00,
  0x38, 0x00, 0x38, 0x00,
  0x07, 0x00, 0x07, 0x00,
  0x38, 0x00, 0x38, 0x00,
  //alt
  0x2a, 0x00, 0x2a, 0x00,
  0x15, 0x00, 0x15, 0x00,
  0x2a, 0x00, 0x2a, 0x00,
  0x15, 0x00, 0x15, 0x00,
  //All
  0x3f, 0x00, 0x3f, 0x00,
  0x3f, 0x00, 0x3f, 0x00,
  0x3f, 0x00, 0x3f, 0x00,
  0x3f, 0x00, 0x3f, 0x00,
  };
int RECV_PIN = 11; // the pin where you connect the output pin of TSOP4838
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int led5 = 6;
int led6 = 7;
int itsONled[] = {0,0,0,0};
/* the initial state of LEDs is OFF (zero) 
the first zero must remain zero but you can 
change the others to 1's if you want a certain
led to light when the board is powered */
 
IRrecv irrecv(RECV_PIN);
 
decode_results results;
 
void setup()
{
  Serial.begin(9600);   // you can comment this line
  irrecv.enableIRIn();  // Start the receiver
 for (int i = 2; i <= 7; i++) {
    pinMode(i, OUTPUT);
  }
}
 
void loop() {

  if (irrecv.decode(&results)) {
    unsigned int value = results.value;
    switch(value) {
       case code1:
         if(itsONled[1] == 1) {        // if first led is on then
            digitalWrite(led1, LOW);   // turn it off when button is pressed
            itsONled[1] = 0;           // and set its state as off
         } else {                      // else if first led is off
             digitalWrite(led1, HIGH); // turn it on when the button is pressed
             itsONled[1] = 1;          // and set its state as on
         }
          break; 
       case code2:
         if(itsONled[2] == 1) {
            digitalWrite(led2, LOW);
            itsONled[2] = 0;
         } else {
             digitalWrite(led2, HIGH);
             itsONled[2] = 1;
         }
          break;
       case code3:
{
  DisplayPattern(led_pattern, sizeof(led_pattern));
  delay(SPEED_MS);
}

void DisplayPattern(unsigned char *pattern, int num_patterns)
{
  static int pattern_num = 0; // keeps count of patterns
  unsigned char mask = 1;     // for testing each bit in pattern

  // do for LEDs on pin 2 to pin 7
  for (int i = 2; i <= 7; i++) {
    // check if bit in pattern is set or not and switch LED accordingly
    if (pattern[pattern_num] & mask) {
      digitalWrite(i, HIGH);
    }
    else {
      digitalWrite(i, LOW);
    }
    mask <<= 1;   // adjust mask for checking next bit in pattern
  }
  pattern_num++;  // move to next pattern for next function call
  // keep pattern within limits of pattern array
  if (pattern_num >= num_patterns) {
    pattern_num = 0;
  }
}

          break;          
    }
    Serial.println(value); // you can comment this line
    irrecv.resume(); // Receive the next value
  }
}

And next, how do I break down my various flash patterns so they can be called by an IR code, rather than as I have so far done copy the loop of each one from the original sketch.

You have pasted the DisplayPattern function inside the loop function. Cut it out and paste it at the very end of the file.

Mind your curly brackets. You have are defining DisplayPattern inside of loop(). Not legal. If you put every { and every } on their own lines and use auotformat, a missing or misplaced { or } is easier to see. In the IDE, if you put the cursor to the right of a {, }, ( or ) the matching (logically) bracket will be highlighted.

wildbill:
You have pasted the DisplayPattern function inside the loop function. Cut it out and paste it at the very end of the file.

Thanks, that helped. It compiles but does not run as I hoped/expected. I shall spend more time on it before crawling back for more help

Hi,
Combining sketches is difficult. HERE is a suggested approach.

terryking228:
Hi,
Combining sketches is difficult.

It is indeed, and it is often the point where many "hack-and-paste" coders get jammed up and have to quit.

What you really want to do is to learn and understand what each sketch does and how it does it and then you can write a code that does both things. It will use some elements from the two examples you're looking at but won't be just a simple stick the two together type of operation.