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

Preview:

Citation preview

ARDUINO CLUBWhat we’re really doing

BASICSThe 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

void loop()• Also like main()

• This is where you put your actual program

• This loops until the board loses power, or breaks

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

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)

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

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

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

WIRINGMake sure you don’t blow things up

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

CONDITIONALS

"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");

}}

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

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

LOOPS

"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

}

}

"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 */

}}

"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

}

}

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

Recommended