Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions."— Presentation transcript:

1 ARDUINO CLUB What we’re really doing

2 BASICS The setup() and loop() functions

3 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

4 void loop() Also like main() This is where you put your actual program This loops until the board loses power, or breaks

5 CONTROLLING PINS But I want to make my LED flash! D:

6 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)

7 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

8 delay() Pauses the program for a set amount of time delay(time); Time is in milliseconds, so 1 second would be delay(1000)

9 WIRING Make sure you don’t blow things up

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

11 CONDITIONALS

12 "If" Statements These allow us to perform an action, if a condition is met, and otherwise perform another action #include 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"); }

13 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

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

15 LOOPS

16 "While" Loops These allow us to loop code indefinitely until a condition is met: #include //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 }

17 "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 /* includes stdio library, necessary */ void main(){ // starts main function for (int x = 0; x<100; x++){ // sets how the for loop will run printf("%d \n", x); /* prints "x" until it"s equal to 100, then stops */ }

18 "For" Loops with Arrays We can also iterate through an array with a for loop, as shown below: #include //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 }

19 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


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

Similar presentations


Ads by Google