Mailbag #37 from "Downtown Tasmania"
A really eclectic mix of weird and wonderful in these bags - including some parts that I completely mislabel, and some other parts that defy description.
Enjoy the madness!
Each week I will feature a circuit based on one or more of the electronic parts I have ordered then forgotten about over the years.
A really eclectic mix of weird and wonderful in these bags - including some parts that I completely mislabel, and some other parts that defy description.
Enjoy the madness!
I'm still fiddling with the code (current version below) and the layout of the famous PFS154 solar powered candle.
/* Candle with Three PWM Pseudo-random flickering to simulate a candle. Output is via 3xPWM channels, variables can be changed to alter the simulation Tue 16 Jan 2024 13:50:35 AEDT */ #include <stdint.h> #include <stdlib.h> #include "../device.h" #include "../easy-pdk/calibrate.h" #include "../auto_sysclock.h" #include "../delay.h" #include <stdbool.h> #define LED4_BIT 4 #define LED0_BIT 0 #define LED3_BIT 3 uint16_t myrand = 2901; // happy birthday uint8_t slowcounter = 0; uint8_t medcounter = 0; uint8_t fastcounter = 0; uint8_t slowstart = 0; uint8_t slowend = 0; uint8_t medstart = 0; uint8_t medend = 0; uint8_t faststart = 0; uint8_t fastend = 0; uint8_t faster = 0; uint8_t waveslow[] = {15, 25, 60, 100}; uint8_t wavemed[] = {10, 25, 110, 140}; uint8_t wavefast[] = {20, 25, 100, 120}; bool fastup = true; bool slowup = true; bool medup = true; void mydelay(uint8_t counter) { for (uint8_t thiscount = 0; thiscount <= counter; thiscount++) { _delay_us(1); } } uint16_t gimmerand(uint16_t small, uint16_t big) { myrand ^= (myrand << 13); myrand ^= (myrand >> 9); myrand ^= (myrand << 7); if (abs(myrand) % 13 == 0) { myrand = myrand - 23; } if (abs(myrand) % 17 == 0) { myrand = myrand + 11; } return abs(myrand) % 23 * (big - small) / 23 + small; } void getnewslow() { slowstart = gimmerand(waveslow[0], waveslow[1]); slowend = gimmerand(waveslow[2], waveslow[3]); } void getnewmed() { medstart = gimmerand(wavemed[0], wavemed[1]); medend = gimmerand(wavemed[2], wavemed[3]); } void getnewfast() { faststart = gimmerand(wavefast[0], wavefast[1]); fastend = gimmerand(wavefast[2], wavefast[3]); faster = gimmerand(2, 6); } // Main program void main() { PAC |= (1 << LED4_BIT) | (1 << LED0_BIT) | (1 << LED3_BIT); // see datasheet PWMG1DTL = 0x00; PWMG1DTH = 0x00; PWMG1CUBL = 0xff; PWMG1CUBH = 0xff; PWMG1C = 0b10100111; PWMG1S = 0b00000000; PWMG0DTL = 0x00; PWMG0DTH = 0x00; PWMG0CUBL = 0xff; PWMG0CUBH = 0xff; PWMG0C = 0b10100111; PWMG0S = 0b00000000; PWMG2DTL = 0x00; PWMG2DTH = 0x00; PWMG2CUBL = 0xff; PWMG2CUBH = 0xff; PWMG2C = 0b10100111; PWMG2S = 0b00000000; getnewfast(); getnewslow(); getnewmed(); slowcounter = slowstart; fastcounter = faststart; medcounter = medstart; // Main processing loop while (1) { // ramp up slow if (slowup) { slowcounter++; if (slowcounter > slowend) { // ramp finished so switch boolean slowup = !slowup; } } else { // ramp down slow slowcounter--; if (slowcounter < slowstart) { // ramp finished so switch boolean slowup = !slowup; getnewslow(); } } // ramp up med if (medup) { medcounter++; if (medcounter > medend) { // ramp finished so switch boolean medup = !medup; } } else { // ramp down med medcounter--; if (medcounter < medstart) { // ramp finished so switch boolean medup = !medup; getnewmed(); } } // ramp up fast if (fastup) { fastcounter = fastcounter + faster; if (fastcounter > fastend) { // ramp finished so switch boolean fastup = !fastup; } } else { // ramp down fast fastcounter = fastcounter - faster; if (fastcounter < faststart) { // ramp finished so switch boolean fastup = !fastup; getnewfast(); } } // delay + a re-purposed random for ramp speeds mydelay(25 + faster); PWMG2DTL = slowcounter & 255; PWMG2DTH = slowcounter; PWMG0DTL = fastcounter & 255; PWMG0DTH = fastcounter; PWMG1DTL = medcounter & 255; PWMG1DTH = medcounter; } } // Startup code - Setup/calibrate system clock unsigned char _sdcc_external_startup(void) { CLKMD = CLKMD_ILRC | CLKMD_ENABLE_ILRC | CLKMD_ENABLE_IHRC; CLKMD = CLKMD_ILRC | CLKMD_ENABLE_ILRC; PDK_SET_SYSCLOCK(SYSCLOCK_ILRC); EASY_PDK_CALIBRATE_ILRC(47000,4000); return 0; }
Check out 5 new "candles" straight off the production line in the following video:
I upgraded my NAS Media Centre from an Orange Pi PC Plus to an Orange Pi 3 LTS, both running OpenMediaVault (OMV).
Apart from the usual configuration SNAFUs and other delights in such a move, I was a tad concerned about the reported CPU temperature on the new machine.
Logging into the new Orange Pi 3 LTS and using HTOP I consistently saw over 50 degrees Celsius - about 20 degrees higher than the Orange Pi PC Plus running Orange Pi's own Armbian image.
It's inevitable I surmise that with higher performance comes higher temperatures, but a little online chatter on the subject of software differences sent me on a typical OneCircuit rabbit hole expedition where I compared six(!) OS options based on temperature profile.
All images were homemade roll-yer-own Armbian based along the following lines:
Orange Pi PC Plus: Ubuntu Jammy, Debian Bullseye and Debian Bookworm
Orange Pi 3 LTS: Ubuntu Jammy, Debian Bullseye and Debian Bookworm
All images also were CLI only (no desktop) and I used the same micro SD card and ambient temperature for all tests - no fans employed but the chips did have heat sinks onboard.
If you want me to do a deeper dive on building your own Armbian images, or configuring OMV please leave a comment on YouTube (video link below).
The results of all this amateur science? Surprising!
I recommended to peeps at work that they buy a bulk order (10pcs) of a cheap ESP32 module that the Science Dept can use for projects (I'm a high school teacher as well as a technology junky!)
Instant success as they applied one to a watering project - no IoT as yet but a start in the right direction.
Well I was less than impressed that two of the 10 did not work - I'd never had that trouble before with AliExpress orders so I felt duty bound to take the ailing items home for a little amateur surgery.
At the same time in the process of...er...experimentation with Arduino Pro Mini units trying to squeeze more power out of them for my KimUno project (blog and video) I had fried two of the unlucky blighters. More amateur surgery required?
You be the judge!
Well after the shocking reviews for my last two videos I've decided to do the old favourite - a mailbag!
Algorithm alone knows these are so popular, but as I'm behind on opening up my late night purchases I'd better get to it!
The only extra bit is the code I had to write for the two joystick Arduino module - I couldn't find anything online and so I cobbled together the following - which works fine.
/* * Found this twin joystick module online here: * https://www.aliexpress.com/item/1005001882246352.html * * ..but with no code I had to find out the pins for * input and output, then made the code below to demonstrate * the use of the module - enjoy! * * Exponential function provided by BARD * https://bard.google.com/ * * OneCircuit - https://www.youtube.com/@onecircuit-as * Tue 26 Dec 2023 16:28:52 AEDT */ #define LjoyXaxis_pin A0 #define LjoyYaxis_pin A1 #define RjoyXaxis_pin A3 #define RjoyYaxis_pin A2 #define LSW_pin 2 #define RSW_pin 4 #define LLEDPin 9 #define RLEDPin 10 int LYLED = 0; int RYLED = 0; boolean LButton = 0; boolean RButton = 0; int exponentialMap(int S) { float base = 3.0; float normalizedS = S / 1023.0; float L = pow(normalizedS, base) * 255.0; return constrain(L, 0, 255); } void setup() { pinMode(LjoyXaxis_pin, INPUT); pinMode(LjoyYaxis_pin, INPUT); pinMode(RjoyXaxis_pin, INPUT); pinMode(RjoyYaxis_pin, INPUT); pinMode(LSW_pin, INPUT); pinMode(RSW_pin, INPUT); pinMode(LLEDPin, OUTPUT); pinMode(RLEDPin, OUTPUT); digitalWrite(LSW_pin, HIGH); digitalWrite(RSW_pin, HIGH); Serial.begin(115200); } void loop() {// Left joystick routinesSerial.print("LX-axis:"); Serial.print(analogRead(LjoyXaxis_pin)); Serial.print(":"); LButton = 1 - digitalRead(LSW_pin); LYLED = 1023 - analogRead(LjoyYaxis_pin); Serial.print("LY-axis:"); Serial.print(LYLED); Serial.print(":"); if (LButton == 0) { // LYLED = map(LYLED, 0, 1023, 0, 255); // linear map // LYLED = pow(1.0054,LYLED); // exponential map LYLED = exponentialMap(LYLED); analogWrite(LLEDPin, LYLED); } else { analogWrite(LLEDPin, 0); } Serial.print("LSW:"); Serial.print(LButton);
// Right joystick routines Serial.print(" RX-axis:"); Serial.print(analogRead(RjoyXaxis_pin)); Serial.print(":"); RButton = 1 - digitalRead(RSW_pin); RYLED = 1023 - analogRead(RjoyYaxis_pin); Serial.print("RY-axis:"); Serial.print(RYLED); Serial.print(":"); if (RButton == 0) { // RYLED = map(RYLED, 0, 1023, 0, 255); // linear map // RYLED = pow(1.0054,RYLED); // exponential map RYLED = exponentialMap(RYLED); analogWrite(RLEDPin, RYLED); } else { analogWrite(RLEDPin, 0); } Serial.print("RSW:"); Serial.println(RButton);
delay(100);
}
Thank you to BARD for the exponential mapping function - increase "float base = 3.0;" for a flatter curve.
User: Can you suggest an Arduino function that maps
numbers 0-1023 (S: from a joystick source)
to 0-255 (L: for an LED output), but instead of
mapping linearly it maps exponentially so that
values of S such as 300 map quite low to, for
instance, to an L value of 5, and a value ofL=600 maps to, for instance, S=25? Bard: Here's an Arduino function that maps
numbers 0-1023 to 0-255 exponentially,
as you described: Arduino int exponentialMap(int S) { // Adjust this base value to fine-tune the curve float base = 2.0; // Normalize S to a range of 0.0 to 1.0 float normalizedS = S / 1023.0; // Apply exponential curve using pow() function float L = pow(normalizedS, base) * 255.0; // Clamp L to 0-255 range to ensure valid output return constrain(L, 0, 255); } Use code with caution. Learn more Explanation: Normalization: The normalizedS calculation ensures
the input value S is within a 0.0 to 1.0 range,
making it suitable for exponential calculations.
Exponential Mapping: The pow(normalizedS, base) expression applies the exponential curve. The base
variable controls the steepness of the curve: Higher base values result in a more pronounced
exponential response.
Lower base values lead to a more gradual curve.
Scaling: The result of pow() is multiplied
by 255.0 to scale it back to the 0-255 range
for LED output. Clamping: The constrain(L, 0, 255) function
ensures the final output value L stays within
the valid 0-255 range, even if the calculation
produces a value outside this range. Usage: Arduino int S = analogRead(joystickPin); // Read joystick value int L = exponentialMap(S); // Apply exponential mapping analogWrite(ledPin, L); // Set LED brightness Use code with caution. Learn more Customization: Experiment with different base values to achieve
the desired mapping curve.
Consider adjusting the normalization and scaling
factors if your input or output ranges differ. User: Works beautifully - well done! 👏👏👏 Bard: I'm glad it works well for you! Thank you for
the appreciation, it makes helping fun! If you
have any other questions or challenges related
to Arduino or code, feel free to ask. I'm always happy to assist.
And here is the video - enjoy!
My first attempt at programming was a Lunar Lander game back in 1978. I used an Apple II which had arrived at my school and then was promptly stored in the cleaner's room (!!) to gather dust.
I used to be let in after school and on weekends to try to make this thing work. Fortunately the manuals were great and pretty soon I had written a BASIC Lunar Lander.
Wouldn't it be nice to go back to those times and get enjoyment from a game that mostly happens in your head?
Well the successful building of a KimUno recently (video and blog) has meant an opening to go back to the future and play with these games/applications again.
I hadn't been too successful trying to shove code into the memory available, due in no small part to the byzantine memory map of this replica.
But I did think I could succeed if I made a ROM and programmed the game at the source, straight into the Arduino Pro Mini that drives the whole deal. These are the steps I thought might result in a Lunar Lander on a KimUno.
1. Find a good Lunar Lander written in 6502 style for the Kim1.2. Use an assembler to make the Hex code for the game (see below).3. Convert the Hex code to the array format used by the KimUno code with the help of my old friend Nedit (xNedit).4. Change the roms.h, cpu.c and config.h files of the KimUno project code to insert the game ROM at $3000.5. Compile and upload to the Arduino Pro Mini6. Practise my landings.
Here's the Hex code if you wish to try it yourself!
3000: A2 0D BD CC 30 95 D5 CA3008: 10 F8 A2 05 A0 01 F8 183010: B5 D5 75 D7 95 D5 CA 883018: 10 F6 B5 D8 10 02 A9 993020: 75 D5 95 D5 CA 10 E5 A53028: D5 10 0D A9 00 85 E2 A23030: 02 95 D5 95 DB CA 10 F93038: 38 A5 E0 E5 DD 85 E0 A23040: 01 B5 DE E9 00 95 DE CA3048: 10 F7 B0 0C A9 00 A2 033050: 95 DD CA 10 FB 20 BD 303058: A5 DE A6 DF 09 F0 A4 E13060: F0 20 F0 9C F0 A4 A2 FE3068: A0 5A 18 A5 D9 69 05 A53070: D8 69 00 B0 04 A2 AD A03078: DE 98 A4 E2 F0 04 A5 D53080: A6 D6 85 FB 86 FA A5 D93088: A6 D8 10 05 38 A9 00 E53090: D9 85 F9 A9 02 85 E3 D83098: 20 1F 1F 20 6A 1F C9 1330A0: F0 C0 B0 03 20 AD 30 C630A8: E3 D0 ED F0 B7 C9 0A 9030B0: 05 49 0F 85 E1 60 AA A530B8: DD F0 FA 86 DD A5 DD 3830C0: F8 E9 05 85 DC A9 00 E930C8: 00 85 DB 60 45 01 00 9930D0: 81 00 99 97 02 08 00 0030D8: 01 01 00 00 00 00 00 00
And here is the video showing the final project and all the little funny bits along the way!
You know those moments when you are stuck thousands of kilometres from your home (computer) and you need to change the code on an ESP32 chugging along on doing some ground breaking science?
Well in that situation, how would you update the code? How might you download any stored data? How is it possible for you upload a new file (e.g. the html interface) to the LittleFS file system onboard?
I have thought about this quite a bit, and in fact I've chatted with ChatGPT about this over a few months now, and here is what I/we've come up with:
Further to this idea, it's not fun to look in one single large code file to make the necessary changes, it is better to have the code as modular as possible and only find/change what is necessary. Here's another picture to help explain this aspect.
1. A deeper dive into the code including some exploration of how both cores of the ESP32 are used (one for the main.h code and one for the OTA update code)2. A look at security issues including LittleFS encryption and password management.3. Error checking on file uploads/deletions and partition management5. Binary production and distribution6. A discussion about what happened to point 4, above.
All the code is on the github site for you to download and play with - have at it!
So now let's dive into the video and maybe come back in a 2nd and maybe 3rd episode/post for all the juicy deets.