Arduino Script problem, help apreciated.

Hello there,

Iv'e only just discovered the world of Arduino and am currently trying to create a script for a moodlight.

The basic Idea for it was that, with the click of a button i'd would either turn on or off depending on its current state.
It would also have a Fade function wich makes it brighter or less bright depending on its current state.
Also by twisting the sensor Pin it will change Colour (RGB)

I am, however kind of stuck and could really use someone to help me with the finish(?) touch.

What it does so far;
Change Brightness. (i believe it's called a byte-dim Curve?)
Change Colour // when adjusting sensorPin (potmeter)
Im pretty happy with this so far, but I'd like to get a function for my Button aswel, prev. On. Off. (on off on off etc.)

Here is the Script; Note that I havent made this all by myself, heck..I couldn't if i wanted to..

const byte dim_curve[] = {
    0,   1,   1,   2,   2,   2,   2,   2,   2,   3,   3,   3,   3,   3,   3,   3,
    3,   3,   3,   3,   3,   3,   3,   4,   4,   4,   4,   4,   4,   4,   4,   4,
    4,   4,   4,   5,   5,   5,   5,   5,   5,   5,   5,   5,   5,   6,   6,   6,
    6,   6,   6,   6,   6,   7,   7,   7,   7,   7,   7,   7,   8,   8,   8,   8,
    8,   8,   9,   9,   9,   9,   9,   9,   10,  10,  10,  10,  10,  11,  11,  11,
    11,  11,  12,  12,  12,  12,  12,  13,  13,  13,  13,  14,  14,  14,  14,  15,
    15,  15,  16,  16,  16,  16,  17,  17,  17,  18,  18,  18,  19,  19,  19,  20,
    20,  20,  21,  21,  22,  22,  22,  23,  23,  24,  24,  25,  25,  25,  26,  26,
    27,  27,  28,  28,  29,  29,  30,  30,  31,  32,  32,  33,  33,  34,  35,  35,
    36,  36,  37,  38,  38,  39,  40,  40,  41,  42,  43,  43,  44,  45,  46,  47,
    48,  48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,
    63,  64,  65,  66,  68,  69,  70,  71,  73,  74,  75,  76,  78,  79,  81,  82,
    83,  85,  86,  88,  90,  91,  93,  94,  96,  98,  99,  101, 103, 105, 107, 109,
    110, 112, 114, 116, 118, 121, 123, 125, 127, 129, 132, 134, 136, 139, 141, 144,
    146, 149, 151, 154, 157, 159, 162, 165, 168, 171, 174, 177, 180, 183, 186, 190,
    193, 196, 200, 203, 207, 211, 214, 218, 222, 226, 230, 234, 238, 242, 248, 255,
};

//STATE
const int buttonPin  = 8;     // the pin that the pushbutton is attached to
const int ledPin     = 13;    // the pin that the LED is attached to

int buttonState      = 0;     // current state of the button
int lastButtonState  = 0;     // previous state of the button
int ledState         = 0;// remember current led state
int ledStateR        = 9;
int ledStateG        = 10;
int ledStateB        = 11;



//RGB
const int sensorPin = 0;  // pin the potmeter is attached too
const int ledPinR   = 9;  // pwm pin with red led
const int ledPinG   = 10; // pwm pin with green led
const int ledPinB   = 11; // pwm pin with blue led

int sensorVal = 0; // store the value coming from the sensor
int fadeVal   = 0; // value that changes between 0-255
int fadeSpeed = 2; // 'speed' of fading

// getRGB function stores RGB values in this array
// use these values for the red, blue, green led. 
int rgb_colors[3]; 

int hue;
int saturation;
int brightness;

void setup() { 
  
  
  //STATE
  pinMode(buttonPin, INPUT);  // initialize the button pin as a input
  pinMode(ledPin, OUTPUT);    // initialize the button pin as a output
  
  
  //RGB
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  Serial.begin(57600);
}

void loop()  { 
    
  //RGB
  
  sensorVal = analogRead(sensorPin);
  
  // fade from 0 - 255 and back with a certain speed   
  fadeVal = fadeVal + fadeSpeed;         // change fadeVal by speed
  fadeVal = constrain(fadeVal, 0, 255);  // keep fadeVal between 0 and 255
  
  if(fadeVal==255 || fadeVal==0)         // change from up>down or down-up (negative/positive)
  { fadeSpeed = -fadeSpeed;  
  }  
  
  // set HSB values
  hue        = map(sensorVal,0, 1023,0, 359);     // hue is a number between 0 and 360
  saturation = 255;                               // saturation is a number between 0 - 255
  brightness = fadeVal;                           // value is a number between 0 - 255
    
  getRGB(hue,saturation,brightness,rgb_colors);   // converts HSB to RGB
      
  analogWrite(ledPinR, rgb_colors[0]);            // red value in index 0 of rgb_colors array
  analogWrite(ledPinG, rgb_colors[1]);            // green value in index 1 of rgb_colors array
  analogWrite(ledPinB, rgb_colors[2]);            // blue value in index 2 of rgb_colors array
  
  delay(20); // delay to slow down fading
}

void getRGB(int hue, int sat, int val, int colors[3]) { 
  /* convert hue, saturation and brightness ( HSB/HSV ) to RGB
     The dim_curve is used only on brightness/value and on saturation (inverted).
     This looks the most natural.      
  */
  
  val = dim_curve[val];
  sat = 255-dim_curve[255-sat];
  
  int r;
  int g;
  int b;
  int base;
  
  if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
    colors[0]=val;
    colors[1]=val;
    colors[2]=val;  
  } else  { 
    
    base = ((255 - sat) * val)>>8;
  
    switch(hue/60) {
      case 0:
            r = val;
            g = (((val-base)*hue)/60)+base;
            b = base;
      break;
      
      case 1:
            r = (((val-base)*(60-(hue%60)))/60)+base;
            g = val;
            b = base;
      break;
      
      case 2:
            r = base;
            g = val;
            b = (((val-base)*(hue%60))/60)+base;
      break;
      
      case 3:
            r = base;
            g = (((val-base)*(60-(hue%60)))/60)+base;
            b = val;
      break;
      
      case 4:
            r = (((val-base)*(hue%60))/60)+base;
            g = base;
            b = val;
      break;
      
      case 5:
            r = val;
            g = base;
            b = (((val-base)*(60-(hue%60)))/60)+base;
      break;
    }
      
    colors[0]=r;
    colors[1]=g;
    colors[2]=b; 
  }   
  

   //STATE
   // read the pushbutton input pin
  buttonState = digitalRead(buttonPin);

  // check if the button is pressed or released
  // by comparing the buttonState to its previous state 
  if (buttonState != lastButtonState) {
    
    // change the state of the led when someone pressed the button
    if (buttonState == 1) { 
      if(ledState==1) ledState=0  ;      
      else            ledState=1  ; 
               
    }
    
    // remember the current state of the button
    lastButtonState = buttonState;
  }
  
  // turns LED on if the ledState=1 or off if the ledState=0
  digitalWrite(ledPin, ledState);
  
  // adding a small delay prevents reading the buttonState to fast
  // ( debouncing )
  delay(20);
  
  

}

If anyone could just see what needs to be changed in order for my Button to actually have a function, or simply send me in the right direction that would be great!

(I'm not sure if ive left enough information for anyone to answer anything at all, if not i'm willing to dig up what I can find..just started with this, sorry!)

Thanks in Advance -R

I'm new here, but I think the general consensus is that you should place codes in a text box using the # (enter code) button. Otherwise it adds a lot of unnecessary length to your thread.

Ah yes, I do apologise for that, wont happen again :slight_smile:

You are reading the button state in the getRGB function, not in the loop function.

You should put everything that you now have in the loop function into a function called fade.

You should move the button read stuff from getRGB to loop.

You should either toggle the led on or off, or call the fade function, or both, as you see fit.

I’ll be honest, and i do apologise, but when i read all this im still rather confused I havent actually figured out how to create a Function, and i tought fade was already implemented in this script :frowning: ?

You are reading the button state in the getRGB function, not in the loop function.

isnt everything Under Loop as it is? somehow i think simply placing the button state function above the RGB function wont do the trick…again im sorry. At this point it’s still a bit hard to understand for me.

You should put everything that you now have in the loop function into a function called fade.

Any pointers on how I go about making a Fade function?
Is it anything like Void fade?

You should move the button read stuff from getRGB to loop.

So if i got tis right, when i finally manage to create a Fade function i can put the button function back in the void loop ?

You should either toggle the led on or off, or call the fade function, or both, as you see fit.

Toggle the led on and off is done by the Buttonstate change I believe?
In the current script Led 13(on the arduino board) does turn off an on when pressed.

I do apologise once more, I can imagine how tiresome it can be when talking to someone who doesnt quite has the needed amount of expertise yet to understand complete what you mean, but im willing to learn.

Thanks!

void setup()
{
}

and

void loop()
{
}

are functions. You can create another function just as easily:

void fade()
{
   // Put all the code to do with fading the LED here
}

The actually fading of the LED is implemented by the script, but it is implemented in the loop function. If you want to do something besides fading the LED, it’s better to put the code for fading the LED in a separate function.

Then, whenever you want to fade the LED, call the fade function.

If you make loop look something like this:

int fadeMe = 0;
void loop()
{
    buttonState = digitalRead(buttonPin);
    if(buttonState == HIGH && previousButtonState == LOW)
    {
        // button is now pressed, but wasn't the last time we looked
        if(fadeMe == 0)
           fadeMe = 1;
        else
           faseMe = 0;
    }

    previousButtonState = buttonState;

    if(fadeMe == 1)
      fade();
}

Then each time you press the button, the fading will stop or resume.