【发布时间】:2020-08-30 12:37:46
【问题描述】:
这是我第一次编码。我刚刚从 youtube.com 学到了一些东西,这是我第一次使用这些知识为我的微控制器编写一个 arduino 草图。请对我放轻松。
我遇到的问题是这样的:
*“int ”类型的参数与“uint8_t”类型的参数不兼容
在 OUTPUT 数组声明中。
Error areas 1 and 2 Error area 3
//https://codeshare.io/GA9YDl
#include <Arduino.h>
#include <avr/sleep.h> // this AVR library contains the methods that controls the sleep modes
bool RSTstate = 0;
//Inputs and their Variables
bool J5 = 19; //J5 is also an boolerrupt so we are going to be using that as one too.
bool j5state;
//Inputs PULLUPs
bool S3 = 0;
bool S1 = 1;
bool S1state;
bool S3state;
//Momentary PULLUPs
bool J1 = 17;
bool j1state;
//Analog Inputs
int J3in = 2;
int J3state;
//Momentary OUTPUTs
bool J4 = 14;
bool J3out = 18;
//OUTPUTs
int C1[4] = {10, 11, 13, 4};
int C2[3] = {11, 12, 4};
int C3[2] = {10, 4};
int C4[1] = {9};
int C5[1] = {8};
void goingToSleep() {
sleep_enable(); // enabling sleep mode
attachInterrupt(j5state, wakeUp, LOW); // attaching to boolerrupt J5 (INTERRUPT_PIN) the function wakeUp to be triggered when get LOW signal... Also J5 LOW means plugged IN.
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // setting the sleep mode, in our case full sleep
sleep_cpu(); // activating sleep mode
}
void wakeUp() {
Serial.println("interrrupt Fired!"); // print message to serial monitor
sleep_disable(); // disable sleep mode
detachInterrupt(j5state); // removes the boolerrupt from J5
softreset(); //Now needs to reset the mcu as the jack is plugged in.
RSTstate = 1; //Set Reset State to 1 so that the MCU restarts the sketch
}
void softreset() {
asm volatile (" jmp 0"); //Just restart the sketch, does not reset the registers etc.
}
void setup() {
pinMode(J5, INPUT);
pinMode(S3, INPUT_PULLUP);
pinMode(S1, INPUT_PULLUP);
pinMode(J1, INPUT);
pinMode(J3in, INPUT);
pinMode(C1, OUTPUT);
pinMode(C2, OUTPUT);
pinMode(C3, OUTPUT);
pinMode(C4, OUTPUT);
pinMode(C5, OUTPUT);
Serial.begin(9600);
//First turn off every output
digitalWrite(C1, LOW);
digitalWrite(C2, LOW);
digitalWrite(C3, LOW);
digitalWrite(C4, LOW);
digitalWrite(C5, LOW);
//This is the first phase: To check if the headphone plug is connected or not.
pinMode(J4, OUTPUT);
digitalWrite(J4, LOW);
delay(5);
j5state = digitalRead(J5);
if (j5state == LOW) { //That means that the 3.5mm plug is put in.
//Here the second phase starts where we need to check the connectivity of S1 and S3.
pinMode(J4, INPUT); //Fisrt setup the J4 to INPUT so that it does not boolerface with the Audio.
S1state = digitalRead(S1);
S3state = digitalRead(S3);
if (S1state == HIGH && S3state == LOW) { //This means that Condition 1, 2 and 3 applies.
//Here is the 3rd phase starts where we have to check the conditions for C1, C2 and C3
//First C1 where we have to check for J1 and J2 connectivity. J2 is Hardware GND and J1 is mic input which is boolernal PULLUP.
pinMode(J1, INPUT_PULLUP);
delay(5);
j1state = digitalRead(J1);
if (j1state == LOW) {
//This is where the C1 condition is TRUE and is executed
digitalWrite(C1, HIGH);
}
else if (j1state == HIGH) {
//That means C1 is not meeting requirements and thus C2 is now checked
//To check C2 we need to check between J3 and J4 and the easy way to do that is to just check an analog value for J3 to GND.
pinMode(J3in, INPUT);
pinMode(J3out, OUTPUT);
delay(5);
digitalWrite(J3out, HIGH);
delay(5);
J3state = analogRead(J3in);
pinMode(J3out, INPUT);
if (J3state > 15) {
//That means that J3 is not connected to GND. Which means there is a speaker attached. That means C2 is applied.
digitalWrite(C2, HIGH);
}
else if (J3state < 15) {
//That means that J3 is directly connected to GND which also means that there is no speaker attached.
//So now C3 is applied.
digitalWrite(C3, HIGH);
}
else {
Serial.println("ERROR C2, C3");
}
}
else {
Serial.println("ERROR C1");
}
}
else if (S1state == LOW && S3state == HIGH) {
//Now the S1 is turned ON and thus we only need to check Condition 4 (C4) and C5.
//First C4, for C4 we need the connection between J3 and GND. Same method as C2 and C3.
pinMode(J3out, OUTPUT);
delay(5);
digitalWrite(J3out, HIGH);
delay(5);
J3state == analogRead(J3in);
pinMode(J3out, INPUT);
if (J3state > 15) {
//That means J3 is not connected to GND. Which applied C4.
digitalWrite(C4, HIGH);
}
else if (J3state < 15) {
//Now the condition is set to C5
digitalWrite(C5, HIGH);
}
else {
Serial.println("ERROR C4, C5");
}
}
else {
Serial.println("ERROR S1, S3");
}
}
else if (j5state == HIGH) {
goingToSleep();
//This loop is for when there is no jack, the microcontroller can just
//wait for the plug to be inserted. But also in some kind of sleepmode to conserve power.
}
else {
Serial.println("ERROR JACK");
}
}
//All of the above process is done in the setup loop since it does not needs to be repeated. Only run once.
void loop() {
if (RSTstate == 1){
softreset();
}
else {
goingToSleep(); //Once everything is executed succesfully. Here the microcontroller goes into sleep mode and will only wake up if the jack is plugged IN again.
}
}
【问题讨论】:
-
请确认错误涉及哪些行号,因为您的代码没有编号。您的
main()也丢失了,所以这不是一个最小的可重现示例。此外,请考虑 (a) 将此类单体函数拆分为更小的函数,(b) 如果它们没有正当理由成为全局变量,则不要使用如此多的全局变量(很少这样做),以及( c) 在错误情况下尽早退出,而不是做那种可怕的不断增加的if/else缩进厄运金字塔。 -
了解您的数据类型。正如错误所说,它们可能不兼容。
-
@TomServo:如果您在谈论 Pins 变量的数据类型,那么是的,您是正确的,我更改了它们。但是这个错误只发生在 //OUTPUTs 下的数组中。现在我已经删除了数组,它很好。
标签: arrays arduino arduino-ide arduino-c++