Hello, I am having trouble getting attachInterupt to work on an Arduino nano.
I am trying to catch the change of state of an encoder when it is turned, not read the encoder.
CLK pin of the encoder is attached to D3.
A0 goes to a transistor, in order to ground a pin on a sound board.
The sound circuit works when sent 5 volts manually.
I have been through multiple references and tutorials, I don't know what I'm doing wrong.
Here is the code, //********** indicates code related to the problem.
Thanks for any help.
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
#define SERVOMIN 150
#define SERVOMAX 600
#define USMIN 600
#define USMAX 2400
#define SERVO_FREQ 50
uint8_t servonum = 0;
const int buttonPinstockext = 12;
const int buttonPinstockret = 11;
const int buttonPinbarrelext = 8;
const int buttonPinbarrelret = 7;
const int buttonPinencodeclick = 9;
const int interruptpin = 3; //**************************************************************
int buttonStatestockext = 0;
int buttonStatestockret = 0;
int buttonStatebarrelext = 0;
int buttonStatebarrelret = 0;
int buttonStateencodeclick = 0;
volatile int encodeturn = 0;//*************************************************************
void setup() {
pinMode(buttonPinstockext, INPUT);
pinMode(buttonPinstockret, INPUT);
pinMode(buttonPinbarrelext, INPUT);
pinMode(buttonPinbarrelret, INPUT);
pinMode(buttonPinencodeclick, INPUT);
pinMode(interruptpin, INPUT);//***********************************************************
attachInterrupt(digitalPinToInterrupt(interruptpin), beep, CHANGE);//*********************
pinMode(A0, OUTPUT);//********************************************************************
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A6, OUTPUT);
pinMode(A7, OUTPUT);
digitalWrite(A0, LOW);//******************************************************************
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
digitalWrite(A3, LOW);
digitalWrite(A6, LOW);
digitalWrite(A7, LOW);
pwm.begin();
pwm.setOscillatorFrequency(27000000);
pwm.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates
delay(10);
}
void setServoPulse(uint8_t n, double pulse)
{
double pulselength;
pulselength = 1000000;
pulselength /= SERVO_FREQ;
pulselength /= 4096;
pulse *= 1000000;
pulse /= pulselength;
pwm.setPWM(n, 0, pulse);
}
void beep()//*******************************************************************************
//*************************************
{ //*************************************
encodeturn = 1; //*************************************
} //*************************************
void loop() {
buttonStateencodeclick = digitalRead(buttonPinencodeclick);
buttonStatestockext = digitalRead(buttonPinstockext);
buttonStatestockret = digitalRead(buttonPinstockret);
buttonStatebarrelext = digitalRead(buttonPinbarrelext);
buttonStatebarrelret = digitalRead(buttonPinbarrelret);
if (encodeturn == 1);//********************************************************************
//*************************************
{ //*************************************
digitalWrite(A0, LOW); //*************************************
delay(130); //*************************************
digitalWrite(A0, HIGH); //*************************************
encodeturn = 0; //*************************************
} //*************************************
if ((buttonStatestockext == HIGH) || (buttonStatestockret == HIGH))
pwm.setPWM(7, 0, 0);
if ((buttonStatebarrelext == HIGH) || (buttonStatebarrelret == HIGH))
pwm.setPWM(6, 0, 0);
if ((buttonStateencodeclick == LOW) && (buttonStatestockext == HIGH))
pwm.setPWM(7, 0, 240);
if ((buttonStateencodeclick == LOW) && (buttonStatebarrelext == HIGH))
pwm.setPWM(6, 0, 275);
if ((buttonStateencodeclick == LOW) && (buttonStatestockret == HIGH))
pwm.setPWM(7, 0, 340);
if ((buttonStateencodeclick == LOW) && (buttonStatebarrelret == HIGH))
pwm.setPWM(6, 0, 320);
}