Tuesday, December 17, 2013

Do while loop

This loop quiet similar to While loop, but here firstly it does action and then check condition.

Synatx:

do
{
  //code
} while(condition);

Example:

int i=15;
do
{
  i=i-5;
} while(i!=0);

This code will substract '5' from i until it is not equals to '0'.

While loop

This loop will repeat action until it is true.
Syntax:

while(condition)
{
  //code
}

Example:

int i=10;
while(i > 0)
{
  i=i-1;
}

This loop will continue substracting '1' from i, until i > 10.

For loops and Arduinod

We need loops to repeat one action multiple times.
Syntax:

for(variable, which we will change; condition; change variable)
{
  //code to repeat
}

Example:

int a=0;
for(int i=0; i<10; i++)
{
  a=a+1;
}

This code will add to a '1' 10 times, so at the end of our loop a will be equal '10'.

Arduibo serial port

You can use serial port for displaying text, which will help you to debug your code. Firstly you need to start it:

void start()
{
  Serial.begin(9600); //Starting serial port
}

Then you can use it. Example:

void loop()
{
  Serial.println("Simple text");
}

Friday, December 13, 2013

Arduino Switch and Case

Now I will tell you how to use "Switch and Case" in Arduino. You can use it instead of "if". 

Here you can see a code:

int i = Random(5); //generate Random number from 0 to 4

switch(i)
{
  case 0:
    //Action if value is 0
    break;

  case 1:
    //Action if value is 1
    break;

  case 2:
    //Action if value is 2
    break;

  case 3:
    //Action if value is 3
    break;

  case 4:
    //Action if value is 4
    break;
}

Arduino easy button contrAol

When I was searching the WEB, I have found very easy way how to control buttons in Arduino.

Connections:
OK, and now code:

int ledPin = 13; // choose the pin for the LED
int inPin = 7; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status

void setup() 
{  
  pinMode(ledPin, OUTPUT); // declare LED as output    pinMode(inPin, INPUT); // declare pushbutton as input
}

void loop()

{  
  val = digitalRead(inPin); // read input value  
  if (val == HIGH) //check if the input is HIGH (button released)
      
    digitalWrite(ledPin, LOW); // turn LED OFF  
  } 
  else 
  {    
    digitalWrite(ledPin, HIGH); // turn LED ON  
  }
}

Array database part 3

Now i will show you how to setup array to kind of "default" value. For example to 0.
C0D3:

int array[10]; //create int array

for(int i=0; i<10; i++) //for loop
{
    array[i] = 0; //setup to default value
}

Array database part 2

Also you can change array after initialization. For example:

int array[10]; //create int array, which can store 10 int values (from 0 to 9)

array[0] = 2;
array[1] = 4;
array[2] = 5;
//...

Tuesday, December 3, 2013

Array database

  It (array) will be useful for you if you don't want to initialise tones of variables. For example: instead of using two int variables we can create one int array, which will store two int values.

//instead of using it:
int a = 2;
int b = 4;

//we can use it:
int array[2] = {2, 4};

Syntax:
typeOfVariable anyNameYouWant[emountOfValuesToStore] = {vars};

Sunday, December 1, 2013

How to control speaker with Arduino

  Hello everyone, today i will show you how to control speaker with Arduino.

We will need:

  • Arduino
  • Computer
  • Speaker
  • Wires
  • Arduino software (compiler)
  • Arduino cable
Firtly we need to connect speaker to digital output, for example to digital output 8.
Ok, now code. 
At the beginning lets define our notes:

#define NOTE_B0  31

#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978


Next step is to create our melody with using notes and setup duration for them:

int melody[] = {NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4};
int noteDurations[] = {8, 8, 8, 8, 8, 4, 8, 8}; //1-8

And now we can write our main functions:

void setup() 
{
  for (int thisNote = 0; thisNote < 8; thisNote++) 
  {
    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration); //PIN, melody, duration

    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);

    noTone(8);
  }
}

void loop() 
{
}

Done! But if you will use more or less notes, you should change loop settings:
for (int thisNote = 0; thisNote < 8; thisNote++)

Where 8 is an amount of using notes;