I have tested it with using analog reads and serial prints to test that it is working but I wanted to know if I can use it with one of my external interrupt pins (assuming I will need to send a digital signal not an analog to do that)
My testing code to get it to simply light up the pin 13 led when it is blocked is
int sensor =20;
int LED = 13;
void setup()
{
pinMode(LED, OUTPUT);
pinMode(sensor, INPUT);
attachInterrupt(3, counter, RISING);
}
void loop()
{
digitalWrite(LED, LOW);
}
void counter()
{
digitalWrite(LED, HIGH);
}
You should be able to use it as an i/p on a interrupt pin.
I do not have a Mega, only a uno.
There are two interrupts on a uno 0 and 1 which are pins 2 ans 3.
Make sure the pins on your mega are interrupt pins and it is wired properly.
int ledPin = 13; // LED connected to digital pin 13
int photoPin = 3; //
int val = 0;
int count=0;
void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(photoPin, INPUT);
attachInterrupt(1, counting, RISING);
}
void loop() // run over and over again
{
val = digitalRead(photoPin);
if (val==LOW)
{
digitalWrite(ledPin, LOW);
}
else
{
digitalWrite(ledPin, HIGH);
}
}
void counting()
{
count=count+1;
Serial.print(count);
}
It can digitally read the sensor but I can't get anything inside the interrupt loop to work. Any ideas?