ARDUINO IS AN OPEN-SOURCE PHYSICAL COMPUTING PLATFORM DESIGNED TO MAKE EXPERIMENTING WITH ELECTRONICS MORE FUN AND INTUITIVE. ARDUINO HAS ITS OWN UNIQUE, SIMPLIFIED PROGRAMMING LANGUAGE, A VAST SUPPORT NETWORK, AND THOUSANDS OF POTENTIAL USES, MAKING IT THE PERFECT PLATFORM FOR BOTH BEGINNER AND ADVANCED DIY ENTHUSIASTS.
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH); //Turn on LED
delay(1000); //Wait 1s
digitalWrite(13, LOW); //Turn off LED
delay(1000); //Wait 1s
}
Base Code
Light blinks on and off once per second
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH); //Turn on LED
delay(5); //Wait 5ms
digitalWrite(13, LOW); //Turn off LED
delay(5); //Wait 5ms
}
My Modified Code
Light blinks on and off every 0.005 seconds
//Variables
int sensorPin = 0; //Connect to Pin 0
int ledPin = 13; //Connect to Pin 13
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
int sensorValue;
sensorValue = analogRead(sensorPin);
digitalWrite(ledPin, HIGH); //Turn LED On
delay(sensorValue); //Wait 'sensorValue' ms
digitalWrite(ledPin, LOW); //Turn LED Off
delay(sensorValue); //Wait 'sensorValue' ms
}
Code
Potentiometer Video
A potentiometer dial adjusts the resistance in an electrical circuit, and when connected to an LED circuit, turning the dial changes the blink speed of the LED. By altering the resistance, the rate at which the LED capacitor charges and discharges is modified, resulting in a faster or slower blinking pattern depending on the dial's position.
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
int DISPLAY_TIME = 100;
void setup() {
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
void loop() {
mainColors();
showSpectrum();
}
void mainColors() {
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
delay(1000);
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
delay(1000);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(1000);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(1000);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);
}
Base Code
LED flashes through primary and secondary color combinations made through different Red, Green, & Blue levels.
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
int DISPLAY_TIME = 100;
void setup() {
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
void loop() {
//mainColors();
showSpectrum();
}
void showSpectrum() {
int x;
for (x = 0; x < 768; x++)
{
showRGB(x);
delay(10);
}
}
void showRGB(int color) {
int redIntensity;
int greenIntensity;
int blueIntensity;
if (color <= 255) {
redIntensity = 255 - color;
greenIntensity = color;
blueIntensity = 0;
}
else if (color <= 511) {
redIntensity = 0;
greenIntensity = 255-(color-256);
blueIntensity = (color - 256);
}
else // color >= 512 {
redIntensity = (color - 512);
greenIntensity = 0;
blueIntensity = 255-(color-512);
}
analogWrite(RED_PIN, redIntensity);
analogWrite(BLUE_PIN, blueIntensity);
analogWrite(GREEN_PIN, greenIntensity);
}
My Modified Code
Now only shows a pleasant spectrum of color through gradual shifting of RGB values.
void loop()
{
oneAfterAnotherLoop(); //
//oneOnAtATime();
//pingPong(); // Back & Forth
//marquee(); // Chase lights
//randomLED(); // Blink LEDs randomly
}
void oneAfterAnotherLoop() {
int index;
int delayTime = 100;
for(index = 0; index <= 7; index++)
{
digitalWrite(ledPins[index], HIGH);
delay(delayTime);
}
for(index = 7; index >= 0; index--)
{
digitalWrite(ledPins[index], LOW);
delay(delayTime);
}
}
Base Code
void loop() {
pingPong(); // Back & Forth
//oneAfterAnotherLoop(); //
//oneOnAtATime();
//marquee(); // Chase lights
//randomLED(); // Blink LEDs randomly
}
void pingPong() {
int index;
int delayTime = 100;
for(index = 0; index <= 7; index++)
{
digitalWrite(ledPins[index], HIGH);
delay(delayTime);
digitalWrite(ledPins[index], LOW);
}
for(index = 7; index >= 0; index--) {
digitalWrite(ledPins[index], HIGH);
delay(delayTime);
digitalWrite(ledPins[index], LOW);
}
}
My Modified Code
void setup()
{
pinMode(button1Pin, INPUT); //Pushbutton 1
pinMode(button2Pin, INPUT); //Pushbutton 2
pinMode(ledPin, OUTPUT); //LED 1
}
void loop()
{
int button1State, button2State;
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
if (((button1State == LOW) || (button2State == LOW))
((button1State == LOW) && (button2State == LOW)))
{
digitalWrite(ledPin, HIGH); //LED On
}
else
{
digitalWrite(ledPin, LOW); //LED Off
}
}
Code
Code Demonstration
A 2-button logic gate for turning off an LED operates by requiring both buttons to be simultaneously pressed, creating a logical AND condition. When both buttons are pressed, an electrical circuit is completed, allowing current to flow through the LED and illuminate it; otherwise, the LED remains off.
const int sensorPin = 0;
const int ledPin = 9;
int lightLevel, high = 0, low = 1023;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
lightLevel = analogRead(sensorPin);
manualTune(); //Manually change light to dark
analogWrite(ledPin, lightLevel);
}
void manualTune() {
lightLevel = map(lightLevel, 0, 1023, 0, 255);
lightLevel = constrain(lightLevel, 0, 255);
}
void autoTune() {
if (lightLevel < low) {
low = lightLevel;
}
if (lightLevel > high) {
high = lightLevel;
}
lightLevel = map(lightLevel, low+30, high-30, 0, 255);
lightLevel = constrain(lightLevel, 0, 255);
}
Base Code
Uses manualTune function to turn on LED depending on the lightLevel the Arduino detects
const int sensorPin = 0;
const int ledPin = 9;
int lightLevel, high = 0, low = 1023;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
lightLevel = analogRead(sensorPin);
autoTune(); //Automatically change light to dark
analogWrite(ledPin, lightLevel);
}
void manualTune() {
lightLevel = map(lightLevel, 0, 1023, 0, 255);
lightLevel = constrain(lightLevel, 0, 255);
}
void autoTune() {
if (lightLevel < low) {
low = lightLevel;
}
if (lightLevel > high) {
high = lightLevel;
}
lightLevel = map(lightLevel, low+30, high-30, 0, 255);
lightLevel = constrain(lightLevel, 0, 255);
}
My Modified Code
Enables AutoTune function to turn on the LED based on whether is lightLevel is higher or lower
const int temperaturePin = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
float voltage, degreesC, degreesF;
voltage = getVoltage(temperaturePin); //Get Voltage reading from temperaturePin's voltage
degreesC = (voltage - 0.5) * 100.0; //Convert Voltage to Celsius
degreesF = degreesC * (9.0/5.0) + 32.0; //Convert Voltage to Fahrenheit
//Print Voltage, Celsius, and Fahrenheit to Serial
Serial.print("voltage: ");
Serial.print(voltage);
Serial.print(" deg C: ");
Serial.print(degreesC);
Serial.print(" deg F: ");
Serial.println(degreesF);
delay(1000); // Wait 1000ms (1 Second)
}
float getVoltage(int pin){
return (analogRead(pin) * 0.004882814); //Convert analog read to 0.0 to 5.0
}
Base Code
const int temperaturePin = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
float voltage, degreesC, degreesF;
voltage = getVoltage(temperaturePin); //Get Voltage reading from temperaturePin's voltage
degreesC = (voltage - 0.5) * 100.0; //Convert Voltage to Celsius
degreesF = degreesC * (9.0/5.0) + 32.0; //Convert Voltage to Fahrenheit
//Print Voltage, Celsius, and Fahrenheit to Serial
Serial.print("voltage: ");
Serial.print(voltage);
Serial.print(" deg C: ");
Serial.print(degreesC);
Serial.print(" deg F: ");
Serial.println(degreesF);
if(degreesF < 70){
digitalWrite(13, HIGH); //Turn on LED
} else{
digitalWrite(13, LOW); //Turn off LED
}
delay(1000); // Wait 1000ms (1 Second)
}
float getVoltage(int pin){
return (analogRead(pin) * 0.004882814); //Convert analog read to 0.0 to 5.0
}
My Modified Code
#include <Servo.h> // servo library
Servo servo1; // servo control object
void setup() {
servo1.attach(9);
}
void loop()
{
servo1.write(90); //Turn 90 degrees
delay(1000); //Wait 1000ms
servo1.write(180); //Turn 180 degrees
delay(1000); //Wait 1000ms
servo1.write(0); //Turn 0 degrees
delay(1000); //Wait 1000ms
for(position = 0; position < 180; position += 2) {
servo1.write(position); // Move to next position
delay(20); // Wait 20ms
}
for(position = 180; position >= 0; position -= 1) {
servo1.write(position); // Move to next position
delay(20); // Wait 20ms
}
}
Base Code
#include <Servo.h> // servo library
Servo servo1; // servo control object
void setup() {
servo1.attach(9);
}
void loop()
{
int position;
servo1.write(180); // Turn 180 degrees
delay(250); // Wait 250ms
servo1.write(180); // Turn 180 Degrees
delay(250); // Wait 250ms
servo1.write(0); // Turn 0 Degrees
delay(250); // Wait 250ms
for(position = 0; position < 180; position += 2) {
servo1.write(position); // Move to next position
delay(20); // Wait 20ms
}
for(position = 180; position >= 0; position -= 1) {
servo1.write(position); // Move to next position
delay(20); // Wait 20ms
}
}
My Modified Code
const int buzzerPin = 9;
const int songLength = 18; //How many notes
char notes[] = "cdfda ag cdfdg gf "; //Array of note values and rests
int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2}; //Array of note lengths
int tempo = 150;
void setup() {
pinMode(buzzerPin, OUTPUT);
}
void loop() {
int i, duration;
for (i = 0; i < songLength; i++) // step through the song arrays
{
duration = beats[i] * tempo; // length of note/rest in ms
if (notes[i] == ' ') { //If current note is a rest
delay(duration); //Wait for duration ms
}
else { //Otherwise play note
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
}
delay(tempo/10); //Brief pause between notes
}
while(true){}
}
//Takes notes A - G and corresponds them to a frequency the buzzer and produce
int frequency(char note) {
int i;
const int numNotes = 8; //Number of stored notes
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
for (i = 0; i < numNotes; i++) //Go through notes
{
if (names[i] == note) //If correct note
{
return(frequencies[i]); //Return
}
}
return(0);
}
Base Code
const int motorPin = 9;
void setup(){
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
}
void loop(){
// motorOnThenOff();
// motorOnThenOffWithSpeed();
// motorAcceleration();
serialSpeed();
}
void serialSpeed(){
int speed;
Serial.println("Type a speed (0-255) into the box above,");
Serial.println("then click [send] or press [return]");
Serial.println(); // Print a blank line
while(true){
while (Serial.available() > 0){
speed = Serial.parseInt();
// Because analogWrite() only works with numbers from
// 0 to 255, we'll be sure the input is in that range:
speed = constrain(speed, 0, 255);
Serial.print("Setting speed to ");
Serial.println(speed);
analogWrite(motorPin, speed);
}
}
}
Base Coded Song
const int motorPin = 9;
void setup(){
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
}
void loop(){
// motorOnThenOff();
// motorOnThenOffWithSpeed();
motorAcceleration();
// serialSpeed();
}
void motorAcceleration(){
int speed;
int delayTime = 20; //Time between change in speed
for(speed = 0; speed <= 255; speed++){
analogWrite(motorPin,speed); // set the new speed
delay(delayTime); // delay between speed steps
}
for(speed = 255; speed >= 0; speed--){
analogWrite(motorPin,speed); // set the new speed
delay(delayTime); // delay between speed steps
}
}
My Modified Code
Base Code
const int relayPin = 2; //Setup Relay Pin
const int timeDelay = 1000; //Int for delay
void setup() {
pinMode(relayPin, OUTPUT); //Set Pin as Output
}
void loop() {
digitalWrite(relayPin, HIGH); //Turn Relay On
delay(timeDelay); //Wait 1 Second
digitalWrite(relayPin, LOW); //Turn Relay Off
delay(timeDelay); //Wait 1 Second
}
int datapin = 2;
int clockpin = 3;
int latchpin = 4;
byte data = 0;
void setup(){ // Set the three SPI pins to be outputs:
pinMode(datapin, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(latchpin, OUTPUT);
}
void loop(){
//oneAfterAnother(); //All on, all off
oneOnAtATime(); //Scroll down the line
//pingPong(); //Like above, but back and forth
//randomLED(); //Blink random LEDs
//marquee();
//binaryCount(); // Bit patterns from 0 to 255
}
void shiftWrite(int desiredPin, boolean desiredState){
bitWrite(data,desiredPin,desiredState);
shiftOut(datapin, clockpin, MSBFIRST, data);
digitalWrite(latchpin, HIGH);
digitalWrite(latchpin, LOW);
}
void oneOnAtATime(){
int index;
int delayTime = 100; //Wait 100ms (0.1 Seconds)
for(index = 0; index <= 7; index++){ //LEDs 0-7
shiftWrite(index, HIGH); //Turn LED on
delay(delayTime); //Pause
shiftWrite(index, LOW); //Turn LED off
}
}
Base Code
int datapin = 2;
int clockpin = 3;
int latchpin = 4;
byte data = 0;
void setup(){ // Set the three SPI pins to be outputs:
pinMode(datapin, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(latchpin, OUTPUT);
}
void loop(){
//oneAfterAnother(); //All on, all off
//oneOnAtATime(); //Scroll down the line
//pingPong(); //Like above, but back and forth
randomLED(); //Blink random LEDs
//marquee();
//binaryCount(); // Bit patterns from 0 to 255
}
void shiftWrite(int desiredPin, boolean desiredState){
bitWrite(data,desiredPin,desiredState);
shiftOut(datapin, clockpin, MSBFIRST, data);
digitalWrite(latchpin, HIGH);
digitalWrite(latchpin, LOW);
}
void randomLED(){
int index;
int delayTime = 100; //Wait 100ms (0.1 Seconds)
index = random(8); //Random Number 0 - 7
shiftWrite(index, HIGH); //LED on
delay(delayTime); //Pause
shiftWrite(index, LOW); //LED Off
}
My Modified Code