Hello to all ![]()
Basically I have included the fade and blink with out delay sketches and placed them into classes. Then included a pin change interrupt for a button attached to A5 that when pressed increments thru switch cases.
I would like to update the values of brightness, fadeAmount and interval depending on the values that are set within the currently selected case.
So...
Question 1: Is it possible to update these values from within these switch cases?
After a bit of research I thought that I may be able to use pointers.
I have used pointers in other code with functions successfully but have not been able to include them while using a class.
I am unsure how and where to reference the variables that I would like to update and how/where to pass them into the class or if it's even possible to update them at all.
Question 2: If pointers are the way to do this. Can anyone please provide a working example in this context?
Question 3: If there is a better way to do both these tasks (fade/flash) at the same time and be able to change these variables depending on how many pushes of a button how can I do this and keep my code fairly clean and easy to maintain?
Thank you for your time
Suggestions are greatly appreciated ![]()
Board - Uno
IDE - Arduino 1.6.7
Button pin - A0
Led pins - 12,11,9,8
#define modebutton A5
int mode = 0;
class fade {
 public:
  int led;
  int brightness;
  int fadeAmount;
  fade(int p1, int b, int f)
  {
   led = p1;
   pinMode(led, OUTPUT);
   brightness = b;
   fadeAmount = f;
  }
  void Update()
  {
   analogWrite(led, brightness);
   brightness = brightness + fadeAmount;
   if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
   }
  }
};
fade nine(9, 0, 1); //How do I pass new brightness & fade values/addresses to this?
fade eleven(11, 125, 1);//Is this the right place to send them?
class flash {
 public:
  int ledPin;
  int ledState;
  int interval;
  unsigned long previousMillis;
  flash(int p2, int i)
  {
   ledPin = p2;
   pinMode(ledPin, OUTPUT);
   ledState = LOW;
   interval = i;
   previousMillis = 0;
  }
  void Update()
  {
   unsigned long currentMillis = millis();
   if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (ledState == LOW) {
     ledState = HIGH;
    } else {
     ledState = LOW;
    }
    digitalWrite(ledPin, ledState);
   }
  }
};
flash eight(8, 500); // How do I update this interval -
flash twelve(12, 250); // Without affecting this one?
void setup() {
 Serial.begin(9600);
 pinMode(modebutton, INPUT);
 digitalWrite(modebutton, HIGH);
 Interrupt();
}
void loop() {
 Serial.println(mode);
 switch (mode) {
  case 0:
//----------Update Brightness, Fade Amount and Interval Here----------//
//interval of 8 = 250;
//interval of 9 = 1000;
//brightness of 11 = 100;
//fadeAmount of 11 = 2;
//brightness of 12 = 255;
//fadeAmount of 12 = -1;
  break;
  case 1: break;
  case 2: break;
  case 3: break;
  case 4:
//----------Update Brightness, Fade Amount and Interval Again Here----------//
  break;
  case 5: break;
  case 6: break;
  case 7: break;
  case 8: break;
  case 9:
//-------Update Brightness, Fade Amount and Interval, From Any Case-------//
  break;
 }
 if (mode > 9) {
  mode = 0;
 }
 eight.Update();// I have tried sending addresses/values of variable from here
 nine.Update(); // but have had no luck :(
 eleven.Update();
 twelve.Update();
}
void Interrupt() {
 cli();
 PCICR = 0x02;
 PCMSK1 = B00100000;
 sei();
}
ISR(PCINT1_vect) {
 if (digitalRead(modebutton) == 0) {
  mode++;
 } delay(50);
}
bwod_fade.ino (2.35 KB)