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'.
Tuesday, December 17, 2013
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.
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'.
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;
}
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:
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
}
}
OK, and now code:
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
}
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;
//...
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};
//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:
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;
Friday, November 22, 2013
How to control termometer in Arduino
Today I'm going to show you how to control termometer. Firstly you will need to connect terometer to Arduino.
Now open your arduino! =)
void setup() //setup function
{
Serial.begin(9600); //install Serial output
}
int checkTemp() //read temperature function
{
float voltage = 0; //float variable for volts
float celsius = 0; //float variable for celsius
float sensor = 0; //float variable for sensor
sensor = analogRead(0); //read from analog input 0
voltage = (sensor * 5000) / 1024; // convert raw sensor value to millivolts
voltage = voltage - 500; // remove voltage offset
return (voltage / 10); // convert millivolts to Celsius and return result
}
void loop() //loop function
{
Serial.println(checkTemp()); //print result of checkTemp() function
}
Ok, now upload your code. And press tools->"Serial Monitor" or Ctrl + Shift + M. Done!
void setup() //setup function
{
Serial.begin(9600); //install Serial output
}
int checkTemp() //read temperature function
{
float voltage = 0; //float variable for volts
float celsius = 0; //float variable for celsius
float sensor = 0; //float variable for sensor
sensor = analogRead(0); //read from analog input 0
voltage = (sensor * 5000) / 1024; // convert raw sensor value to millivolts
voltage = voltage - 500; // remove voltage offset
return (voltage / 10); // convert millivolts to Celsius and return result
}
void loop() //loop function
{
Serial.println(checkTemp()); //print result of checkTemp() function
}
Ok, now upload your code. And press tools->"Serial Monitor" or Ctrl + Shift + M. Done!
Thursday, November 14, 2013
Plans for future
On robotics lesson we have started out own projects. My group and me decided to create Samon Says game. Now we are waiting for items to build our game and pretty soon we will start writing source code for our game. We have got alot of ideas how to modifie this game and make it as much as it possible better.
When we will finish our project i will publish about it in my blog!
P.S. If you too have got some ideas, which will be interesting to apply to our project - don't be shy! Share them with me!
Robatics convention
We have been on robotics alley convention yesterday (11.13.13). There we have seen alot of different robots and ideas, which will be very interesting to realize in future for us. There we have seen such robots as baseball player robot, Frisbee thrower robot, football player robot. For me the most interesting robot was robot, which was scanning table with full bunch of sweets and give you one with flavour you have chosen.
We have earned alot of ideas which will be very useful and interesting realize for us.
We have earned alot of ideas which will be very useful and interesting realize for us.
Sunday, November 3, 2013
Arduino coding and functions
What is functions? Why do we need to use them? Now I will explain it to you!
OK, let's with little introduction. I have found website with very good explanation, if you want to learn more about them, it will be good idea to visit it.
We need to use functions to make our program easyer and more complicated.
Syntax of functions:
type_of_function name_of_function(variables(input) if needed)
{
//do staff
return value; //if needed
}
And now, how to call(execute it):
Form another function.
void loop()
{
name_of_function(variables(input) if needed);
}
Now I will show you a little example:
void setup()
{
digitalPin(13, OUTPUT);
}
void bLED()
{
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
}
void loop()
{
bLED();
}
If you have got some questions, fell free to ask me.
OK, let's with little introduction. I have found website with very good explanation, if you want to learn more about them, it will be good idea to visit it.
We need to use functions to make our program easyer and more complicated.
Syntax of functions:
type_of_function name_of_function(variables(input) if needed)
{
//do staff
return value; //if needed
}
And now, how to call(execute it):
Form another function.
void loop()
{
name_of_function(variables(input) if needed);
}
Now I will show you a little example:
void setup()
{
digitalPin(13, OUTPUT);
}
void bLED()
{
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
}
void loop()
{
bLED();
}
If you have got some questions, fell free to ask me.
Thursday, October 24, 2013
Project 4 - modernization
We have finished (Caleb and me) our project 4 (modernization)
Algorithm:
Explonations: Firstly program will generate random number and will turn on LED, next, what will do this program is checking in the loop which button was pressed. Then move users LED to the correct side and compare: are random LED is on the same position with user LED, if yes - generate new random number and change LED.
Sketch:
// Leds for user
#define LEDU1 8 //Left one
#define LEDU2 7
#define LEDU3 6
#define LEDU4 5
#define LEDU5 4 //Right one
//Leds for random
#define LEDR1 13 //Left one
#define LEDR2 12
#define LEDR3 11
#define LEDR4 10
#define LEDR5 9//Right one
#define BUTTON1 3 //left //Buttons
#define BUTTON2 2 //right
#define MinLED (LEDU5)
#define MaxLED (LEDU1)
int posU = 0; //position of user's led
int posR = 0; //position of random's led
void setup()
{
pinMode(LEDU1, OUTPUT); //SetUp user LED outputs
pinMode(LEDU2, OUTPUT);
pinMode(LEDU3, OUTPUT);
pinMode(LEDU4, OUTPUT);
pinMode(LEDU5, OUTPUT);
pinMode(LEDR1, OUTPUT); //SetUp random LED outputs
pinMode(LEDR2, OUTPUT);
pinMode(LEDR3, OUTPUT);
pinMode(LEDR4, OUTPUT);
pinMode(LEDR5, OUTPUT);
pinMode(BUTTON1, INPUT); // input for the button
pinMode(BUTTON2, INPUT); // input for the button
digitalWrite(LEDU1, HIGH);
posR = random(5);
digitalWrite(LEDR1, LOW);
digitalWrite(LEDR2, LOW);
digitalWrite(LEDR3, LOW);
digitalWrite(LEDR4, LOW);
digitalWrite(LEDR5, LOW);
if(posR == 0)
{
digitalWrite(LEDR1, HIGH);
}
else if(posR == 1)
{
digitalWrite(LEDR2, HIGH);
}
else if(posR == 2)
{
digitalWrite(LEDR3, HIGH);
}
else if(posR == 3)
{
digitalWrite(LEDR4, HIGH);
}
else if(posR == 4)
{
digitalWrite(LEDR5, HIGH);
}
compare();
}
void loop()
{
if(digitalRead(BUTTON1) == HIGH) //move left
{
if(posU == 0)
{
posU = 4;
}
else
{
posU--;
}
digitalWrite(LEDU1, LOW);
digitalWrite(LEDU2, LOW);
digitalWrite(LEDU3, LOW);
digitalWrite(LEDU4, LOW);
digitalWrite(LEDU5, LOW);
if(posU == 0)
{
digitalWrite(LEDU1, HIGH);
}
else if(posU == 1)
{
digitalWrite(LEDU2, HIGH);
}
else if(posU == 2)
{
digitalWrite(LEDU3, HIGH);
}
else if(posU == 3)
{
digitalWrite(LEDU4, HIGH);
}
else if(posU == 4)
{
digitalWrite(LEDU5, HIGH);
}
}
//-------------------------------------------------------------
if(digitalRead(BUTTON2) == HIGH) //move right
{
if(posU == 4)
{
posU = 0;
}
else
{
posU++;
}
digitalWrite(LEDU1, LOW);
digitalWrite(LEDU2, LOW);
digitalWrite(LEDU3, LOW);
digitalWrite(LEDU4, LOW);
digitalWrite(LEDU5, LOW);
if(posU == 0)
{
digitalWrite(LEDU2, HIGH);
}
else if(posU == 1)
{
digitalWrite(LEDU3, HIGH);
}
else if(posU == 2)
{
digitalWrite(LEDU4, HIGH);
}
else if(posU == 3)
{
digitalWrite(LEDU5, HIGH);
}
else if(posU == 4)
{
digitalWrite(LEDU1, HIGH);
}
}
//---------------------------------------------------------------
compare();
delay(200);
}
int compare()
{
if(posU == posR)
{
posR = random(5);
digitalWrite(LEDR1, LOW);
digitalWrite(LEDR2, LOW);
digitalWrite(LEDR3, LOW);
digitalWrite(LEDR4, LOW);
digitalWrite(LEDR5, LOW);
if(posR == 0)
{
digitalWrite(LEDR1, HIGH);
}
else if(posR == 1)
{
digitalWrite(LEDR2, HIGH);
}
else if(posR == 2)
{
digitalWrite(LEDR3, HIGH);
}
else if(posR == 3)
{
digitalWrite(LEDR4, HIGH);
}
else if(posR == 4)
{
digitalWrite(LEDR5, HIGH);
}
return 1; //return 1 if user win
}
return 0; //return 0 if user lose
}
Schematic:
How it works:
P.S. Sorry for bad quality of video...
Sunday, October 13, 2013
Robotics private top secret chat!
Hello 2 everyone! I decided to code something interesting for our robotics team and i made private top secret chat. You can download it here. It is very easy to use it!
After you have downloaded it. Create a directory and put this program into new directory (that's because program will download temporary files and it will make your desktop "dirty"). After that you can open a program. Here you can see main menu of my program:
After you have downloaded it. Create a directory and put this program into new directory (that's because program will download temporary files and it will make your desktop "dirty"). After that you can open a program. Here you can see main menu of my program:
Pretty easy, isn't it? In main menu you can see 5 options:
- Login
- Register a new account
- Information about program
- Options
- Exit
First thing, which you need to do - check program for updates! To do that you need to go to the "options" and then press "Check program for updates".
Then press "1". Program will do everything by itself.
Now you will need to Register a new account, so you will need to press "2" and then "Enter".
And you will see this page:
Here you will need to create your unique account nickname. And later your password and confirmation of it. After you have created a new account you will need to wait for activation (from 1 hour to 1 day).
After activation you will be able to login into the program. Each time, program will check itself for updates.
After authorization you will see..
- Chat
- Usefull files
- Options
- Log out
In useful files you will be able to find our arduino pdf book, eagle pcb installer and a lot of another useful things.
And of course! Chat! You can write messages! It is awesome!
Some information about my program:
- c0ded by Kirill (11moon11) 2013
- c0d3d on C++
- 1224 lines of code
- I spent 17 hours to create this program (it is only for v1.0, now you are able to download v1.6)
Subscribe to:
Posts (Atom)