没有名称类型,也没有声明错误

时间:2017-08-24 04:40:21

标签: arduino

试图制作振动探测器并按照制造商网站上的教程我购买了Arduino,但是我遇到了错误。 我试过改变

unsigned char state = 0;

unsigned char state;
state =0;

没有运气。

错误是:

error: 'digital' does not name type
'blink' was not declared in this scope
'state' was not declared in this scope

代码:

int SensorLED = 13;                              //define LED digital pin 13
int SensorINPUT = 3;                           // connect tilt sensor to   interrupt 1 in
digital pin 3
unsigned char state = 0;

void setup() {
pinMode(SensorLED, OUTPUT);      //configure LED as output mode
pinMode(SensorINPUT, INPUT);     //configure tilt sensor as input mode
//when low voltage changes to high voltage, it triggers interrupt 1 and runs the blink function
attachInterrupt(1, blink, RISING);
}


void loop(){
if(state!=0){                                                     // if state is not 0
state = 0;                                                     // assign state value 0
digitalWrite(SensorLED,HIGH);               // turn on LED
delay(500);                                                 // delay for 500ms
}
else{
digitalWrite(SensorLED,LOW);                 // if not, turn off LED
}
}


void blink(){                                              // interrupt             function blink()
state++;                                                    //once trigger the interrupt, the state keeps increment
} 

2 个答案:

答案 0 :(得分:0)

首先

unsigned char state = 0;

unsigned char state;
state =0;

完全相同。

代码中digital pin 3行的含义是什么?您已定义SensorINPUT = 3,并将INPUT设为D3作为输入引脚。

所以只需删除该行,代码就可以正常编译。其余的错误似乎只是由于这一行。

答案 1 :(得分:0)

我不是在评论功能,但错误可以修复如下:

const byte SensorLED = 13;
const byte SensorINPUT = 3;


volatile byte state = LOW;

void blink(void)
{
  state = !state;
}

void setup() 
{
  pinMode(SensorLED, OUTPUT);      
  pinMode(SensorINPUT, INPUT);     
  attachInterrupt(1, blink, RISING);
}


void loop()
{
   digitalWrite(SensorLED, state);
   delay(500);
}