top of page

Experimenting with Arduino

Writer's picture: Valentina NenkovaValentina Nenkova

Experiment 1 - make a light blink



This was the easiest code and interaction to make. It was very simple which allowed us to understand the basics of arduino and how it works.


The code -

/*

Blink


Turns an LED on for one second, then off for one second, repeatedly.


Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO

it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to

the correct LED pin independent of which board is used.

If you want to know what pin the on-board LED is connected to on your Arduino

model, check the Technical Specs of your board at:

https://www.arduino.cc/en/Main/Products


modified 8 May 2014

by Scott Fitzgerald

modified 2 Sep 2016

by Arturo Guadalupi

modified 8 Sep 2016

by Colby Newman


This example code is in the public domain.


http://www.arduino.cc/en/Tutorial/Blink

*/


// the setup function runs once when you press reset or power the board

void setup() {

// initialize digital pin LED_BUILTIN as an output.

pinMode(LED_BUILTIN, OUTPUT);

}


// the loop function runs over and over again forever

void loop() {

digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)

delay(500); // wait for a second

digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW

delay(500); // wait for a second

digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)

delay(500); // wait for a second

digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW

delay(500); // wait for a second

digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)

delay(500); // wait for a second

digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW

delay(500); // wait for a second

//the longer blinks of morse code

digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW

delay(1000); // wait for a second

digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW

delay(1000); // wait for a second

digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW

delay(1000); // wait for a second


//short blinks again

digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)

delay(500); // wait for a second

digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW

delay(500); // wait for a second

digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)

delay(500); // wait for a second

digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW

delay(500); // wait for a second

digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)

delay(500); // wait for a second

digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW

delay(2000); // wait for a second

}


Experiment 2 - Christmas lights


We then when a little bit more advance and started adding more lights and more cables which also changed the code itself. This definitely took some good patience even though once you do one you understand the get the hang of it, you have a lot more cables to work with which makes in a way more complicated because you start to confuse yourself whats where for what.


The code -

/*

SparkFun Inventor's Kit

Example sketch 04


MULTIPLE LEDs


Make eight LEDs dance. Dance LEDs, dance!


Hardware connections:


You'll need eight LEDs, and eight 330 Ohm resistors

(orange-orange-brown).


For each LED, connect the negative side (shorter leg)

to a 330 Ohm resistor.


Connect the other side of the resistors to GND.


Connect the positive side (longer leg) of the LEDs

to Arduino digital pins 2 through 9.


This sketch was written by SparkFun Electronics,

with lots of help from the Arduino community.

This code is completely free for any use.

Visit http://learn.sparkfun.com/products/2 for SIK information.

Visit http://www.arduino.cc to learn about the Arduino.


Version 2.0 6/2012 MDG

*/



// To keep track of all the LED pins, we'll use an "array".

// An array lets you store a group of variables, and refer to them

// by their position, or "index". Here we're creating an array of

// eight integers, and initializing them to a set of values:


int ledPins[] = {2,3,4,5,6,7,8,9};


// The first element of an array is index 0.

// We've put the value "2" in index 0, "3" in index 1, etc.

// The final index in the above array is 7, which contains

// the value "9".


// We're using the values in this array to specify the pin numbers

// that the eight LEDs are connected to. LED 0 is connected to

// pin 2, LED 1 is connected to pin 3, etc.



void setup()

{

int index;


// In this sketch, we'll use "for() loops" to step variables from

// one value to another, and perform a set of instructions for

// each step. For() loops are a very handy way to get numbers to

// count up or down.


// Every for() loop has three statements separated by

// semicolons (;):


// 1. Something to do before starting

// 2. A test to perform; as long as it's true, keep looping

// 3. Something to do after each loop (increase a variable)


// For the for() loop below, these are the three statements:


// 1. index = 0; Before starting, make index = 0.

// 2. index <= 7; If index is less or equal to 7,

// run the following code.

// (When index = 8, continue with the sketch.)

// 3. index++ Putting "++" after a variable means

// "add one to it".

// (You can also use "index = index + 1".)


// Every time you go through the loop, the statements following

// the for() (within the brackets) will run.


// When the test in statement 2 is finally false, the sketch

// will continue.



// Here we'll use a for() loop to initialize all the LED pins

// to outputs. This is much easier than writing eight separate

// statements to do the same thing.


// This for() loop will make index = 0, then run the pinMode()

// statement within the brackets. It will then do the same thing

// for index = 2, index = 3, etc. all the way to index = 7.


for(index = 0; index <= 7; index++)

{

pinMode(ledPins[index],OUTPUT);

// ledPins[index] is replaced by the value in the array.

// For example, ledPins[0] is 2

}

}



void loop()

{

// This loop() calls functions that we've written further below.

// We've disabled some of these by commenting them out (putting

// "//" in front of them). To try different LED displays, remove

// the "//" in front of the ones you'd like to run, and add "//"

// in front of those you don't to comment out (and disable) those

// lines.


oneAfterAnotherNoLoop(); // Light up all the LEDs in turn


//oneAfterAnotherLoop(); // Same as oneAfterAnotherNoLoop,

// but with much less typing


//oneOnAtATime(); // Turn on one LED at a time,

// scrolling down the line


//pingPong(); // Light the LEDs middle to the edges


//marquee(); // Chase lights like you see on signs


//randomLED(); // Blink LEDs randomly

}



/*

oneAfterAnotherNoLoop()


This function will light one LED, delay for delayTime, then light

the next LED, and repeat until all the LEDs are on. It will then

turn them off in the reverse order.


This function does NOT use a for() loop. We've done it the hard way

to show you how much easier life can be when you use for() loops.

Take a look at oneAfterAnotherLoop() further down, which does

exactly the same thing with much less typing.

*/


void oneAfterAnotherNoLoop()

{

int delayTime = 100; // time (milliseconds) to pause between LEDs

// make this smaller for faster switching


// turn all the LEDs on:


digitalWrite(ledPins[0], HIGH); //Turns on LED #0 (pin 2)

delay(delayTime); //wait delayTime milliseconds

digitalWrite(ledPins[1], HIGH); //Turns on LED #1 (pin 3)

delay(delayTime); //wait delayTime milliseconds

digitalWrite(ledPins[2], HIGH); //Turns on LED #2 (pin 4)

delay(delayTime); //wait delayTime milliseconds

digitalWrite(ledPins[3], HIGH); //Turns on LED #3 (pin 5)

delay(delayTime); //wait delayTime milliseconds

digitalWrite(ledPins[4], HIGH); //Turns on LED #4 (pin 6)

delay(delayTime); //wait delayTime milliseconds

digitalWrite(ledPins[5], HIGH); //Turns on LED #5 (pin 7)

delay(delayTime); //wait delayTime milliseconds

digitalWrite(ledPins[6], HIGH); //Turns on LED #6 (pin 8)

delay(delayTime); //wait delayTime milliseconds

digitalWrite(ledPins[7], HIGH); //Turns on LED #7 (pin 9)

delay(delayTime); //wait delayTime milliseconds


// turn all the LEDs off:


digitalWrite(ledPins[7], LOW); //Turn off LED #7 (pin 9)

delay(delayTime); //wait delayTime milliseconds

digitalWrite(ledPins[6], LOW); //Turn off LED #6 (pin 8)

delay(delayTime); //wait delayTime milliseconds

digitalWrite(ledPins[5], LOW); //Turn off LED #5 (pin 7)

delay(delayTime); //wait delayTime milliseconds

digitalWrite(ledPins[4], LOW); //Turn off LED #4 (pin 6)

delay(delayTime); //wait delayTime milliseconds

digitalWrite(ledPins[3], LOW); //Turn off LED #3 (pin 5)

delay(delayTime); //wait delayTime milliseconds

digitalWrite(ledPins[2], LOW); //Turn off LED #2 (pin 4)

delay(delayTime); //wait delayTime milliseconds

digitalWrite(ledPins[1], LOW); //Turn off LED #1 (pin 3)

delay(delayTime); //wait delayTime milliseconds

digitalWrite(ledPins[0], LOW); //Turn off LED #0 (pin 2)

delay(delayTime); //wait delayTime milliseconds

}



/*

oneAfterAnotherLoop()


This function does exactly the same thing as oneAfterAnotherNoLoop(),

but it takes advantage of for() loops and the array to do it with

much less typing.

*/


void oneAfterAnotherLoop()

{

int index;

int delayTime = 100; // milliseconds to pause between LEDs

// make this smaller for faster switching


// Turn all the LEDs on:


// This for() loop will step index from 0 to 7

// (putting "++" after a variable means add one to it)

// and will then use digitalWrite() to turn that LED on.


for(index = 0; index <= 7; index++)

{

digitalWrite(ledPins[index], HIGH);

delay(delayTime);

}


// Turn all the LEDs off:


// This for() loop will step index from 7 to 0

// (putting "--" after a variable means subtract one from it)

// and will then use digitalWrite() to turn that LED off.


for(index = 7; index >= 0; index--)

{

digitalWrite(ledPins[index], LOW);

delay(delayTime);

}

}



/*

oneOnAtATime()


This function will step through the LEDs,

lighting only one at at time.

*/


void oneOnAtATime()

{

int index;

int delayTime = 100; // milliseconds to pause between LEDs

// make this smaller for faster switching


// step through the LEDs, from 0 to 7


for(index = 0; index <= 7; index++)

{

digitalWrite(ledPins[index], HIGH); // turn LED on

delay(delayTime); // pause to slow down

digitalWrite(ledPins[index], LOW); // turn LED off

}

}



/*

pingPong()


This function will step through the LEDs,

lighting one at at time in both directions.

*/


void pingPong()

{

int index;

int delayTime = 100; // milliseconds to pause between LEDs

// make this smaller for faster switching


// step through the LEDs, from 0 to 7


for(index = 0; index <= 7; index++)

{

digitalWrite(ledPins[index], HIGH); // turn LED on

delay(delayTime); // pause to slow down

digitalWrite(ledPins[index], LOW); // turn LED off

}


// step through the LEDs, from 7 to 0


for(index = 7; index >= 0; index--)

{

digitalWrite(ledPins[index], HIGH); // turn LED on

delay(delayTime); // pause to slow down

digitalWrite(ledPins[index], LOW); // turn LED off

}

}



/*

marquee()


This function will mimic "chase lights" like those around signs.

*/


void marquee()

{

int index;

int delayTime = 200; // milliseconds to pause between LEDs

// Make this smaller for faster switching


// Step through the first four LEDs

// (We'll light up one in the lower 4 and one in the upper 4)


for(index = 0; index <= 3; index++) // Step from 0 to 3

{

digitalWrite(ledPins[index], HIGH); // Turn a LED on

digitalWrite(ledPins[index+4], HIGH); // Skip four, and turn that LED on

delay(delayTime); // Pause to slow down the sequence

digitalWrite(ledPins[index], LOW); // Turn the LED off

digitalWrite(ledPins[index+4], LOW); // Skip four, and turn that LED off

}

}



/*

randomLED()


This function will turn on random LEDs. Can you modify it so it

also lights them for random times?

*/


void randomLED()

{

int index;

int delayTime;


// The random() function will return a semi-random number each

// time it is called. See http://arduino.cc/en/Reference/Random

// for tips on how to make random() even more random.


index = random(8); // pick a random number between 0 and 7

delayTime = 100;


digitalWrite(ledPins[index], HIGH); // turn LED on

delay(delayTime); // pause to slow down

digitalWrite(ledPins[index], LOW); // turn LED off

}



Experiment 3 - turn light one with button



This experiment was easier than some of the other ones that we try to do. The webiste below is very helpful to how to do this. It literally tells you step by step how to make it work. It has the code exactly how you need it and all you have to do is copy and paste it onto the arduino software and connect it with a wire and finally upload it. What was even more helpful was the fact that it has images which makes it x100 easier to understand and create this little interaction.


The code -

https://www.instructables.com/id/Arduino-Button-With-LED/?fbclid=IwAR20HwAUxrigs93ldqlNeHqZ4HZSbZPZtFb8omzJbSgo8WdCnVRWT8ChXDg


Experiment 4 - make the buzzer sing happy birthday by pressing a button



With this we had some difficulties. At the beginning it wasn't making any sound and we kept checking if maybe the code was broken or we had connected something in the wrong place, but everything seemed fine. We talked to Xavi about it to check how it is and what's wrong with it. First of all one of the wires was wrongly connected, the code was fine, however we also discovered that our button was not plugged in right as in it didn't work when you press it, so we decided to change it. The problem after that was for the fact that you have to press it and then sing happy birthday, but ours constantly just sang happy birthday without having to press the butter. The it turned out that we had to create this 'library' into arduino which will allow you to insert the different keys from the melody. We couldn't understand how we have to do this, so we googled it and it literally came up step by step how to create the library.


The code -

https://www.instructables.com/id/Happy-Birthday-Buzzer-and-Button/


Working with this program and this website it's essential to understand that what they are asking you to do is literally what you have to do, step by step to be able to have this working. It will not work if it is not done the way they have shown. Another thing to keep in mind is that some of the cables might not be properly plugged in or maybe the buzzer or light itself might not work, so it's good for those to be checked out too. It's quite interesting how this works and the fact that so many programs and machines are actually done just using these elements that we have to.

3 views0 comments

Recent Posts

See All

Commentaires


bottom of page