Monday, October 20, 2014

//This is an updated code for the light that lights up when pressed
//And dims when not pressed. The last code turned it on and off
//completely, whereas this one can move between 0-250 whenever the
//button is pressed or released without having to to to a max.
//Includes "Serial.println's" for debugging.
int led=3;
int val;
int buttonstate;
int fadeValue;
int switchpin=2;
int Val2;

void setup()
{
  pinMode(switchpin,INPUT);
  Serial.begin(9600);
  buttonstate=digitalRead(switchpin);
}
void loop()
{
  val=digitalRead(switchpin);
  Serial.println("AAAAAAAAAAAAA");
  if(val!=buttonstate)
  {
    if(val==HIGH)
    {
      Serial.println("INCREASING");
      Val2=1;
      delay(70);
    }
    else
    {
      Serial.println("DECREASING");
      Val2=2;
      delay(70);
    }
  }
  if(Val2==1)
  {
    if(fadeValue<=250)
    {
    fadeValue=fadeValue+5;
    }
  }
  if(Val2==2)
  {
    if(fadeValue>=5)
    {
    fadeValue=fadeValue-5;
    }
  }
  analogWrite(led, fadeValue);
  Serial.println(fadeValue);
  buttonstate=val;
}
   

No comments:

Post a Comment