(Arduino study) LED brightness control by switch connection

The source code is combine of Brightness changing(http://feelmare.blogspot.kr/2013/12/arduino-study-led-brightness-changing.html) and LED turn on/off(http://feelmare.blogspot.kr/2013/12/arduino-led-onoff-using-switch-aduino.html).


The LDE brightness is changed when switch is connected.

////
const int LED=9;
const int BUTTON = 7;

int val = 0;

int old_val = 0;
int state = 0;

int brightness = 128;
unsigned long startTime = 0;

void setup(){
  pinMode(LED, OUTPUT);
  pinMode(BUTTON, INPUT);
}

void loop()
{
  val = digitalRead(BUTTON);
  
  if( (val == HIGH) && (old_val == LOW) ){
    state = 1-state;
    startTime = millis();
    delay(10);
  }
  
  
  if( (val == HIGH) && (old_val==HIGH) ){
    
    if(state == 1 && (millis() - startTime) > 500 ){
      
      brightness++;
      delay(10);
      
      if(brightness > 255){
        brightness=0;
      }
    }
  }
  
  
  old_val = val;
  if(state == 1)
  {
    analogWrite(LED, brightness);
  }
}
////





Comments

Popular posts from this blog

OpenCV Stitching example (Stitcher class, Panorama)

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Real-time N camera stitching Class.

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Optical Flow sample source code using OpenCV

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)

(OpenCV Study) calcOpticalFlowFarneback example source code ( dense optical flow )

yuv422(YUYV) to RGB and RGB to yuv422(YUYV), (Using OpenCV and TBB)

(C, C++) TinyXML , xml read & write