Skip to content

Instantly share code, notes, and snippets.

@shostakovich
Created September 30, 2012 10:11
Show Gist options
  • Save shostakovich/3806384 to your computer and use it in GitHub Desktop.
Save shostakovich/3806384 to your computer and use it in GitHub Desktop.
LED USB LAMP
/* LED USB Lamp
This program controls a high power LED via PWM and reads in a touch pad.
The circuit: * LED attached from digital pin 3 to ground. * touch pad connected to pin 6 and 5
Created 18 Aug 2012 By Sebastian Schuster
*/
#include <CapSense.h>
CapSense cs_6_5 = CapSense(6,5); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired int ledPin = 3; // LED connected to digital pin 9
int actualState = 0; int nextState = 0;
void setup() { cs_6_5.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example Serial.begin(9600);
actualState = 0; }
boolean readTouchpad(){ long start = millis(); long touchpad = cs_6_5.capSense(30); boolean onOff=0;
Serial.print(millis() - start); // check on performance in milliseconds Serial.print("\t"); // tab character for debug windown spacing Serial.println(touchpad); // print sensor output 1
if (touchpad > 100) onOff=1; return onOff; }
/* state 0: LED on (100%)
(transistion: touch pad activated) state 1: LED on (fading down)
(transistion: touch pad activated) state 2: LED on (fading stop)
(transistion: touch pad activated) state 3: LED on (fading up)
(transistion: touch pad activated) state 4: LED on (fading stop)
*/
int fadeValue= 255; long timer_125ms=0; long timer_125ms_old=0; boolean touchpadPressed_old=0;
void loop() {
timer_125ms = millis();
long timer_diff_125ms = timer_125ms - timer_125ms_old;
//abfrage alle 500ms if(timer_diff_125ms>125) { boolean touchpadPressed = readTouchpad();
switch (actualState) { case 0: Serial.println("State: 0");
if((touchpadPressed==1)&&(touchpadPressed_old==0)) nextState = 1; break; case 1: Serial.println("State: 1");
if((touchpadPressed==1)&&(touchpadPressed_old==0)) nextState = 2; break; case 2: Serial.println("State: 2");
if((touchpadPressed==1)&&(touchpadPressed_old==0)) nextState = 3; break; case 3: Serial.println("State: 3");
if((touchpadPressed==1)&&(touchpadPressed_old==0)) nextState = 4; break; case 4: Serial.println("State: 4");
if((touchpadPressed==1)&&(touchpadPressed_old==0)) nextState = 0; break; }
touchpadPressed_old=touchpadPressed; timer_125ms_old = millis(); }
//##################################################
if(actualState==0) { fadeValue = 255; analogWrite(ledPin, fadeValue); }
else if(actualState==1) { fadeValue -=3; analogWrite(ledPin, fadeValue); }
else if(actualState==3) { fadeValue +=3; analogWrite(ledPin, fadeValue); }
//################################################## actualState=nextState; delay(50); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment