Programming Assistance

 #include <Boards.h>
#include <Firmata.h>
#include <FirmataConstants.h>
#include <FirmataDefines.h>
#include <FirmataMarshaller.h>
#include <FirmataParser.h>



#include <Wire.h>

const int  Down_buttonPin = 3;

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[4] = {2,3,4,5};

int prevpincheck[4] = {2,3,4,5};

int Score_Pins = (Score_23,Score_24,Score_25);

const int Game_Reset = 2;

int Score = 0000;

int a = 100;

bool bPress = false;

String pin_str = "";



void setup()

{

  Serial.begin(9600);

  digitalWrite(Game_Reset, LOW);
  pinMode(Score_Pins, INPUT);
  pinMode(Game_Reset, OUTPUT);
  pinMode( Down_buttonPin , INPUT_PULLUP);
  pinMode(Game_start, OUTPUT);
  for (int ii = 2;ii<=sizeof(pincheck)/sizeof(pincheck[0]);ii++){
    pinMode(ii,OUTPUT);
  }
}

void loop()

{
     pincheck[Game_start] = digitalRead(Game_start);

   if (digitalRead(Score_Pins) == HIGH)
   {
     delay(500);
     Score = a + Score;
   }
   if (pincheck[Game_start] == HIGH){
        delay(5);
      } 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 = "";
      }
     }
    }

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:

       shotsLeftCheck--;
    }

    // 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;

}

}

Everything is working fine I have it set up and working with a unity application. Other than the scoring system. So I believe My Issue may be with the other arduino. The code below is on the target Arduino.

int test = 8;
void setup()

{
  Serial.begin(9600);
  pinMode( test, OUTPUT);
}

void loop()
{
}

This is just a test to see if I manually connect a wire while the main is set as an input and the slave is set as an output if this will bring the pin as standard input is set to low and output is a high if I am correct. However, When I do this nothing changes and the score remains 0. Any assistance on this?

This code does nothing at all....

 int test = 8;
void setup()

{
  Serial.begin(9600);
  pinMode( test, OUTPUT);
}

void loop()
{
}

besides setting pin 8 as an output...

This is just a test to see if I manually connect a wire while the main is set as an input and the slave is set as an output if this will bring the pin as standard input is set to low and output is a high if I am correct.

i have no clue what that means at all... where do you connect a wire, where is the other end connected, what is the main, what is the slave, what does it mean to have a slave as an output, which pin and why it would become magically an input, why low, why high....

Start from the beginning and describe your system, the wirings, what you are trying to achieve, what the problem is etc...

The code above, the main is going to be controlled with a gun from the shooting gallery. The slave, is going to be a target receiver with IR reading I understand what you are saying and I should have been more clear. The slave currently all I need it to do is read as an output for means of testing. I have actually figured that out. I guess now my main question is how can I get pins 22-53 in an array to be called later in a script?

jonathanrhcooper:
I guess now my main question is how can I get pins 22-53 in an array to be called later in a script?

Not sure what you mean by “called”. If you mean a simple way of accessing the pins you may not need an array at all. If you are using EVERY pin between 22 and 53 then you could just use an offset to access them. For example, if you want to declare them all as output you could do this:

for (byte i = 0; i < 32; i++)
{
  pinMode(i + 22, OUTPUT);
}

or

for (byte i = 22; i < 54; i++)
{
  pinMode(i, OUTPUT);
}

The slave currently all I need it to do is read as an output for means of testing

I find this weird. Usually you read from an INPUT. Do you mean you want to set the state of an OUTPUT pin and the master will read that pin to find out if a target got hit?

Correct The Slave Would bet set as ouput and the aster as inputs I got it worked out with

#include <Boards.h>
#include <Firmata.h>
#include <FirmataConstants.h>
#include <FirmataDefines.h>
#include <FirmataMarshaller.h>
#include <FirmataParser.h>
#include <Wire.h>
const int  Down_buttonPin = 3;
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] = {2,3,4,5};
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;
  }
  pinMode(Score_Pins1, INPUT);
  pinMode(Score_Pins2, INPUT);
  pinMode(Score_Pins3, INPUT);
  pinMode(Game_Reset, OUTPUT);
  digitalWrite(Game_Reset, LOW);
  pinMode( Down_buttonPin , INPUT_PULLUP);
  pinMode(Game_start, OUTPUT);
  for (int ii = 2;ii<=sizeof(pincheck)/sizeof(pincheck[0]);ii++){
    pinMode(ii,OUTPUT);
  
  }
  Serial.println(Score);
}

void pin_status()
{
  for (int i=3;i<54;i++) {
    pincheck[i]=digitalRead(i);    
  }
}

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(5);
      } 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=3;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);
      digitalWrite(Game_start, HIGH);
      Score = 0000;

    }
    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:

       shotsLeftCheck--;
    }

    // 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;

}

}

I will keep updated once i get the ir receiver and transmitter in to have it tested out.

Ok

Make sure grounds are connected

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.