Hello
I am having a problem with some code Iv written.
I'm relatively new to programming so copy paste is the name of the game at this point and I'm sure the fix will be something basic, iv tried all sorts of this to fix this but I'm at loss now.
So, What the code should do.
If the onOff is LOW it turns the whole process on.
then if the eye senses something it should run the portion of code between "iv seen something" and "doneski"
if at any point pin 12 goes HIGH it should run the interrupt function, turning label HIGH.
then after 100millis turn Label back to LOW.
Most of the time this all works a charm
BUT
If I turn the power on, leave it sitting for a few minutes the trigger pin 12, it won't do anything.
the same happens if I have triggered pin 12 then waited a few minutes then tried to trigger it again it doesn't work.
if i trigger every 20 or so seconds its fine.
Im using and Arduino UNO, IDE 1.6.12
any help appreciated
Thanks
here's my code
#include <Servo.h>
#include <EnableInterrupt.h>
#define ARDUINOPIN 12//
const int onOff = 4; //on Button
const int eye = 2; //sensor that triggers start of main loop
const int blow = 5; //blow
const int next = 6; //lifts the blower
const int rotate = 7; //rotate
const int punch = 8; //push
const int label = 9; //
int servoPin = 11; //servo attached here
int buttonState = 0; //variable for onOff button
int eyeState = 0; //variable for eye sensor
Servo Servo1;//servo assign
int interruptCounter = 0; //time since the last cycle
int labelling = 0; //variable, goes to 1 when cycling to make sure it wont cycle agin till time done.
int labelTime = 2200; // adjust this to change cycle downtime.
void interruptFunction() { //interrupt from label start sensor, sends the signal to label but only if its been long enough since last cycle
if (interruptCounter >= labelTime) {
digitalWrite(label, HIGH);
Serial.println("Start LABEL");
interruptCounter = 0; //sets the timer to zero, starts the waiting process
labelling = 1; //sets the labelling to 1 so that it can run the timer interrupt loop but only if its actually labelling
}
}
void setup() {
pinMode(onOff, INPUT_PULLUP);
pinMode(eye, INPUT_PULLUP);
pinMode(blow, OUTPUT);
pinMode(next, OUTPUT);
pinMode(rotate, OUTPUT);
pinMode(punch, OUTPUT);
pinMode(label, OUTPUT);
digitalWrite(blow, HIGH);
delay(100);
digitalWrite(blow, LOW);
Serial.begin(9600);
Servo1.attach(servoPin);
Serial.println("SETUP");
Servo1.write(85);
enableInterrupt(ARDUINOPIN, interruptFunction, CHANGE);
OCR0A = 0xAF; //time interrupt stuff, copied from adafruit timer interrupts tutorial i think.
TIMSK0 |= _BV(OCIE0A);
}
SIGNAL(TIMER0_COMPA_vect) //checks this once per milisecond, when true stops the label signal
{
interruptCounter++;
if (interruptCounter >= 100 && labelling == 1) {
digitalWrite(label, LOW);
Serial.println("Label signal sent");
labelling = 0;
Serial.print("Labelling");
Serial.println(labelling);
}
}
void loop() {
buttonState = digitalRead(onOff);
eyeState = digitalRead(eye);
if (buttonState == LOW) {
if (eyeState == LOW) {
if (interruptCounter >= labelTime) { //hoping to fix the random 'no label after sitting issue...'
interruptCounter = 0;
labelling = 0;
}
Serial.println("Seen Something");
digitalWrite(next, HIGH);
Serial.println("Next");
delay(40);
digitalWrite(next, LOW);
delay(500);
digitalWrite(rotate, HIGH);
Servo1.write(170);
delay(300);
Serial.print("Rotate");
digitalWrite(punch, HIGH);
Serial.println("Punch!!!");
delay(150);
digitalWrite(punch, LOW);
delay(100);
digitalWrite(rotate, LOW);
Serial.println("Rot return");
delay(200);
Serial.println("Sol OTW");
digitalWrite(blow, HIGH);
Serial.println("Huff Puff");
delay(300);
digitalWrite(blow, LOW);
Servo1.write(85);
Serial.println("DONSKI");
}
}
}