1 / 19

Arduino club

Arduino club. What we’re really doing. basics. The setup() and loop() functions. v oid 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

lapis
Download Presentation

Arduino club

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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<stdio.h> inti; 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<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 } }

  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<stdio.h> /* 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<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 } }

  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

More Related