INDEX
2024-02-27 16:08 - Arduino Examples https://docs.arduino.cc/built-in-examples/ - going through these till I run out of time Using arduino-cli and a starter kit - going to write notes on anything of note Found some notes on my computer too - put here with comments to explain # Package: arduino-cli arduino-cli update; arduino-cli upgrade # Update cli tool arduino-cli board list # List arduino devices # Setup system (run as root - same user that compiles) arduino-cli config init; arduino-cli core update-index; arduino-cli core install arduino:avr # ./compiler.sh FILE (run as root) arduino-cli compile --fqbn arduino:avr:uno $1 arduino-cli upload -p /dev/ttyACM0 --fqbn arduino:avr:uno $1 # Arduino files are .ino files Right now this is failing because of cdc_adm (kernel module) not loading properly https://wiki.archlinux.org/title/Arduino - can actually allow normal users to use arduino port Regular port is ttyACM0 but need kernel modules working properly Possibly failing because I upgraded system packages without a reboot? Rebooted and ran `modprobe cdc_adm` - works now So when you make an arduino project the folder and .ino file must have the same name I've set the compiler to just use code/code.ino and keep everything in there Now actually get started... Analog Read Serial I did just accidentally put live-ground on the same row and short it - arduino shuts off Potentiometer with out->A0, Left->Gnd and Right->5V # code.ino void setup() { Serial.begin(9600); } void loop() { int sensorValue = analogRead(A0); # Read pin0 Serial.println(sensorValue) delay(1); } # Run compiler (compiles and uploads) arduino-cli monitor -p /dev/ttyACM0 # Live monitor of arduino tty # Rotating dial changes value displayed Blink This blinks the internal light by writing directly to it (no circuit) pinMode(LED_BUILTIN, OUTPUT) # Enables pin13 for output (internal LED in the Uno) digitalWrite(LED_BUILTIN, HIGH) # Can also set to LOW To use an LED from the circuit, connect live to a digital out pin Add 220 ohm resistor (Y-Br-R-Go) in series with LED (long leg to positive) pinMode(12, OUTPUT) # Use pin 12 I don't think in this case a resistor is required but visibly makes it dimmer Digital Read Serial Potentiometer task used analog input - this uses digital (1/0) states Connect a switch to pin2 (digital pin) Then ground -> 10K ohm resistor (Br-Bl-Or-Go) -> switch (same number/column as ground) Then live -> switch (other side) pinMode(2, INPUT); int buttonState = digitalRead(2); I keep having to remind myself that the rows are connected, especially with a button So if the button crosses the middle, the two sets of rows (e.g. row25) are connected It's the top and bottom of the switch that are separated by the button Fading a LED Gnd -> 220ohm -> LED -> Pin9 analogWrite(9, brightness) # Set pin9 to the value of variable "brightness" # Change brightness over time brightness = brightness + fadeAmount; if (brightness <= 0 || brightness >= 255) fadeAmount = -fadeAmount; Fades in brightness up and down - just changes directions at end points Read Analog Voltage Just takes analog input and calculates voltage from that - same setup as "analog read serial" float voltage = sensorValue * (5.0 / 1023.0); # Now turning the dial gives a voltage reading - 3.3v input at max seems to be 3.27 # If you reverse live/grnd you get opposite readings (e.g. 5->0 and 0->5) Add a resistor between the potentiometer and the live and it reduces the max voltage Can use this to test effect of resistors - in series they lower by a lesser degree Blink without Delay How can you do multiple things at once? e.g. respond to buttons while waiting DigitalOut -> 220ohm -> LED -> Grnd const # Set values that don't change, like the pin number unsigned long previousMillis = 0 # Store time in unsigned long (time since last LED update) unsigned long currentMillis = millis() # Essentially instead of sleeping, count the time difference and act if time>threshold # Test "current - previous" and then do "previous=current" - like a datetime stamp compare # "millis" measures milliseconds - so desired interval is in milliseconds too Write and program a button Live -> switch -> pin2 + 10Kohm -> Grnd (default is LOW) This specifically links the internal LED to switch state - pin2 (switch state) Nothing is special in the code, just that switch connects to led via arduino Going to try using a potentiometer and a threshold to see waht that does const int ledPin = 13; const float voltThr = 1.5; void setup() { pinMode(ledPin, OUTPUT); } void loop() { int sensorValue = analogRead(A0); float voltage = sensorValue * (5.0 / 1023.0); if (voltage >= voltThr) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } } I could add another potentiometer to set the thresold Now how I've set this up the top potentiometer seems to have higher voltage Oh I'm being stupid - these are 2 different resistors - was certain this circuit was parallel So the circuit is just 2 (resistor + potentiometer) in parallel - pointing at A0 and A1 const int ledPin = 13; void setup() { pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { int ledInput = analogRead(A0); int thrInput = analogRead(A1); float ledVoltage = ledInput * (5.0 / 1023.0); float thrVoltage = thrInput * (5.0 / 1023.0); Serial.print("["+String(ledVoltage)+"/"+String(thrVoltage)+"] "); if (ledVoltage >= thrVoltage) { digitalWrite(ledPin, HIGH); Serial.println("ON"); } else { digitalWrite(ledPin, LOW); Serial.println("OFF"); } delay(100); } You can change the dials to effectively change the voltage limits for ON/OFF Would be a nice idea to add another one so you can set lower and upper limits with a free mid
2024-02-28 14:30 - Arduino Examples Debounce on a Pushbutton Same basic setup - switch + resistor -> lined into pin2 No code is that exciting just changes LED state when button is pressed (like a switch) But uses a delay (50mil) and a timer to not change within a certain time after button press Can otherwise cause a flicker Input Pullup Serial Can link switch directly to pin2 (Grnd -> Switch -> Pin2) INPUT_PULLUP is INPUT with a default HIGH state - so transfers some power output pinMode(2,INPUT_PULLUP); int SensorValue = digitalRead(2) # Gives a 1 or 0 This code just turns the light on at a button press - but using only the pin's power State Change Detection Again nothing interesting just counts when state is changed from LOW->HIGH and lights on 4s Keyboard using tone() This one seems more involved: Analog->(10Kohm->Grnd)+(sensors->live5v) x3 Then Pin8->100ohm->Spkr->Grnd 100ohm=(Br+Bl+R+Go), 10K=(Br+Bl+Or+Go) I don't have force sensing resistors for this so I'll try with switches I'll try analog first but may have to switch to digital #include "pitches.h" # Allows you to import a header file with definitions of notes Okay using a red LED as a voltage test is fun - basically don't read from digital pin1 It seems to always be on, for some reason. Now we've got buttons linked to outputs: for (int thisSensor = 2; thisSensor <= 4; thisSensor++) { int sensorReading = digitalRead(thisSensor); if (sensorReading == HIGH) { Serial.println(String(thisSensor)+" - "+String(sensorReading)); } } I've linked up what I THINK is a speaker (now) - seems to make sounds on button presses tone(8, notes[thisSensor], 20); Okay let's move back to analog - remember to change to 0-2 analogRead Now it's sounding much more like the notes I was expecting So what if I use potentiometers here instead? Seems to work int threshold = 190; void setup() { Serial.begin(9600); } void loop() { int sensors[] = {0,0,0}; for (int i = 0; i <= 2; i++) { sensors[i] = (analogRead(i)-300); if (sensors[i] >= threshold) { tone(8, sensors[i]+(1000*i), 20); } Serial.print("["+String(i)+" = "+String(sensors[i])+"]"); } Serial.println(); } I've got potentiometers linked to A0-2 feeding back to digital 8 -> speaker One thing of interest is LEDs seem to have a threshold for turning on properly - changes stuff Tone duration also seems to impact the sound more than I'd thing - duration=60 is solid But overlapping tones isn't as clean as it could be - gives some interference