Skip to content

Instantly share code, notes, and snippets.

@c010
Created January 27, 2016 03:49
Show Gist options
  • Save c010/bd18ed4a9d4b04c827ba to your computer and use it in GitHub Desktop.
Save c010/bd18ed4a9d4b04c827ba to your computer and use it in GitHub Desktop.
Tut 2
void fade2()
{
val = digitalRead(BUTTON); // read input value and store it // yum, fresh
// check if there was a transition
if ((val == HIGH) && (old_val == LOW))
{
state = 1 - state; // change the state from off to on
// or vice-versa
startTime = millis(); // millis() is the Arduino clock // it returns how many milliseconds
// have passed since the board has
// been reset.
// (this line remembers when the button // was last pressed)
delay(10);
}
// check whether the button is being held down
if ((val == HIGH) && (old_val == HIGH))
{
// If the button is held for more than 500 ms.
if (state == 1 && (millis() - startTime) > 900)
// last number is how long the led will stay at max before
//going through this if statement
{
brightness++; // increment brightness by 1
delay(60); // delay to avoid brightness going
// up too fast
if (brightness > 55)
{
brightness = 0; // if we go over 255 it goes back to 0
}
}
}
old_val = val; // val is now old, let's store it
if (state == 1)
{
analogWrite(LED, brightness); // turn LED ON at the
// current brightness level
}
else
{
analogWrite(LED, 0); // turn LED OFF
}
}
void serial()
{
val = analogRead(SENSOR); // read the value from // the sensor
Serial.print("val = ");
Serial.println(val); // print the value. adds new line after// the serial port
delay(100); // wait 100ms between // each send
}
const int LED = 9; // the pin for the LED
int i = 0; // We'll use this to count up and down
int led = 13;
const int BUTTON = 7; // input pin of the pushbutton
int val = 0; // stores the state of the input pin
int old_val = 0; // stores the previous value of "val"
int state = 0; // 0 = LED off while 1 = LED on
int brightness = 128; // Stores the brightness value
unsigned long startTime = 0; // when did we begin pressing
const int SENSOR = 0;
void setup()
{
pinMode(LED, OUTPUT); // LED is an output
pinMode(led, OUTPUT); // LED is an output
pinMode(BUTTON, INPUT); // BUTTON is an input
Serial.begin(9600); // open the serial port to send
// data back to the computer at
// 9600 bits per second
}
void loop()
{
// fade2();
//lightsource_blink();
serial();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment