Programming Modules
Once you learned about a module, you usually reuse it without much change. There is danger in using modules that you don't understand, but there is also value in treating parts of your Arduino sketch as modules. This serves as a page to document different modules that are used in the tutorial for a quick reference.
Digital Read/Write
void setup()
{
pinMode(PIN,OUTPUT);
pinMode(PIN2,INPUT);
}
void loop()
{
digitalWrite(PIN,NUMBER);
digitalRead(PIN2); //HIGH & LOW
}
Analog Read/Write
void setup()
{
//No need to set pinMode
}
void loop()
{
analogWrite(PIN,NUMBER);
analogRead(PIN2);
}
Loops
while (True)
{
\\do something
}
for (int i = 0; i < NUMBER; i++)
{
\\do something NUMBER times.
}
Conditionals
if (True)
{
\\do something
}
else if (True)
{
\\do something
}
else
{
\\do something
}
Addressable LEDS
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel pixel = Adafruit_NeoPixel(NUM_OF_LED, PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
pixel.begin();
}
void loop()
{
pixel.setPixelColor(PIXEL_NUMBER,pixel.Color(R,G,B));
pixel.show();
allChange(NUM_OF_LED,R,G,B);
}
def allChange(NUM_OF_LED,r,g,b)
{
for (int i = 0; i < NUM_OF_LED;i++)
{
pixel.setPixelColor(i,pixel.Color(r,g,b));
}
pixel.show();
}