Hey Guys,
working on a project to essentially control some motors with DMX and seem to be running into a situation where the two interrupts are continually firing, almost like they switches arent grounded through a resistor, except when they arent used as interrupts they have no issues. i dont think its anything hardware related, so i was wondering if anyone here might be able to spot something wrong in my code:
#include <DMXSerial.h>
#include <EEPROM.h>
// this constant won't change:
const int upBoundryPin = 20;
const int downBoundryPin = 21;
const int counterPin = 3;
const int calibratePin = 4;
const int relayDown = 5;
const int relayUp = 6;
const int enable = 133;
// Variables will change:
volatile int linkCounter = 0; // counter for the number of button presses
int counterState = 0; // current state of the button
int lastCounterState = 0; // previous state of the button
volatile int directionState = 0;
int target = 0;
int rawTarget = 0;
int lastTarget = 0;
int range = 20;
int movingUp = LOW;
int movingDown = LOW;
volatile int halt = LOW;
int startAddress = 1;
int enableValue = 0 ;
void upLimitTriggered() {
/* digitalWrite(relayDown, LOW);
digitalWrite(relayUp, LOW);
if (movingUp == HIGH) {
linkCounter = 0;
directionState = LOW;
Serial.println("Upper Limit Hit");
}
else {
halt = HIGH;
}
*/
Serial.println("interupt up");
}
void downLimitTriggered() {
/* digitalWrite(relayDown, LOW);
digitalWrite(relayUp, LOW);
if (movingDown == HIGH) {
linkCounter = range;
directionState = HIGH;
Serial.println("Lower Limit Hit");
}
else {
halt = HIGH;
}
*/
Serial.println("interupt Down");
}
void setup() {
DMXSerial.init(DMXReceiver);
// initialize the button pin as a input:
pinMode(counterPin, INPUT);
pinMode(upBoundryPin, INPUT);
pinMode(downBoundryPin, INPUT);
pinMode(calibratePin, INPUT);
pinMode(relayUp, OUTPUT);
pinMode(relayDown, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
//Home
while (digitalRead(upBoundryPin) == LOW) {
digitalWrite(relayUp, HIGH);
Serial.println("Homing");
}
digitalWrite(relayUp, LOW);
Serial.println("Homed");
/*Calibrate Range
if (digitalRead(calibratePin) == HIGH) {
while (digitalRead(downBoundryPin) == LOW) {
digitalWrite(relayDown, HIGH);
//Set Direction
counterState = digitalRead(counterPin);
if (target > linkCounter) {
directionState = LOW;
}
else if (target < linkCounter) {
directionState = HIGH;
}
if (counterState != lastCounterState) {
if ((counterState == HIGH) && (directionState == LOW)) {
linkCounter--;
Serial.println(linkCounter);
}
}
delay(50);
}
digitalWrite(relayDown, LOW);
EEPROM.update(1, linkCounter);
//ReHome
while (digitalRead(upBoundryPin) == LOW) {
digitalWrite(relayUp, HIGH);
}
Serial.println("Homed");
linkCounter = 0;
}
range = EEPROM.read(1);
}
*/
attachInterrupt(digitalPinToInterrupt(upBoundryPin), upLimitTriggered, RISING);
attachInterrupt(digitalPinToInterrupt(downBoundryPin), downLimitTriggered, RISING);
}
void loop() {
//Etsop for when interupts trigger in the wrong way
while (0 == 0) {
enableValue = DMXSerial.read(startAddress + 1);
if (0 == 0) {
//Check if still getting DMX
unsigned long lastPacket = DMXSerial.noDataSince();
if (lastPacket < 5000) {
//Read Target Location
rawTarget = DMXSerial.read(startAddress);
target = map(rawTarget, 0, 255, 0, range);
if (lastTarget != target) {
Serial.print("Current Target: ");
Serial.println(target);
lastTarget = target;
}
}
else {
target = 0;
}
//Set Direction
counterState = digitalRead(counterPin);
if (target > linkCounter) {
directionState = LOW;
}
else if (target < linkCounter) {
directionState = HIGH;
}
//Relay Control
//Down
if ((linkCounter < target) && (movingUp == LOW)) {
digitalWrite(relayUp, LOW);
digitalWrite(relayDown, HIGH);
movingDown = HIGH;
}
//Up
else if ((linkCounter > target) && (movingDown == LOW)) {
digitalWrite(relayUp, HIGH);
digitalWrite(relayDown, LOW);
movingUp = HIGH;
}
else {
digitalWrite(relayUp, LOW);
digitalWrite(relayDown, LOW);
movingDown = LOW;
movingUp = LOW;
}
//Link Counter
if (counterState != lastCounterState) {
if ((counterState == HIGH) && (directionState == LOW)) {
linkCounter++;
Serial.println("Down");
Serial.print("number of Links Counted: ");
Serial.println(linkCounter);
}
else if ((counterState == HIGH) && (directionState == HIGH)) {
linkCounter--;
Serial.println("Up");
Serial.print("number of Links Counted: ");
Serial.println(linkCounter);
}
else {
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastCounterState = counterState;
}
}
}