arduino club what we’re really doing. basics the setup() and loop() functions

19
ARDUINO CLUB What we’re really doing

Upload: tyrese-bedwell

Post on 01-Apr-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

ARDUINO CLUBWhat we’re really doing

Page 2: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

BASICSThe setup() and loop() functions

Page 3: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

void setup()• Like the main() you’re used to

• For setting pin modes• pinMode(pinNumber,MODE);

• MODE can be ‘INPUT’ or ‘OUTPUT’• pinNumber is the number of the pin

• Useful to start serial connection here• Serial.begin(9600);

• Only run once, when the arduino board powers on

Page 4: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

void loop()• Also like main()

• This is where you put your actual program

• This loops until the board loses power, or breaks

Page 5: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

CONTROLLING PINSBut I want to make my LED flash! D:

Page 6: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

digitalWrite()• Basically, turns things on and off

• digitalWrite(pinNumber,STATE);• pinNumber can be any pin that was set, in the the setup, to be an

output• STATE can either be ‘HIGH’ or ‘LOW’ (on or off)

Page 7: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

digitalRead()• Checks if inputs are high, or low

• digitalRead(pinNumber)• pinNumber can be any pin that you set as an INPUT in the setup

loop• Will return either HIGH or LOW

Page 8: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

delay()• Pauses the program for a set amount of time

• delay(time);• Time is in milliseconds, so 1 second would be delay(1000)

Page 9: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

WIRINGMake sure you don’t blow things up

Page 10: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

LEDs• Require 220Ω resistor (red, red, brown)

Page 11: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

CONDITIONALS

Page 12: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

"If" StatementsThese allow us to perform an action, if a condition is met, and otherwise perform another action

#include<stdio.h>int i;void main(){

printf("Enter a number! \n");scanf("%d",&i);if (i == 0){

printf("that number was zero \n");

} else {printf("that number was not zero \n");

}}

Page 13: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

Logic Operators• == means equal to• != means not equal to• && means AND• || means OR• > means greater than• < means less than• >= means greater than or equal to• <= means less than or equal to

Page 14: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

Buttons• Need to be connected to ground as well as +volts

Page 15: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

LOOPS

Page 16: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

"While" Loops

These allow us to loop code indefinitely until a condition is met:

#include<stdio.h> //You get the point

int x = 0; //Declares x variable

void main(){ //Starts main

while(x<100){ //Starts while loop with condition

x++; //Increases x by one

printf("%d \n",x); //Prints value of x

}

}

Page 17: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

"For" Loops• Used for performing actions a set amount of times• Also useful for looping through an ARRAY• Started by:

"for (initialization, condition, increment) {(functions in here)}"•Initialization is run only once, and is usually decleration of the condition variable•Condition decides when the for loop will stop, once it is false the loop will be exited•The increment decides how the condition variable will be increased/decreased

#include<stdio.h> /* includes stdio library, necessary */

void main(){ // starts main functionfor (int x = 0; x<100; x++){ // sets how the for loop

will runprintf("%d \n", x); /* prints "x" until it"s

equal to 100, then stops */

}}

Page 18: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

"For" Loops with Arrays

We can also iterate through an array with a for loop, as shown below:

#include<stdio.h> //Necessary

int myNum[2]={1,3}; //Defines array

int arraySize=sizeof(myNum)/sizeof(int); //Works out length of the array

int i=0; //Defines i for iterating

void main(){ //Starts main

for(i=0;i<arraySize;i++){ //Starts for loop

printf("%d \n",myNum[i]); //Outputs each field

}

}

Page 19: ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions

A Note about Iterating• Iterators usually take the form of:

• Variable++• This will increase "Variable" by one every iteration

• Variable--• This will decrease "Variable" by one every iteration

• We aren’t limited to this, an iterator can have any mathematic function applied to it, however these are the most common