5V
is the main power pinGND
pinsDIGITAL
pins, 0
- 13
~
have PWM (Pulse-Width Modulation)... you can turn the power up and down from 0 - 100%L
onboardA0
- A5
button1
is pressed, LOW
, the LEDs will change states. The red LED will turn off and the white LED will turn on. While the bottom pushbuttom button2
is pressed, LOW
, the LEDs will always be turned off, even when both buttons are pressed.
// Initialize the pin variables as integers
int ledPin1 = 12;
int ledPin2 = 11;
int button1 = 10;
int button2 = 9;
// Define the input and output pins
void setup() {
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
// The logic.
// digitalRead: LOW is pressed, HIGH is unpressed
// digitalWrite: LOW is off, HIGH is on full
void loop() {
if (digitalRead(button1) == HIGH
&& digitalRead(button2) != LOW) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
}
if (digitalRead(button1) == LOW
&& digitalRead(button2) != LOW) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
}
if (digitalRead(button2) == LOW) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
}
int redPin = 10;
int greenPin = 9;
int bluePin = 8;
int button = 7;
int randomNum1;
int randomNum2;
int randomNum3;
int inputNum;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(button, INPUT_PULLUP);
Serial.begin(9600); // enable serial monitor
}
void loop() {
if (digitalRead(button) == LOW) {
// Generate the random number
// from 0 to 255, for each color
randomNum1 = random(0, 255); // (min, max)
randomNum2 = random(0, 255);
randomNum3 = random(0, 255);
color(randomNum1, randomNum2, randomNum3);
delay(150);
} else {
// serial monitor input, integer
inputNum = Serial.parseInt();
if ( inputNum > 0 && inputNum <=255) {
color(inputNum, inputNum, inputNum);
}
}
}
// Construct the color combination of:
// red, green, and blue
void color(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
// Show the color combination in the Serial Monitor
Serial.print("\nThe RGB color is: ");
Serial.print("(");Serial.print(red);Serial.print(", ");
Serial.print(green);Serial.print(", ");
Serial.print(blue);Serial.print(")");
}
// the pin that the potentiometer is attached to
const int analogPin = A5;
// the number of LEDs in the bar graph
const int ledCount = 8;
// an array of pin numbers to which LEDs are attached
int ledPins[] = {
5, 6, 7, 8, 9, 10, 11, 12
};
void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
// read the potentiometer:
int sensorReading = analogRead(analogPin);
// map the result to a range
// from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}
analogRead()
, in a range from 0 - 1023. The LED is controlled by pulse wave modulation (PWM) on pin ~11
with the analogWrite()
command.
Serial.println(calValue);
with Serial.println(lightSensorLevel);
. Then set calibrateLowest
and calibrateHigest
to the lowest and highest readings that you get in your Serial Monitor in your Arduino IDE.
int liSenLev; // light sensor level
int calVal; // calibrated value
int ledValue;
int liSenPin = A0; // analog
int ledPin = 11; // ~PWM
// Calibration depends on your 3 things:
// light levels, photocell, resistor.
// set from "liSenLev" reading... the darkest
// it usually gets in the room you're in
int calibrateLow = 25;
// set from "liSenLev" reading... the brightest
// it usually gets in the room you're in
int calibrateHigh = 575;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(liSenPin, INPUT);
Serial.begin(9600); //enable serial monitor
}
void loop() {
// analog reading: 0 - 1023
liSenLev = analogRead(liSenPin);
// map liSenLev (0 - 1023) using reversed,
// calibrated range (calibrateHigh to calibrateLow),
// to 0 - 255 LED brightness
calVal = map(liSenLev, calibrateHigh, calibrateLow, 0, 255);
// failsafe, if sensed value goes above
// your defined calibrated range
if (calVal > 255) {
calVal = 255;
} else if (calVal < 0) {
calVal = 0;
}
analogWrite(ledPin, calVal);
// determine your calibrated range using "liSenLev"
Serial.println("liSenLev: " + String(liSenLev) +
", calVal: " + String(calVal));
delay(100);
}
threshold
. In this setup the readings normalize at around 288, but it will be different for you depending on your resistor and flex sensor. [If the LCD is not displaying, try turning the contrast potentiometer all the way in either direction]
#include <LiquidCrystal.h>
// initialize the library with numbers of interface pins
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
int flexPin = A0;
int loadPin = 6;
int storeMax = 0; // initial
int storeMin = 1000; // initial
int threshold = 250; // adjust, as necessary
String storeMaxS;
String storeMinS;
String lcdView;
void setup() {
pinMode(flexPin, INPUT);
pinMode(loadPin, OUTPUT);
lcd.begin(16, 2);// set LCD's number of columns and rows
lcd.clear(); // clear existing text on screen
}
void loop() {
int flexVal = analogRead(flexPin);
if (flexVal > storeMax) {
storeMax = flexVal;
storeMaxS = String(storeMax);
}
if (flexVal < storeMin) {
storeMin = flexVal;
storeMinS = String(storeMin);
}
lcd.clear();
// turn something (LED) on based on threshold
if (flexVal < threshold) {
digitalWrite(loadPin, HIGH);
lcdView = " ***ACTIVE***";
} else {
digitalWrite(loadPin, LOW);
lcdView = "";
}
// display in the form: MIN < CURRENT VALUE < MAX
lcd.print(storeMinS + " < " + String(flexVal) +
" < " + storeMaxS + lcdView);
delay(200); // more human-friendly
}
#include <LiquidCrystal.h>
// initialize library with numbers of interface pins
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
int up = 5;
int right = 4;
int left = 3;
int down = 2;
String stringy;
void setup() {
pinMode(up, INPUT_PULLUP);
pinMode(right, INPUT_PULLUP);
pinMode(left, INPUT_PULLUP);
pinMode(down, INPUT_PULLUP);
lcd.begin(16, 2); // set LCD's number of columns and rows
lcd.clear(); // clear existing text on screen
lcd.print("PRESS BUTTONS"); // startup message
}
void loop() {
if (digitalRead(up) == LOW) {
writeLCD("up");
} else if (digitalRead(right) == LOW) {
writeLCD("right");
} else if (digitalRead(left) == LOW) {
writeLCD("left");
} else if (digitalRead(down) == LOW) {
writeLCD("down");
}
}
void writeLCD(String stringy) {
lcd.clear();
lcd.print(stringy); // print string to LCD
delay(10); // good practice to delay after commands to LCD
}
#include <LiquidCrystal.h>
// initialize library with numbers of interface pins
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
int rightLED = 6; // ~PWM, right side
int leftLED = 5; // ~PWM, left side
int up = A0;
int right = A1;
int left = A2;
int down = A3;
int rightPower = 50;
int leftPower = 50;
int currentFlagLED = 0;
void setup() {
pinMode(rightLED, OUTPUT);
pinMode(leftLED, OUTPUT);
pinMode(up, INPUT_PULLUP);
pinMode(right, INPUT_PULLUP);
pinMode(left, INPUT_PULLUP);
pinMode(down, INPUT_PULLUP);
lcd.begin(16, 2); // set LCD's number of columns & rows
lcd.clear();
lcd.print("PRESS BUTTONS");
setLED(""); // start with LEDs on
}
/*
=======
Control
=======
*/
void loop() {
if (digitalRead(up) == LOW) {
setLED("up");
} else if (digitalRead(right) == LOW) {
blinkie(rightLED, rightPower);
currentFlagLED = 2;
} else if (digitalRead(left) == LOW) {
blinkie(leftLED, leftPower);
currentFlagLED = 1;
} else if (digitalRead(down) == LOW) {
setLED("down");
}
delay(40);// prevent from jumping too fast, skipping
}
/*
================
Helper Functions
================
*/
void displayPower(int leftPower, int rightPower) {
lcd.clear();
lcd.setCursor(0, 0);
String concatString1 = "LEFT LED: "
+ String(leftPower)
+ "%";
lcd.print(concatString1); // print string to LCD
lcd.setCursor(0, 1);
String concatString2 = "RIGHT LED: "
+ String(rightPower)
+ "%";
lcd.print(concatString2); // print string to LCD
delay(10);
}
void setPower(int led, int powerLevel) {
int val = (float) (powerLevel*255)/100;
analogWrite(led, val);
}
void setLED(String forWhat) {
if (currentFlagLED == 0)
// when just turn on
{
blinkie(rightLED, rightPower);
blinkie(leftLED, leftPower);
}
else if (currentFlagLED == 1 && forWhat == "up")
// left, UP
{
if (leftPower < 100) { ++leftPower; }
}
else if (currentFlagLED == 1 && forWhat == "down")
// left, DOWN
{
if (leftPower > 0) { --leftPower; }
}
else if (currentFlagLED == 2 && forWhat == "up")
// right, UP
{
if (rightPower < 100) { ++rightPower; }
}
else if (currentFlagLED == 2 && forWhat == "down")
// right, DOWN
{
if (rightPower > 0) { --rightPower; }
}
setPower(leftLED, leftPower);
setPower(rightLED, rightPower);
displayPower(leftPower, rightPower);
}
void blinkie(int led, int power) {
digitalWrite(led, LOW);
delay(200);
digitalWrite(led, power);
}
int ledPin = 12;
int buzzPin = 11;
int forSenPin = A5;
int buttonPin = 2;
int flag; // unarmed = 0, armed = 1
int force;
int range = 10; // (%) whole number only, adjust sensitivity
int rangeAccept;
int rangeAcceptHigh;
int rangeAcceptLow;
// non-blocking blink [credit: Mellis & Stoffregen]
unsigned long previousMillis = 0;
int ledState = LOW;
int interval = 3000;
int intervalFast = 100;
void setup(){
pinMode(ledPin, OUTPUT);
pinMode(buzzPin, OUTPUT);
pinMode(forSenPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600); // enable serial monitor
}
void loop(){
force = analogRead(forSenPin);
outputs();
if (digitalRead(buttonPin) == LOW) {
calibrate(); // press button to calibrate & activate alarm
}
delay(20); // reduce rate affects readings and buzzer
}
void calibrate() {
if(force == 0) { // if calibrated with nothing on first
flag = 0; // disarm
// failsafe: undo buzzer and led
digitalWrite(ledPin, LOW);
tone(buzzPin, 0, 100);
} else if (force > 0) {
flag = 1;
}
// calibrate to ± __% of force
// cast the float with (int)
rangeAccept = (int) (force/100*range);
rangeAcceptHigh = force + rangeAccept;
rangeAcceptLow = force - rangeAccept;
delay(500); // just one reading while button pressed
}
void outputs() {
// max: 4294967295 ms = 1193 hr = 49 days
unsigned long currentMillis = millis();
// Blink to show when detected and protected
if (force > rangeAcceptLow &&
force < rangeAcceptHigh && flag == 1) {
if (currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
interval = intervalFast; // blip LED on
} else {
ledState = LOW;
interval = 3000; // reset to low rest state
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
// Blink very fast and sound buzzer for alarm...
if (force < rangeAcceptLow && flag == 1 ||
force > rangeAcceptHigh && flag == 1) {
if (currentMillis - previousMillis > intervalFast) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
// sound the buzzer alarm: tone(pin, frequency, duration)
// 50, 1100, 0r 1605 sound pretty alarm-y
tone(buzzPin, 1100, 100);
}
}
}