You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arduino代码报错:int与uint8_t参数类型不兼容求助

Fixing "int vs uint8_t" Compatibility Error in Your Arduino Sketch

Hey there! Let's get this sorted out for you—no worries, we all start somewhere, and this is a super common mix-up when working with arrays and Arduino pin functions.

What's Causing the Error?

The error pops up because functions like pinMode() and digitalWrite() expect a single pin number (which is of type uint8_t—a small, unsigned integer perfect for pin numbers 0-255). But you're passing entire int arrays (like C1, C2) directly to these functions. Arrays and single integers are different types, so the compiler throws that mismatch error.

On top of that, you've got a critical mistake with variable types for your pins: you defined pins like bool J5 = 19;—bool can only hold true/false (0/1), so assigning 19 here gets converted to true, which means your code is actually using pin 1 instead of 19! That's going to cause all sorts of unexpected behavior even after fixing the first error.

Step-by-Step Fixes

1. Fix Pin Variable Types

Change all your pin definitions from bool to uint8_t (or byte, which is an Arduino alias for uint8_t—it's more readable for pin numbers):

// Inputs and their Variables
uint8_t J5 = 19; // J5 is an interrupt pin
bool j5state;

// Inputs PULLUPs
uint8_t S3 = 0;
uint8_t S1 = 1;
bool S1state;
bool S3state;

// Momentary PULLUPs
uint8_t J1 = 17;
bool j1state;

// Analog Inputs
uint8_t J3in = A2; // Use A2 for analog pin 2 (more explicit)
int J3state;

// Momentary OUTPUTs
uint8_t J4 = 14;
uint8_t J3out = 18;

2. Fix Array Types for Output Groups

Since pin numbers fit in uint8_t, change your output arrays from int to uint8_t (or byte):

// OUTPUTs
uint8_t C1[4] = {10, 11, 13, 4};
uint8_t C2[3] = {11, 12, 4};
uint8_t C3[2] = {10, 4};
uint8_t C4[1] = {9};
uint8_t C5[1] = {8};

3. Loop Through Arrays for pinMode() and digitalWrite()

You can't pass an array directly to these functions—you need to loop through each pin in the array and set them individually. Here's how to update your setup() function:

void setup() {
  pinMode(J5, INPUT);
  pinMode(S3, INPUT_PULLUP);
  pinMode(S1, INPUT_PULLUP);
  pinMode(J1, INPUT);
  pinMode(J3in, INPUT);
  
  // Set all C1 pins to OUTPUT
  for (int i = 0; i < sizeof(C1)/sizeof(C1[0]); i++) {
    pinMode(C1[i], OUTPUT);
  }
  // Repeat for C2-C5
  for (int i = 0; i < sizeof(C2)/sizeof(C2[0]); i++) {
    pinMode(C2[i], OUTPUT);
  }
  for (int i = 0; i < sizeof(C3)/sizeof(C3[0]); i++) {
    pinMode(C3[i], OUTPUT);
  }
  pinMode(C4[0], OUTPUT); // Since C4 only has 1 pin, you can skip the loop
  pinMode(C5[0], OUTPUT);

  Serial.begin(9600);

  // Turn off every output
  for (int i = 0; i < sizeof(C1)/sizeof(C1[0]); i++) {
    digitalWrite(C1[i], LOW);
  }
  for (int i = 0; i < sizeof(C2)/sizeof(C2[0]); i++) {
    digitalWrite(C2[i], LOW);
  }
  for (int i = 0; i < sizeof(C3)/sizeof(C3[0]); i++) {
    digitalWrite(C3[i], LOW);
  }
  digitalWrite(C4[0], LOW);
  digitalWrite(C5[0], LOW);

  // ... rest of your setup code ...
}

4. Fix the Interrupt Attach Call

In your goingToSleep() function, you're using j5state (a bool from digitalRead()) for attachInterrupt()—this is wrong. You need to use the pin number (or interrupt number, but using digitalPinToInterrupt() makes it portable):

void goingToSleep() {
  sleep_enable();
  // Use the pin number J5, converted to its interrupt number
  attachInterrupt(digitalPinToInterrupt(J5), wakeUp, LOW);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_cpu();
}

Quick Note on sizeof()

The sizeof(C1)/sizeof(C1[0]) trick calculates how many elements are in the array—this way, if you add/remove pins from the array later, you don't have to update the loop count manually.

Final Check

After making these changes, the compiler won't throw the int/uint8_t mismatch error anymore, and your pin assignments will be correct. Take it slow and test small parts first if you're unsure—serial prints are your friend for debugging!

内容的提问来源于stack exchange,提问作者IMFROST

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 00:08:01