Arduino Coding - Tech W2
In our second workshop for Arduino, it was about the coding side of things and how to correctly write it as well as the different codes and functions that can be used.
- Variables and constants
- Void setup(), void loop() x()
- If, else if, while, for statements
- Always comment your code!!!
- No exceptions // comments start with ‘//’
- You can write anything in a comment
- {} are not () are not []
- Commands need to be closed with ‘;’
- Analog (wave) vs digital (squared wave)
- Analog inputs, analog inputs vary from 0-1023
- E.g. analogRead(potPin); //analog reads the potentiometer position
- Digital inputs/outputs has only two states
- Digital inputs/outputs: 0-1,LOW-HIGH
- digitalRead(buttonPin); //reads the button state
- digitalWrite(ledPin, HIGH); //turns the LED on
- Digital PMW outputs; 0-255
- analogWrite(ledPin, 200); //turns the LED on a specific brightness
- General order of code
- //constant integers (whole numbers)
- const int name = x,
- //this creates an integer called ‘name’ with a constant value of ‘x’
- //constant integers are commonly used to assign components to different pins
- Basic layout, needed to make the fading light example etc
Exercise Activities
Following the example of exercise 1A and 1B in class, we created a simple light example to practice.
The outcome of the code we wrote up, is seen in the following video;
Comments
Post a Comment