0
votes

Was trying to make a vibration detector and followed the tutorial from manufacturer site where I bought the Arduino but I got errors. I tried changing

unsigned char state = 0;

to

unsigned char state;
state =0;

No luck.

Errors are:

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

Codes:

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

2 Answers

0
votes

First of all

unsigned char state = 0;

and

unsigned char state;
state =0;

are exactly the same thing.

What is the meaning of digital pin 3 line in the code. You have already defined SensorINPUT = 3, and making it INPUT will make pin D3 as input pin.

So just remove that line and code will compile fine. Rest of the error seems due to this line only.

0
votes

I am not commenting on functionality but errors can be fixed like this:

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);
}