When the down button state is changed it will send the ir signal but it takes 3-5 seconds for this to send the ir signal instead of immediately is it due to the fact the function is so far down in the code? IR pin is connected to pin 9 per IR library.
#include <IRremote.h>
#include <Boards.h>
#include <Firmata.h>
#include <FirmataConstants.h>
#include <FirmataDefines.h>
#include <FirmataMarshaller.h>
#include <FirmataParser.h>
#include <Wire.h>
IRsend irsend;
const unsigned long P1 = 0x20DF8877;
const int Down_buttonPin = 5;
int shotsLeftCheck = 35; // Check How Many Shots Left
int down_buttonState = 0; // current state of the trigger
int down_lastButtonState = 0; // previous state of the trigger
const int Game_start = 4; // Starts Game
int Game_startState; // Shows Game On or Off
int pincheck[54];
int prevpincheck[54];
int Score_Pins1 = 22;
int Score_Pins2 = 23;
int Score_Pins3 = 24;
int Score_Pins4 = 25;
int Score_Pins5 = 26;
int Score_Pins6 = 27;
int Score_Pins7 = 28;
int Score_Pins8 = 29;
int Score_Pins9 = 30;
int Score_Pins10 = 31;
int Score_Pins11 = 32;
int Score_Pins12 = 33;
int Score_Pins13 = 34;
int Score_Pins14 = 35;
int Score_Pins15 = 36;
int Score_Pins16 = 37;
int Score_Pins17 = 38;
int Score_Pins18 = 39;
int Score_Pins19 = 40;
int Score_Pins20 = 41;
int Score_Pins21 = 42;
int Score_Pins22 = 43;
int Score_Pins23 = 44;
int Score_Pins24 = 45;
int Score_Pins25 = 46;
int Score_Pins26 = 47;
int Score_Pins27 = 48;
int Score_Pins28 = 49;
int Score_Pins29 = 50;
int Score_Pins30 = 51;
int Score_Pins31 = 52;
int Score_Pins32 = 53;
const int Game_Reset = 2;
int Score = 0000;
int a = 100;
bool bPress = false;
String pin_str = "";
float current_time = millis();
float pintimeout[54];
float pin_timeout_default=500;
void setup()
{
Serial.begin(9600);
for (int i=0;i<54;i++) {
pincheck[i]=0;
prevpincheck[i]=0;
pintimeout[i]=0;
}
for (int ii = 2;ii<=sizeof(pincheck)/sizeof(pincheck[0]);ii++){
pinMode(ii,INPUT);
pinMode(Game_Reset, OUTPUT);
digitalWrite(Game_Reset, LOW);
pinMode( Down_buttonPin , INPUT_PULLUP);
pinMode(Game_start, OUTPUT);
}
Serial.println(Score);
}
void pin_status()
{
for (int i=3;i<54;i++) {
pincheck[i]=digitalRead(i);
}
Serial.println(Score);
}
void loop()
{
// pincheck[Game_start] = digitalRead(Game_start);
pin_status();
current_time = millis();
for (int i=22;i<54;i++) {
//if (pincheck[i]==HIGH && prevpincheck[i]!=HIGH) {
if (pincheck[i]==HIGH && prevpincheck[i]!=HIGH && current_time >= pintimeout[i] && pincheck[Game_start] != HIGH ) {
Score = a + Score;
Serial.println(Score);
pintimeout[i] = current_time + pin_timeout_default;
}
}
if (pincheck[Game_start] == HIGH){
delay(50);
Score = 0000;
} else {
checkDown();
delay(5);
}
//Serial.println(Score);
while (Serial.available()){
char in_char = Serial.read();
if (int(in_char)!=-1){
pin_str+=in_char;
}
if (in_char=='\n'){
int pin_num = pin_str.toInt();
pincheck[pin_num] = !pincheck[pin_num];
digitalWrite(pin_num, pincheck[pin_num]);
if (pincheck[pin_num]==0){
} else {
}
pin_str = "";
}
}
for (int i=5;i<54;i++) {
prevpincheck[i]=pincheck[i];
}
}
void checkDown()
{
down_buttonState = digitalRead(Down_buttonPin);
if(shotsLeftCheck <= 0){
digitalWrite(Game_Reset, HIGH);
delay(500);
digitalWrite(Game_Reset, LOW);
}
if(shotsLeftCheck <= 0 || shotsLeftCheck >35) {
shotsLeftCheck = 35;
} else {
// compare the buttonState to its previous state
if (down_buttonState != down_lastButtonState) {
// if the state has changed, increment the counter
if (down_buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
irsend.sendNEC(P1, 32);
shotsLeftCheck--;
Serial.println(Score);
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
down_lastButtonState = down_buttonState;
}
}