Button
We were able to achieve both LEDs turn on
immediately after the push of a button and stay on.
Fade
We used the button instead for our fading LED
as well. By holding the button, the LED would
become brighter and then loop again and again.
const int LED = 13; // the pin for the LED
const int BUTTON = 7; // the input pin where the
// pushbutton is connected
int val = 0; // val will be used to store the state
// of the input pin
int old_val = 0; // this variable stores the previous
// value of "val"
int state = 0; // 0 = LED off and 1 = LED on
void setup() {
pinMode(LED, OUTPUT); // tell Arduino LED is an output
pinMode(BUTTON, INPUT); // and BUTTON is an input
}
void loop(){
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;
delay(10);
}
old_val = val; // val is now old, let's store it
if (state == 1) {
digitalWrite(LED, HIGH); // turn LED ON
} else {
digitalWrite(LED, LOW);
}
}
Sensor
Using a potentiometer, we were able to control
1. the speed of the blinking
2. the intensity of the LED
through two different codes, but same arrangement.
//CHANGING SPEED
int sensorPin = A0;
int LED = 9;
int sensorValue = 30;
void setup () {
pinMode (LED, OUTPUT);
}
void loop () {
sensorValue = analogRead(sensorPin);
digitalWrite(LED, HIGH);
delay(sensorValue);
digitalWrite(LED, LOW);
delay(sensorValue);
}
//CHANGING INTENSITY
int KNOB = A0;
int LED = 9;
int sensorValue = 0;
int outputValue = 0;
int knobValue = 0;
void setup() {
pinMode (LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
int knobValue = analogRead(A0);
outputValue = map(knobValue, 0, 1023, 0, 255);
analogWrite(LED, outputValue);
Serial.println(outputValue);
delay (10);
}
Actuator
Instead of using LEDs, we used a speaker
from a toy as an output still using the
potentiometer. During the process, however
the chord broke off the speaker, so we were
unable to see if it worked.
int speaker = 11;
int val = 0;
void setup ( ) {
pinMode(speaker, OUTPUT);
}
void loop ( ) {
val = analogRead(0);
tone(speaker, (2*val)-500); // 2 sets range & 500 determines pitch
}
int val = 0;
void setup ( ) {
pinMode(speaker, OUTPUT);
}
void loop ( ) {
val = analogRead(0);
tone(speaker, (2*val)-500); // 2 sets range & 500 determines pitch
}
No comments:
Post a Comment