0

I am trying to figure out what I did wrong in my sketch. In my void loop(), at the very bottom of the sketch, my if/else statement keeps compiling with the above error. Originally, it was an if/else if/else statement. I had the current else's an else if and the Update in my else. Someone had corrected me on it and i removed the code and made the proper changes and the error started. Now, just for the sake of my sanity I went ahead and put the ';' at the end of my else, which I know is wrong, just to see if it compiled, which it did. I ran the sketch and sure enough it broke the operation. I cannot figure out what it is looking for. Any help would be appreciated.

    //ASHPD Barrel Version 4.0.2 (adding pulse) Created By Gixxerfool 01.11.2016

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Pattern types supported:
enum  pattern {NONE, CHASE, PULSE};


#define orange 255,128,0
#define blue 0,0,255

char targetColor = '0,0,255';



// NeoPattern Class
class NeoPatterns : public Adafruit_NeoPixel
{
  public:

    // Member Variables:
    pattern  ActivePattern;  // which pattern is running


    unsigned long Interval;   // milliseconds between updates
    unsigned long lastUpdate; // last update of position

    uint32_t Color1;  // What color is used
    uint16_t TotalSteps;  // total number of steps in the pattern
    uint16_t Index;  // current step within the pattern

   // Constructor
    NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type)
    :Adafruit_NeoPixel(pixels, pin, type)

{

}
    // Update the pattern
    void Update() 
    {
      if ((millis() - lastUpdate) > Interval) // time to update
      {
        lastUpdate = millis();
        switch (ActivePattern)
        {
          case CHASE:
            ChaseUpdate();
            break;
           // case PULSE:
            //PulseUpdate();
            //break;
          default:
            break;
        }
      }
    }

    // Increment the Index and reset at the end
    void Increment()
    {
        Index++;
        if (Index >= TotalSteps)
        {
          Index = 0;
        }
      }


    // Calculate 50% dimmed version of a color (used by ChaseUpdate)
    uint32_t DimColor(uint32_t color)
    {
        // Shift R, G and B components one bit to the right
        uint32_t dimColor = Color(Red(color) >> 1, Green(color) >> 1, Blue(color) >> 1);
        return dimColor;
    }
    // Returns the Red component of a 32-bit color
    uint8_t Red(uint32_t color)
    {
        return (color >> 16) & 0xFF;
    }

    // Returns the Green component of a 32-bit color
    uint8_t Green(uint32_t color)
    {
        return (color >> 8) & 0xFF;
    }

    // Returns the Blue component of a 32-bit color
    uint8_t Blue(uint32_t color)
    {
        return color & 0xFF;
    }


    // Initialize for a CHASE

void Chase(uint32_t color1, uint8_t interval)
    {
        ActivePattern = CHASE;
        Interval = interval;
        TotalSteps = (numPixels());
        Color1 = color1;
        Index = 0;
    }

    // Update the Chase Pattern
    void ChaseUpdate()
    { 
        for (int i = 0; i < numPixels() + 1; i++)
        {
            if (i == Index)  // Scan Pixel
            {
                 setPixelColor(i, Color1);
            }
            else // Fading tail
            {
                 setPixelColor(i, DimColor(getPixelColor(i)));
            }
        }
        show();
        Increment();
    }



//Initialize for a pulse
void Pulse(uint32_t color1, uint8_t interval)
    {
      ActivePattern = PULSE;
      Interval = interval;
      TotalSteps = 256;
      Color1 = color1;
      Index = 0;
    }

    //Update the Pulse
    void PulseUpdate()
   {
   for (int h = 0; h < 256; h++)
    {
    for (int i = 0; i < 126; i++)
     {
      for (int j = 0; j > -1; j--)
      {
      if (h == Index)
      {
      setPixelColor(i, Color1);
      }
      else 
      {
      setPixelColor(j, Color1);
      }
    }
    show();
    Increment();
   }
  }
}

};
// Define some NeoPatterns

NeoPatterns Ring3(16, 4, NEO_GRB + NEO_KHZ800);
NeoPatterns Ring2(12, 1, NEO_GRB + NEO_KHZ800);
NeoPatterns Ring1(7, 0, NEO_GRB + NEO_KHZ800);

// Initialize everything and prepare to start

void setup()
{
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code

pinMode(2, INPUT); //Sets pin 2 as an input

  // Initialize all the pixels

  Ring1.begin();
  Ring2.begin();
  Ring3.begin();

  //Set all pixels to off

  Ring1.show();
  Ring2.show();
  Ring3.show();

  ring3Wipe(Ring3.Color(blue), 50); // Wipe ring 3 in blue once
  ring2Wipe(Ring2.Color(blue), 50); // wipe ring 2 in Blue once
  ring1FadeUpBlue(Ring1.Color(blue)); //Fade in ring1 once

  //Start the idle pattern 
  //Name.PatternName(Name.Color(targetColor), Interval) Changing Interval will adjust the speed of the pattern
  Ring1.Pulse(Ring1.Color(blue), 500);
  Ring2.Chase(Ring2.Color(blue), 55);//keep the interval 15ms above ring3 for synchronicity
  Ring3.Chase(Ring3.Color(blue), 40);//keep the interval 15ms below ring2 for synchronicity

}
    void ring1FadeUpBlue(uint8_t wait)
    {
   for(int j = 0; j < 255; j++)
   {
      uint32_t color = Ring1.Color(0, 0, j );  // ramp up the blue only
      for(uint16_t i=0; i < Ring1.numPixels(); i++)
      {
         Ring1.setPixelColor(i, color);
      }
      delay(7);
      Ring1.show();
   }
}
//wipes the ring2 once in blue
void ring2Wipe(uint32_t c, uint8_t wait) 
{
    for(uint16_t i=0; i < Ring2.numPixels(); i++)  //This for loop runs reverse to ring3 loop
   {
      Ring2.setPixelColor(i, c);
      Ring2.show();
      delay(wait);
   }
}


//wipes the ring3 once for the color blue   
void ring3Wipe(uint32_t c, uint8_t wait) 
{ //uint32_t and uint8_t are the colorWipe parameters c=color wait=#of ms between pixels. larger # slows the action
    for(uint16_t i=0; i < Ring3.numPixels(); i++)
   {
      Ring3.setPixelColor(i, c);
      Ring3.show();
      delay(wait);
   } 
}


// Main loop
void loop()
{
  // Update the rings.
  Ring1.Update();
  Ring2.Update();
  Ring3.Update();


  // check for button press:
  if (digitalRead(2) == LOW) // Button is pressed
  {
    Ring3.Color1 = Ring3.Color(orange);//Change the target color to orange
    Ring2.Color1 = Ring2.Color(orange);
    Ring1.Color1 = Ring1.Color(orange);
  }
  else (digitalRead(2) == HIGH)//Kicks the color back to blue after the button is let go
  {
    Ring3.Color1 = Ring3.Color(blue);//Change the target color back to blue
    Ring2.Color1 = Ring2.Color(blue);
    Ring1.Color1 = Ring1.Color(blue); 
  }
}
  • For starters change "else (digitalRead(2) == HIGH)" to "else if (digitalRead(2) == HIGH)" in loop(). – Mikael Patel Jan 19 '16 at 1:01
  • Also take a long look at the definition of targetColor. – Mikael Patel Jan 19 '16 at 1:03
  • Thank you for your response, I am very new to this and am teaching myself on the fly. I went back and eliminated the char altogether. – Gixxerfool Jan 19 '16 at 2:53
1

The if-else construct is incorrect.

It needs to be like this:

if (condition) 
{ 
    //actions
} else {
    //other actions
}

Which reads: if the conditions are true, then do actions, otherwise do other actions.

OR it can be like like this

if (condition) 
{ 
    //actions
} else {
    if (other condition){
    //other actions
   }
}

Which reads: if conditions are true, then do actions, otherwise, if other conditions are true, then do other actions. Note that in this structure, if neither conditions or "other conditions" are true, then we do nothing.

So if DigitalRead can only be HIGH or LOW, then you don't need condition that in the "else" statement. Your code can look like this:

 // check for button press:
  if (digitalRead(2) == LOW) // Button is pressed
  {
    Ring3.Color1 = Ring3.Color(orange);//Change the target color to orange
    Ring2.Color1 = Ring2.Color(orange);
    Ring1.Color1 = Ring1.Color(orange);
  }
  else 
  {
    // digitalRead(2) must be HIGH if it's not LOW
    //Kicks the color back to blue after the button is let go
    Ring3.Color1 = Ring3.Color(blue);//Change the target color back to blue
    Ring2.Color1 = Ring2.Color(blue);
    Ring1.Color1 = Ring1.Color(blue); 
  }

Only if digitalRead could return another value (e.g. 3, 100, 75 etc) then you would need the other conditions in your else clause, but the code would need to look like this:

 // check for button press:
  if (digitalRead(2) == LOW) // Button is pressed
  {
    Ring3.Color1 = Ring3.Color(orange);//Change the target color to orange
    Ring2.Color1 = Ring2.Color(orange);
    Ring1.Color1 = Ring1.Color(orange);
  }
  else 
  {
    if (digitalRead(2) == HIGH)//Kicks the color back to blue after the button is let go
    {
      Ring3.Color1 = Ring3.Color(blue);//Change the target color back to blue
      Ring2.Color1 = Ring2.Color(blue);
      Ring1.Color1 = Ring1.Color(blue); 
    }
  }

Note: you don't actually need the braces around the second "if" statement there, but it makes the structure more clear.

  • 1
    Ok. While I knew the first explanation, or thought I did, I thought I had tried this already. Eliminating the condition fixed it. I must have eliminated the condition and forgot to remove the if after else and got me turned around. Thank you. I can't vote your explanation up, but you made it very clear. as I mentioned to @Mikael Patel I am teaching myself on the fly. – Gixxerfool Jan 19 '16 at 2:55
  • No worries @Gixxerfool - good on you for teaching yourself and having a go - there's certainly no harm in that. – brendan Jan 19 '16 at 3:41
0

This makes no sense - you can't store 7 characters in the space of one character:

char targetColor = '0,0,255';

You're lacking an "if" here:

} else (digitalRead(2) == HIGH) { //Kicks the color back to blue ...

You have numerous sign mismatches in comparisons.

  • Could you expand on what you mean by the sign mismatches? Thank you. – Gixxerfool Jan 19 '16 at 2:57
  • There are places where (especially in for loops) you define an int (signed) and then compare that to the return value of a function which is an unsigned value (eg numPixels()). That causes warnings if you have warnings turned on. 99.99999% of the time it is fine, but it's just something to be aware of because it can cause problems in some cases. Highly unlikely in your case, but still something to watch. – Majenko Jan 19 '16 at 11:43
  • Ok. That makes perfect sense. I guess since I never had a conflict, I never paid attention. Definitely something I will be more conscious in the future of that. Thank you for the tip. Any help is always appreciated. – Gixxerfool Jan 19 '16 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.