Exercise 3 – Sensors & I/O

Part 1

Setting up the initial version of the button example was simple. The only issue I ran into was accidentally using a switch at first instead of the button because of the appearance of the button in the Fritzing diagram, which meant that the light would only turn on for an instant when the switch was toggled. After that, however, everything worked fine. To rewire with different outputs, I saved wires by running the 10k resistor directly to the ground rail from the button rail as well as putting the cathode of the LED into the ground rail. I also changed my inputs to pins 8 and 10 and made the associated edits to the code.

const int buttonPin = 10;
const int ledPin = 8;

Part 2

For this part I added a second, green LED in addition to the red LED, attached in the same way with the cathode directly in the ground rail. As for functionality, I first added the necessary code to interact with the second LED. This time, the green LED activates whenever the button is pressed. In addition, as long as the button is held for at least two seconds, the red light will also activate and remain on for two seconds after the button is released.

const int buttonPin = 12;
const int redLedPin = 9;
const int greLedPin = 8;
int redState = LOW;
int greState = LOW;

int buttonState = 0; // variable for reading the pushbutton status

unsigned long previousTime = 0;
const int timeDelay = 2000;

void setup() {
// initialize the LED pin as an output:
pinMode(redLedPin, OUTPUT);
pinMode(greLedPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
unsigned long currentTime = millis();

// Turn on the green light while the button is pressed
if(buttonState == HIGH){
digitalWrite(greLedPin, HIGH);
}
else {
digitalWrite(greLedPin, LOW);
}

if(currentTime – previousTime >= timeDelay){
previousTime = currentTime;
// check if the pushbutton is pressed.
if (buttonState == HIGH) {
digitalWrite(redLedPin, HIGH);
}
else {
digitalWrite(redLedPin, LOW);
}
}
}

Part 3

For my modification of the analog basics example, I chose to replace the potentiometer with a photoresistor in order to retrieve analog input. Along with this, I connected a switch for digital input. Using an if() statement to check the state of the switch, I made it so that the light would only blink when the switch is turned on. With the addition of the photoresistor, the light now blinks faster the less light reaches the sensor and slower as more light reaches it.

const int switchPin = 11;
const int sensorPin = A3;
const int ledPin = 13;
int sensorValue = 0; // value of the photoresistor
int switchState = 0;

void setup() {
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
}

void loop() {
// Get the state of the switch
switchState = digitalRead(switchPin);

// Check if the switch is on
if(switchState == HIGH){
sensorValue = analogRead(sensorPin);
digitalWrite(ledPin, HIGH);
delay(sensorValue);
digitalWrite(ledPin, LOW);
delay(sensorValue);
}
}

Part 4

In completing this section, I largely followed the provided voltage divider example. Since I did not have a force sensitive resistor, I instead used a thermistor. Once the thermistor, potentiometer, and photoresistor were connected to the analog input, I added the three LEDs via digital pins 11-13. To control the blinking rate of each light, I adapted code from the AnalogBasics example to relate the analog values to the length of delay affecting the light states.

const int knobPin = A0;
const int photoPin = A3;
const int thermalPin = A5;
int knobValue = 0;
int photoValue = 0;
int thermalValue = 0;
int firstPin = 13;
int secondPin = 12;
int thirdPin = 11;

void setup() {
pinMode(firstPin, OUTPUT);
pinMode(secondPin, OUTPUT);
pinMode(thirdPin, OUTPUT);
pinMode(knobPin, INPUT);
pinMode(photoPin, INPUT);
pinMode(thermalPin, INPUT);
}

void loop() {
// read the sensor’s value (0-1023**)
knobValue = analogRead(knobPin);
photoValue = analogRead(photoPin);
thermalValue = analogRead(thermalPin);

// set the potentiometer light’s rate
digitalWrite(firstPin, HIGH);
delay(knobValue);
digitalWrite(firstPin, LOW);
delay(knobValue);

// Set the photoresistor pin light’s rate
digitalWrite(secondPin, HIGH);
delay(photoValue);
digitalWrite(secondPin, LOW);
delay(photoValue);

// Set the thermal resistor light’s rate
digitalWrite(thirdPin, HIGH);
delay(thermalValue);
digitalWrite(thirdPin, LOW);
delay(thermalValue);
}

Analog Output

Setting up a potentiometer to control the PWM rate was straightforward. All that was required was the addition of the control wired into the power/ground rails and an analog input in the board, leaving the LED as it was for the initial Fade setup. With the appropriate variables to track the analog value, the next step was to map the analog values to PWM. This took me more time than it should have as I first attempted to implement a mapping formula before thinking to check for a mapping function within Processing. Despite attempting to follow several guides, I was unable to figure out an implementation of a low-pass filter to control the fade rate.

int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
const int ledPin = 11; // LED pin, this pin must be a PWM-capable pin
const int analogPin = A0; // pin for potentiometer
int sensorValue = 0;

void setup() {
// declare the LED pin to be an output:
pinMode(analogPin, INPUT);
pinMode(ledPin, OUTPUT);
}

void loop() {
// set the brightness of the LED pin:
analogWrite(ledPin, brightness);

sensorValue = analogRead(analogPin);

// Map the sensor value to a PWM value
fadeAmount = map(sensorValue, -1023, 1023, 0, 255);

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website at WordPress.com
Get started
%d bloggers like this: