r/arduino • • Sep 09 '26

Mod's Choice! Interesting Arduino Behavior - Variables not in Context

Post image
4 Upvotes

I was doing a live debugging session and found an interesting behavior with variables. I'd declared this new structure, GameState, before I called it in any of my functions - standard C practice. However, Arduino C has a "helpful" feature where it will create a prototype of your function at the beginning of your file, right before the setup runs. So the structure ended up actually being defined /after/ it was called, which stopped the code from compiling. The solution was to move all my variable definitions to the beginning of the file.

This is kind of a pain, because I'm trying to build a template for an Arduino sketch that has a few MQTT topics built in, then you can build user code that makes use of those topics to communicate with an external device like your phone. I was intending for all the user code to end up at the end of the file in one place, but it looks like I'll have to chunk it out so that the end user can put their stuff in the right order. Of course, the real solution will be to put all this infrastructure into a header file and library, but one step at a time...

Anyway, if you're interested in the debugging session, it's here. You can also see how someone might use Claude to help them fix code. (And very importantly, that using Claude does not exempt you from needing to test your code!)

https://youtu.be/6yW2u79d7Ls?si=dRZP8xgChvEna6ok

If that sketch sounds handy, you can find a copy on this github. It's not finished yet, but I should be done testing it and updating it in a few days.

https://github.com/DocCarr/sproutsodyssey.git


r/arduino • • Sep 08 '26

Hardware Help Need help in DIY pc hardware monitor newbie here

Thumbnail
gallery
8 Upvotes

Hey guys,

i need a help bulid saw someone video on fb but he didnt mention any kind of details DIY PC Hardware Monitor using an ESP8266 kind of and data from Libre Hardware Monitor. Right now, I have a tiny smart weather clock display connected via USB, but honestly, it looks too basic and small. i have 0 idea how these thing work saw some github method i couldnt do that somehow lol so far i setup that clock for now it is connected with wifi and usb too

i want to set my display show these hardware on display

  • CPU Temperature & Usage (%)
  • GPU Temperature & Usage (%)
  • RAM Usage (%)

r/arduino • • Sep 09 '26

Smart irrigation project: designing for graceful failure rather than maximum automation

4 Upvotes

I'm developing the concept for a small smart-irrigation controller as part of a much larger community/farm project.

Rather than trying to make the first version as sophisticated as possible, I'm taking the opposite approach: deciding what the system should still be capable of doing when parts of the automation stop working.

The current project strategy is:

  • normal irrigation remains local rather than cloud-dependent;
  • there is a genuine physical/manual fallback;
  • moisture sensors influence decisions but are not treated as unquestionable truth;
  • weather information is optional optimisation rather than a requirement;
  • water flow can eventually be checked against what the controller thinks a valve is doing;
  • losing networking or higher-level automation should reduce capability, not stop irrigation altogether.

So the intended progression is roughly:

manual irrigation → reliable local automation → sensor-assisted decisions → optional higher-level optimisation

rather than beginning with a connected “smart” system and working backwards.

I'm interested in the project-level design philosophy rather than troubleshooting a particular component at this stage.

For people who have built automated watering or similar outdoor control projects:

Would you also design the project around graceful degradation from the beginning, or is this adding unnecessary complexity to what should initially be a simple irrigation controller?

I'm particularly interested in whether people who have lived with these systems for a while ended up valuing simplicity and manual fallback more or less than they expected.


r/arduino • • Sep 08 '26

Windows "Error: unable to open port COM4 for programmer wiring" when uploading code with Arduino IDE

1 Upvotes

This problem has been affecting me for months and I am already tired of it. I have used multiple versions of Arduino IDE and made sure that everything is closed, and it never worked.

The only thing I can do when uploading my code or opening the serial monitor (That gives the error Port bussy) is to upload multiple times until one of them doesn't give error, that doesn't guarantee that the next time it will work.

I have tried downloading different versions, made sure to not have anything else open that could block the port, made sure that the board information is correct, used different COMs and boards, having the serial monitor closed when uploading, reinstaling drivers...

I don't know if this information is required but:
-I use Windows 11

-I use either a MEGA or a UNO


r/arduino • • Sep 07 '26

Hardware Help Is there a way to connect my digispark attiny85 board to a usb 3.x port ?

Post image
63 Upvotes

r/arduino • • Sep 08 '26

What is the best way to learn Arduino code?

8 Upvotes

Hello, I was wondering what is the best way to learn arduino code? I made some arduino projects such as LED stuff and a sound board but I had a lot of help from chatgpt. I would like to be able to write arduino code myself while I build more projects and I would like to hear how others learn arduino code. Any advice would help, thank you


r/arduino • • Sep 07 '26

Arduino / ESP32-C6 - sealed with the battery and charging

3 Upvotes

The "project ":

One of my most favourite thing I have for sports, exercise etc is hand counter (https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSpSqnGN7uLmd2I1BlNgs-k92gc8nGvMrVM9z_IpER8Z4GeUAlNJVcZ2u4&s=10) it works amazingly for my purpose. Except one - water, it is rotting, it is not flowing etc.

So my idea is to do some device that will just count, and of course if I will use Arduino and display I will add additional functions as well. I am a software developer with AI etc, so I do not see a problem with that, I also can do CAD etc - so mechanically I will solve it too but I am weak in electronic.

The question.

Since I want to have it waterproof and fully enclosed (no screws, no o-rings), it needs to have power source INSIDE + I have to be able to CHARGE it.

As I understand it, if I go for Arduino / ESP32-C6 I cannot just connect 1S LIPO to it, but I need some boost/step-up to have 5V from 1S LIPO. Not sure about Arduino, seems some have some stabilizer. And on top of that I have two options :

Another circuit that will support charging LIPO while connected or alternatively I have to have it connected via reed switch (or something similar) and disconnect when charging and BMS will be outside.

Looking for some cheap and easy solution - preferably something that will work itself.

What I found : WPM403 [Step-up 2.5-5V till 5V 0.6A USB 2-pack]

Idea is

BATTERY 1S --> REED NOC -> WPM403 -> Arduino / ESP32-C6
BATTERY 1S --> REED NC -> wires to outside charger

Is that good or am I on a wrong path?

Thank you


r/arduino • • Sep 07 '26

Beginner's Project Just design my first arduino project

Post image
24 Upvotes

Hey guy's I just taken an intrest to arduino or stuff like that and just made my first circuit design and the revision by following a tutorial from "https://projecthub.arduino.cc/rinme/gas-leakage-detector-using-arduino-05c0c9"

It's a gas leakage detector and I'm thinking to swap the Arduino uno with a esp32 from my friend and I just bought a couple of things for my first project. I'm not a electrical student I'm and IT student so don't flame me guys. Is there any suggestion I should add into this project of mine?


r/arduino • • Sep 07 '26

Look what I made! Made this thingy and just turned it into a timer

66 Upvotes

I would share the code but its just bootlegged


r/arduino • • Sep 07 '26

numbers clipping on digital clock

3 Upvotes

can anyone tell my why my numbers clipp over the other numbers but not the background...

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <RTClib.h>
#include <Fonts/FreeMonoBoldOblique24pt7b.h>


struct Decoration;


#define TFT_CS   8
#define TFT_DC   5
#define TFT_RST  2


Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
RTC_DS3231 rtc;


// ================= Layout =================
const int DIGIT_WIDTH     = 52;
const int INNER_SPACING   = 4;
const int SEGMENT_SPACING = 58;
const int TEXT_SIZE       = 2;


int16_t posX[4], posY[4];
char lastHour[3]   = "--";
char lastMinute[3] = "--";


// ================= Farben =================
uint16_t COLOR_DIGITS, COLOR_GLOW, COLOR_HEART, COLOR_HEART_SHINE;
uint16_t COLOR_DECO, COLOR_DECO_DARK, COLOR_STAR, COLOR_SKULL;
uint16_t COLOR_CHAR_BODY, COLOR_CHAR_DARK, COLOR_CHAR_ACCENT, COLOR_CHAR_EYES, COLOR_CHAR_WHITE;


void setupColors() {
  COLOR_DIGITS      = tft.color565(255, 105, 220);
  COLOR_GLOW        = tft.color565(140, 40, 130);
  COLOR_HEART       = tft.color565(255, 90, 180);
  COLOR_HEART_SHINE = tft.color565(255, 180, 230);
  COLOR_DECO        = tft.color565(210, 180, 240);
  COLOR_DECO_DARK   = tft.color565(30, 15, 45);
  COLOR_STAR        = tft.color565(230, 200, 255);
  COLOR_SKULL       = tft.color565(245, 240, 255);


  COLOR_CHAR_BODY   = tft.color565(35, 25, 50);
  COLOR_CHAR_DARK   = tft.color565(15, 8, 25);
  COLOR_CHAR_ACCENT = tft.color565(255, 140, 200);
  COLOR_CHAR_EYES   = ST77XX_BLACK;
  COLOR_CHAR_WHITE  = tft.color565(250, 245, 255);
}


// ================= Deko-Elemente =================
struct Decoration {
  uint8_t type; // 0=Stern, 1=Totenkopf, 2=Herz gefüllt, 3=Herz Outline
  int16_t cx, cy, r;
};


const int NUM_DECOS = 14;
Decoration decos[NUM_DECOS] = {
  {0,  28,  28,  8},
  {0,  22, 205,  9},
  {0, 295, 200,  8},
  {0,  30,  75,  6},
  {0, 290,  55,  7},
  {1,  28, 145, 11},
  {1, 290, 140, 11},
  {2,  55,  55, 12},
  {2, 265,  70, 11},
  {2,  50, 175, 10},
  {2, 270, 175, 12},
  {3,  45, 110, 14},
  {3, 275, 110, 13},
  {3, 160,  35, 10}
};


// ================= Charakter =================
int16_t charCx, charCy;
int16_t heartCx, heartCy;



int16_t charLeft, charTop, charRight, charBottom;


unsigned long lastTimeCheck = 0;


// =====================================================
DateTime getLastSunday(int year, int month) {
  static const int daysInMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  int day = daysInMonth[month - 1];
  bool leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
  if (month == 2 && leap) day = 29;
  DateTime d(year, month, day, 0, 0, 0);
  int dow = d.dayOfTheWeek();
  day -= dow;
  return DateTime(year, month, day, 0, 0, 0);
}


bool isSummerTimeUTC(const DateTime &utc) {
  DateTime dstStart = getLastSunday(utc.year(), 3);
  dstStart = DateTime(dstStart.year(), dstStart.month(), dstStart.day(), 1, 0, 0);
  DateTime dstEnd = getLastSunday(utc.year(), 10);
  dstEnd = DateTime(dstEnd.year(), dstEnd.month(), dstEnd.day(), 1, 0, 0);
  return (utc.unixtime() >= dstStart.unixtime() && utc.unixtime() < dstEnd.unixtime());
}


DateTime getLocalTime() {
  DateTime utc = rtc.now();
  int offset = isSummerTimeUTC(utc) ? 2 : 1;
  return utc + TimeSpan(0, offset, 0, 0);
}


// =====================================================
void setup() {
  tft.init(240, 320);
  tft.setRotation(1);
  setupColors();


  charCx = tft.width() / 2;
  charCy = 92;
  heartCx = charCx + 105;
  heartCy = 52;


  charLeft   = charCx - 95;
  charTop    = charCy - 75;
  charRight  = charCx + 100;
  charBottom = charCy + 65;


  Wire.begin();
  if (!rtc.begin()) {
    tft.fillScreen(ST77XX_BLACK);
    tft.setCursor(10, 30);
    tft.setTextColor(ST77XX_RED);
    tft.print("RTC Fehler!");
    while (1) delay(1000);
  }


  if (rtc.lostPower()) {
    // rtc.adjust(DateTime(2026, 9, 7, 8, 58, 30)); // UTC!
  }


  drawBackground();
  calculatePositions();
  drawColon();
  drawMiniCharacter(charCx, charCy);


  
  drawHeart(heartCx, heartCy, 14, COLOR_HEART, COLOR_HEART_SHINE);


  DateTime local = getLocalTime();
  drawFullTime(local.hour(), local.minute());
  sprintf(lastHour,   "%02d", local.hour());
  sprintf(lastMinute, "%02d", local.minute());
}


void loop() {
  unsigned long now = millis();


  if (now - lastTimeCheck > 1000) {
    lastTimeCheck = now;
    updateClockDigits();
  }
}


void updateClockDigits() {
  DateTime local = getLocalTime();


  char currentHour[3], currentMinute[3];
  sprintf(currentHour,   "%02d", local.hour());
  sprintf(currentMinute, "%02d", local.minute());


  char allChars[4] = {
    currentHour[0], currentHour[1],
    currentMinute[0], currentMinute[1]
  };


  bool needRedrawCharacter = false;


  for (int i = 0; i < 4; i++) {
    char prevChar = i < 2 ? lastHour[i] : lastMinute[i-2];
    if (allChars[i] != prevChar) {
      
      int16_t ex = posX[i] - 6;
      int16_t ey = posY[i] - 55;
      int16_t ew = DIGIT_WIDTH + 12;
      int16_t eh = 68;


      eraseArea(ex, ey, ew, eh);
      drawDigit(posX[i], posY[i], allChars[i]);


      
      if (rectsOverlap(ex, ey, ew, eh, charLeft, charTop, charRight - charLeft, charBottom - charTop) ||
          rectsOverlap(ex, ey, ew, eh, heartCx - 20, heartCy - 20, 40, 40)) {
        needRedrawCharacter = true;
      }
    }
  }


  if (needRedrawCharacter) {
    drawMiniCharacter(charCx, charCy);
    drawHeart(heartCx, heartCy, 14, COLOR_HEART, COLOR_HEART_SHINE);
  }


  strcpy(lastHour,   currentHour);
  strcpy(lastMinute, currentMinute);
}


// =====================================================
void calculatePositions() {
  int16_t baseY = tft.height() - 38;
  int16_t totalWidth = DIGIT_WIDTH * 4 + INNER_SPACING * 2 + SEGMENT_SPACING;
  int16_t startX = (tft.width() - totalWidth) / 2;
  int16_t cursorX = startX;


  posX[0] = cursorX; posY[0] = baseY; cursorX += DIGIT_WIDTH + INNER_SPACING;
  posX[1] = cursorX; posY[1] = baseY; cursorX += DIGIT_WIDTH + SEGMENT_SPACING;
  posX[2] = cursorX; posY[2] = baseY; cursorX += DIGIT_WIDTH + INNER_SPACING;
  posX[3] = cursorX; posY[3] = baseY;
}


uint16_t getGradientColor(int16_t y) {
  int h = tft.height();
  y = constrain(y, 0, h - 1);
  uint8_t r = map(y, 0, h, 175, 95);
  uint8_t g = map(y, 0, h, 120, 55);
  uint8_t b = map(y, 0, h, 230, 145);
  return tft.color565(r, g, b);
}


// =====================================================
void drawBackground() {
  int h = tft.height();
  for (int y = 0; y < h; y++) {
    tft.drawFastHLine(0, y, tft.width(), getGradientColor(y));
  }
  drawDashedBorder();
  for (int i = 0; i < NUM_DECOS; i++) {
    drawDecoration(decos[i]);
  }
}


void drawDashedBorder() {
  int w = tft.width(), h = tft.height();
  int dash = 7, gap = 5;
  for (int x = 5; x < w - 5; x += dash + gap) {
    tft.drawFastHLine(x, 5, dash, COLOR_DECO);
    tft.drawFastHLine(x, h - 6, dash, COLOR_DECO);
  }
  for (int y = 5; y < h - 5; y += dash + gap) {
    tft.drawFastVLine(5, y, dash, COLOR_DECO);
    tft.drawFastVLine(w - 6, y, dash, COLOR_DECO);
  }
}


void drawDecoration(const Decoration &d) {
  switch (d.type) {
    case 0: drawStar(d.cx, d.cy, d.r, COLOR_STAR); break;
    case 1: drawSkull(d.cx, d.cy, d.r); break;
    case 2: drawHeart(d.cx, d.cy, d.r, COLOR_HEART, COLOR_HEART_SHINE); break;
    case 3: drawHeartOutline(d.cx, d.cy, d.r, COLOR_HEART); break;
  }
}


void drawStar(int16_t cx, int16_t cy, int16_t r, uint16_t color) {
  tft.fillTriangle(cx, cy - r, cx - , cy + , cx + , cy + , color);
  tft.fillTriangle(cx - r, cy, cx + , cy - , cx + , cy + , color);
  tft.fillCircle(cx - 1, cy - , 2, COLOR_CHAR_WHITE);
}


void drawSkull(int16_t cx, int16_t cy, int16_t r) {
  tft.fillCircle(cx, cy, r, COLOR_SKULL);
  tft.fillCircle(cx - , cy - 1, .5, COLOR_DECO_DARK);
  tft.fillCircle(cx + , cy - 1, .5, COLOR_DECO_DARK);
  tft.fillTriangle(cx, cy + 2, cx - 3, cy + 7, cx + 3, cy + 7, COLOR_DECO_DARK);
  tft.fillRect(cx - , cy +  - 1, r*2/3, , COLOR_SKULL);
  tft.drawLine(cx - , cy +  + 2, cx + , cy +  + 2, COLOR_DECO_DARK);
}


void drawHeart(int16_t cx, int16_t cy, int16_t r, uint16_t color, uint16_t shine) {
  tft.fillCircle(cx - , cy, , color);
  tft.fillCircle(cx + , cy, , color);
  tft.fillTriangle(cx - r, cy, cx + r, cy, cx, cy + r + 1, color);
  if (r > 7) {
    tft.fillCircle(cx - , cy - , max(2, ), shine);
  }
}


void drawHeartOutline(int16_t cx, int16_t cy, int16_t r, uint16_t color) {
  tft.drawCircle(cx - , cy, , color);
  tft.drawCircle(cx + , cy, , color);
  tft.drawLine(cx - r, cy, cx, cy + r, color);
  tft.drawLine(cx + r, cy, cx, cy + r, color);
  tft.drawLine(cx - r + 1, cy, cx, cy + r - 1, color);
  tft.drawLine(cx + r - 1, cy, cx, cy + r - 1, color);
}


// =====================================================
//  Kuromi 
// =====================================================
void drawMiniCharacter(int16_t cx, int16_t cy) {
  int r = 52;


  
  tft.fillTriangle(cx - 22, cy - 32, cx - 78, cy - 52, cx - 48, cy - 2, COLOR_CHAR_BODY);
  tft.fillTriangle(cx - 20, cy - 28, cx - 72, cy - 46, cx - 42, cy + 2, COLOR_CHAR_BODY);


  tft.fillTriangle(cx + 22, cy - 32, cx + 78, cy - 52, cx + 48, cy - 2, COLOR_CHAR_BODY);
  tft.fillTriangle(cx + 20, cy - 28, cx + 72, cy - 46, cx + 42, cy + 2, COLOR_CHAR_BODY);


  
  tft.fillCircle(cx, cy, r, COLOR_CHAR_BODY);


  
  tft.fillCircle(cx, cy + 10, 36, COLOR_CHAR_WHITE);


  
  tft.fillTriangle(cx - 32, cy - 28, cx + 32, cy - 28, cx, cy + 2, COLOR_CHAR_BODY);


  
  tft.fillCircle(cx - 20, cy + 20, 9, COLOR_CHAR_ACCENT);
  tft.fillCircle(cx + 20, cy + 20, 9, COLOR_CHAR_ACCENT);


  
  tft.fillCircle(cx - 14, cy + 6, 10, COLOR_CHAR_EYES);
  tft.fillCircle(cx + 14, cy + 6, 10, COLOR_CHAR_EYES);
  tft.fillCircle(cx - 11, cy + 2, 3, ST77XX_WHITE);
  tft.fillCircle(cx + 17, cy + 2, 3, ST77XX_WHITE);


  
  tft.drawLine(cx - 22, cy - 2,  cx - 16, cy - 7, COLOR_CHAR_DARK);
  tft.drawLine(cx - 18, cy + 0,  cx - 12, cy - 6, COLOR_CHAR_DARK);
  tft.drawLine(cx - 14, cy + 1,  cx - 8,  cy - 5, COLOR_CHAR_DARK);
  tft.drawLine(cx + 8,  cy - 5,  cx + 14, cy + 1, COLOR_CHAR_DARK);
  tft.drawLine(cx + 12, cy - 6,  cx + 18, cy + 0, COLOR_CHAR_DARK);
  tft.drawLine(cx + 16, cy - 7,  cx + 22, cy - 2, COLOR_CHAR_DARK);



  tft.fillCircle(cx, cy + 18, 3, COLOR_CHAR_DARK);
  tft.drawLine(cx - 10, cy + 26, cx, cy + 31, COLOR_CHAR_DARK);
  tft.drawLine(cx + 10, cy + 26, cx, cy + 31, COLOR_CHAR_DARK);
  tft.drawLine(cx - 9,  cy + 27, cx, cy + 32, COLOR_CHAR_DARK);
  tft.drawLine(cx + 9,  cy + 27, cx, cy + 32, COLOR_CHAR_DARK);


 
  tft.fillCircle(cx + 48, cy - 48, 9, COLOR_CHAR_ACCENT);
  tft.fillCircle(cx + 62, cy - 48, 9, COLOR_CHAR_ACCENT);
  tft.fillRect(cx + 51, cy - 51, 10, 7, COLOR_CHAR_ACCENT);
  tft.fillCircle(cx + 55, cy - 62, 8, COLOR_SKULL);
  tft.fillCircle(cx + 52, cy - 64, 2.5, COLOR_DECO_DARK);
  tft.fillCircle(cx + 58, cy - 64, 2.5, COLOR_DECO_DARK);
  tft.fillTriangle(cx + 55, cy - 60, cx + 52, cy - 55, cx + 58, cy - 55, COLOR_DECO_DARK);


  
  tft.drawLine(cx + 44, cy + 42, cx + 72, cy + 26, COLOR_CHAR_BODY);
  tft.drawLine(cx + 44, cy + 43, cx + 72, cy + 27, COLOR_CHAR_BODY);
  tft.drawLine(cx + 45, cy + 44, cx + 72, cy + 28, COLOR_CHAR_BODY);
  tft.fillTriangle(cx + 72, cy + 26, cx + 82, cy + 16, cx + 78, cy + 34, COLOR_CHAR_BODY);
}


// =====================================================
void drawColon() {
  int16_t colonX = posX[1] + DIGIT_WIDTH + (SEGMENT_SPACING / 2) - 5;
  int16_t colonY = posY[0];
  tft.fillCircle(colonX, colonY - 32, 6, COLOR_DIGITS);
  tft.fillCircle(colonX, colonY - 10, 6, COLOR_DIGITS);
}


void drawFullTime(int h, int m) {
  char chars[5];
  sprintf(chars, "%02d%02d", h, m);
  for (int i = 0; i < 4; i++) {
    drawDigit(posX[i], posY[i], chars[i]);
  }
}


void drawDigit(int16_t x, int16_t y, char c) {
  tft.setFont(&FreeMonoBoldOblique24pt7b);
  tft.setTextSize(TEXT_SIZE);


  tft.setTextColor(COLOR_GLOW);
  int offsets[4][2] = {{-3,-3},{3,-3},{-3,3},{3,3}};
  for (int i = 0; i < 4; i++) {
    tft.setCursor(x + offsets[i][0], y + offsets[i][1]);
    tft.print(c);
  }


  tft.setTextColor(COLOR_DIGITS);
  tft.setCursor(x, y);
  tft.print(c);
}


// =====================================================
bool rectsOverlap(int16_t ax, int16_t ay, int16_t aw, int16_t ah,
                   int16_t bx, int16_t by, int16_t bw, int16_t bh) {
  return !(ax + aw < bx || bx + bw < ax || ay + ah < by || by + bh < ay);
}


void eraseArea(int16_t x, int16_t y, int16_t w, int16_t h) {
  
  for (int16_t row = 0; row < h; row++) {
    int16_t yy = y + row;
    if (yy >= 0 && yy < tft.height()) {
      tft.drawFastHLine(x, yy, w, getGradientColor(yy));
    }
  }


  
  for (int i = 0; i < NUM_DECOS; i++) {
    Decoration &d = decos[i];
    int16_t dx = d.cx - d.r - 3;
    int16_t dy = d.cy - d.r - 3;
    int16_t dw = d.r * 2 + 6;
    int16_t dh = d.r * 2 + 6;
    if (rectsOverlap(x, y, w, h, dx, dy, dw, dh)) {
      drawDecoration(d);
    }
  }
}