Monday, August 24, 2015

Hello World with LCD Keypad Shield #Learn Arduino

I already found very useful code to display the character with the LCD.

//I've got nice code from the instructable (http://www.instructables.com/id/Arduino-LCD/)
//And I modified into my name
//The code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int readkey;
void setup() {
lcd.begin(16, 2);
lcd.print("Hello World!");
}
void loop() {
lcd.setCursor(0, 1); //start from coloumn 0 and row 1
lcd.print("My name is Hani");
readkey=analogRead(0);
if (readkey<50) {
lcd.clear();
lcd.print("Button Right");
}
else if(readkey<195) {
lcd.clear();
lcd.print("Button Up");
}
else if(readkey<380) {
lcd.clear();
lcd.print("Button Down");
}
else if(readkey<790) {
lcd.clear();
lcd.print("Button Left");
}
}



Happy share :)

Saturday, August 22, 2015

Playing The Melody


This weekend I added up some instrument called piezo buzzer. We can play the tone, and as the beginner I started with the example from here . The copied code is here




int speakerPin = 9;
int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void setup() {
pinMode(speakerPin, OUTPUT);
}
void loop() {
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}
}
view raw melody.ino hosted with ❤ by GitHub



Friday, August 7, 2015

Controlling the LED Brightness #Learn Arduino



Finally I can go up from blink to another basic of Arduino. In this circuit we can control the brightness of LED with potentiometer.LED brightness is determined from the position knob on the potentiometer.

Some material we need:
  • Arduino (I am using Arduino UNO)
  • LED
  • Potentiometer 10K ohm
  • 330 Ohm resistor
  • PC/laptop
  • Breadboard
  • 6 jumperwire
  • Data cable from Arduino to PC 
 Here is the code that we can simply copying from Arduino IDE example:
/*
Analog Input
Demonstrates analog input by reading an analog sensor on analog pin 0 and
turning on and off a light emitting diode(LED) connected to digital pin 13.
The amount of time the LED will be on and off depends on
the value obtained by analogRead().
The circuit:
* Potentiometer attached to analog input 0
* center pin of the potentiometer to the analog pin
* one side pin (either one) to ground
* the other side pin to +5V
* LED anode (long leg) attached to digital output 13
* LED cathode (short leg) attached to ground
* Note: because most Arduinos have a built-in LED attached
to pin 13 on the board, the LED is optional.
Created by David Cuartielles
modified 30 Aug 2011
By Tom Igoe
This example code is in the public domain.
http://arduino.cc/en/Tutorial/AnalogInput
*/
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}


the new things we learn in here is "sensorPin =A0". It means that we select the input pin for the potentiometer. To read the value from the sensor we just type "sensorValue = analogRead(sensorPin);"  inside the loop.

Monday, August 3, 2015

Blink LED #Learn Arduino

Up from hello world, now we go little bit further to make the LED blinking.

Some material we need:
  • Arduino (I am using Arduino UNO)
  • LED
  • 330 Ohm resistor
  • PC/laptop
  • Breadboard
  • 3 jumperwire
  • Data cable from Arduino to PC
This is the code, and it's available on the example of Arduino IDE Software with title "Blink"

view raw blink.ino hosted with ❤ by GitHub

"high" means 5V supply and make the LED is ON and
"low" meand 0V supply and make the LED is OFF.

The LED will be ON and OFF repeatedly every 1 second  that's because we put the code on the loop.

Sunday, August 2, 2015

Hello World Repeated #Learn Arduino

Modifying from the previous, here I would like to make the "hello world"  appear repeatedly.

---------------------------------------------------------------------------------------------------

//Learn Arduino-Hello world Repeatedly-Hanifadinna

void setup () {
  Serial.begin(9600); //open serial port with speed 9600 bit per second
}

void loop () {
 delay(10000);  // delay time 10 second
 Serial.println("Hello world");  //print out the hello world
}
---------------------------------------------------------------------------------------------------
 Verivy, uplod and we just simply type "ctrl+shift+M" to show the print of "hello world", and then we got this from serial monitor.



To make the hello world appear repeatedly is because we put :

 delay(10000);  // delay time 10 second
 Serial.println("Hello world");  //print out the hello world

inside the void loop, so the hello world will appear repeatedly every 10 second.