Criteria: The purpose of Part 1 is to practice working with both analog input and analog output (PWM) and learn to properly read and write changing I/O values.
Build a circuit with the following:
One potentiometer input *must connect to an analog pin
One other variable resistor input (photocell, FSR, flex sensor, etc.) *must connect to an analog pin
2 standard LEDs as output *must connect to PWM pins
For part 1, I had one potentiometer and one photocell. I had three outputs total: the brightness of the blue and red LED was determined by the potentiometer and the green LED would turn on when the photocell would get an input of <= 100 and off if it read >100.
int potPin = A3; // declaring potentiometer pin
int potVal = 0; //saving value for potentiometer
int lightPin = A0; // declaring Photo cell pin
int lightVal = 0; //saving value for photo cell
const int redLED = 6;
const int blueLED = 5;
const int greenLED = 3;
void setup() {
pinMode(redLED, OUTPUT); // declaring Red LED pin at PWM pin for potentiometer
pinMode(blueLED, OUTPUT); // declaring Blue LED at PWM pin for potentiometer
pinMode(greenLED, OUTPUT); // declaring Blue LED at PWM pin for photo cell
Serial.begin(9600);
}
void loop() {
potVal = analogRead(potPin)/4; //pot reading
lightVal = analogRead(lightPin)/4; //photocell reading
//print message with 2 values
Serial.print(potVal);
Serial.print(" , ");
Serial.println(lightVal);
if(lightVal <= 100){
digitalWrite(greenLED, HIGH);
}
else{
digitalWrite(greenLED, LOW);
}
analogWrite(redLED, potVal);
analogWrite(blueLED, potVal);
}
Criteria: Design an interactive concept based on the following circuit requirements: two or more analog inputs and 2 different outputs (light, sound, movement..). At least one of your outputs needs to be analog (speaker, servo motor, etc.).
Choose your components and develop an interactive concept - for example: a toy, a musical instrument, or a costume. Create a creative enclosure (with soft or hard materials) that supports the interaction you chose. Your enclosure may be laser cut, sewn, or build out of other creative materials. It must look polished, and no wires can be showing! Components must be concealed and/or finished with knobs, light diffusers, etc.
For my sensor box, I choose to do an interactive speedometer toy car. The box is stationary, but there is a pressure sensor where the harder you press it the more the servo motor will turn and more pixel lights will show up. There is also a button that acts like a car horn. Lastly, there is a photocell where if the surroundings are dark, the "headlights" will turn on and when it's bright outside the headlights will not turn on. I used cotton balls to diffuse the LED lights.
I didn't have time to learn how to laser cut in time, so I hand-made the box with cardboard and an expo knife. I made teeth slots so I was able to connect the box. There are seven sides to the box and is a size 8 3/8 x 5.5 x 4 in. I only had tape and hot glue for extra supplies so I just used tape and hold the sides together and hot glue to hold all the components inside. It took me more to make the box then the code.
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#include <Servo.h>
#define PIN 6
int numPixels = 5;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
int pressVal2;
int pressPin = A6; // declaring pressure pin
int pressVal = 0; //saving value for pressure
Servo myservo;
int lightPin = A0; // declaring Photocell pin
int lightVal = 0; //saving value for photocell
const int LED1 = 12;
const int LED2 = 11;
const int button1 = 2;
const int buzzer = 3; //buzzer to arduino pin 3
int buttonState1 = 0;
void setup() {
strip.begin();
strip.setBrightness(30);
strip.show();
myservo.attach(9);
pinMode(LED1, OUTPUT); // declaring Blue LED at PWM pin for pressure
pinMode(LED2, OUTPUT); // declaring Blue LED at PWM pin for photo cell
pinMode(buzzer, OUTPUT); // Set buzzer - pin 3 as an output
Serial.begin(9600);
}
void loop() {
pressVal = analogRead(pressPin); //press reading
lightVal = analogRead(lightPin)/4; //photo cell reading
buttonState1 = digitalRead(button1);
//print message with 2 values
Serial.print(pressVal);
Serial.print(" , ");
Serial.print(lightVal);
Serial.print(" , ");
//Car Beep noise
if(buttonState1 == HIGH) { // button pressed
tone(buzzer, 250); // Send 200Hz sound signal...
delay(50); // ...for 100 milisec
}
else{
tone(buzzer, 0); // Send 200Hz sound signal...
}
// photocell
if (lightVal <= 100){
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
}
else{
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
}
//servo
pressVal2 = analogRead(pressPin); // reads the value of the potentiometer (value between 0 and 1023)
pressVal2 = map(pressVal2, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(180 - pressVal2); // sets the servo position according to the scaled value
delay(100);
pressVal2 = map(pressVal, 0, 1023, 0, 255);
Serial.println(pressVal2);
// Neopixel
if (pressVal2 > 212) {
strip.setPixelColor(4, strip.Color(255, 0, 222));
} else {
strip.setPixelColor(4, strip.Color(0, 0, 0));
}
if (pressVal2 > 170) {
strip.setPixelColor(3, strip.Color(122, 198, 9));
} else {
strip.setPixelColor(3, strip.Color(0, 0, 0));
}
if (pressVal2 > 127) {
strip.setPixelColor(2, strip.Color(100, 63, 180));
} else {
strip.setPixelColor(2, strip.Color(0, 0, 0));
}
if (pressVal2 > 85) {
strip.setPixelColor(1, strip.Color(255, 123, 0));
} else {
strip.setPixelColor(1, strip.Color(0, 0, 0));
}
if (pressVal2 > 42) {
strip.setPixelColor(0, strip.Color(5, 173, 204));
} else {
strip.setPixelColor(0, strip.Color(0, 0, 0));
}
strip.show();
}
// pressure with Neopixel and servo
// Photocell with head lights
// button with buzzer