0

I have a project in arduino, an arcade basketball machine using 4" Single-7-Segment Displays driven by IC 74HC595N. The purpose of my program is to record the score by merging 2 SSDs at the same time counts down using the last 2 SSDs.

The whole process of my system is this:

  1. Input: Relay + Button
  2. Output: Triggers a 5V servo motor from another system then a 12V DC Motor will lower the panel stopping the balls
  3. Output: Counts down from 60 while scoring and after that the 12V Motor will risen again to stop the balls.

My problem is: How can I merge this two scripts without interrupting the Timer() function while Scorer() is running too.

Thank you!

Timer Script:

#include <Servo.h>
boolean interrupt = false;
int z         = 0;
int u         = 45 ;
int dw        = 130;
int motor_open = 6; //positive
int motor_close = 7; //negative
int button    = 5; //LED Red Button
int led       = 4; // LED red NPN
int relay     = 17; //from Rpi
int stop_close = 36; //rise stopper
int stop_open = 34; //lows stopper 
unsigned long previousMillis = 0;        // will store last time LED was updated

const long interval = 20000;  

int latchpin  = 8; // connect to pin 12 on the '595
int clockpin  = 12; // connect to pin 11 on the '595
int datapin   = 13;


enum {OPEN, CLOSED, OPENING, CLOSING, UNKNOWN};
byte get_roof_status()
{
    if (digitalRead(stop_close) == LOW) return CLOSED;
    else if (digitalRead(stop_open) == LOW) return OPEN; // Fixed typo
    else if (digitalRead(motor_open) == HIGH) return OPENING;
    else if (digitalRead(motor_close) == HIGH) return CLOSING;
    return UNKNOWN;
}

float b       = 0;
int   c       = 0;  
float d       = 0;
int   e       = 0;
int   speed   = 500; // used to control speed of counting
int   segdisp[10] = { 3,159,37,13,153,73,65,27,1,9 };

boolean Bpressed = false;
boolean Rpressed;
Servo myservo;   


void open_roof() {
    digitalWrite(motor_close, LOW);
    digitalWrite(motor_open, HIGH);
    // Loop until roof is open
    while (OPEN != get_roof_status()) /* NULL */;
    stop();
}
void close_roof() {
    digitalWrite(motor_open, LOW);
    digitalWrite(motor_close, HIGH);
    // Loop until roof is closed
    while (CLOSED != get_roof_status()) /* NULL */;
    stop();
}
void stop() {

    digitalWrite(motor_open, LOW);
    digitalWrite(motor_close, LOW);
}

void setup()
{
  pinMode(led, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  pinMode(relay, INPUT_PULLUP);
  pinMode(motor_open, OUTPUT);
  pinMode(motor_close, OUTPUT);
  pinMode(stop_close, INPUT_PULLUP);
  pinMode(stop_open, INPUT_PULLUP);
  pinMode(latchpin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  pinMode(datapin, OUTPUT);
  digitalWrite(led, LOW);
  myservo.attach(30); 
  myservo.write(u);
  open_roof();
  timer0();
  delay(25000);
  Rpressed = false;

}

void loop() {

  if(digitalRead(relay) == LOW && Rpressed == false){
    Rpressed = true;
  digitalWrite(led, HIGH);
     servo();
  delay(150);
  }
  if(digitalRead(button) == LOW && Rpressed == true){
      Bpressed = true;
  delay(150);    
  }

  if(Bpressed == true) { 
      digitalWrite(led, LOW);
      delay(100);
    close_roof();
      delay(500);   
    timer(60); 
      delay(1000);
   open_roof();
        }
  }

void servo(){
 myservo.write(dw);
 delay(2000);
 myservo.write(u);
 delay(1000);
}

void timer0() {
    digitalWrite(latchpin, LOW);
    shiftOut(datapin, clockpin, LSBFIRST, segdisp[0]); // clears the right display
    shiftOut(datapin, clockpin, LSBFIRST, 255); // clears the left display
    digitalWrite(latchpin, HIGH);
}

void timer(int t) {

  for (int z=t; z>=0; z--)
  {
    digitalWrite(latchpin, LOW);
    shiftOut(datapin, clockpin, LSBFIRST, 255); // clears the right display
    shiftOut(datapin, clockpin, LSBFIRST, 255); // clears the left display
    digitalWrite(latchpin, HIGH);

    if (z<10)
    {
      digitalWrite(latchpin, LOW);
      shiftOut(datapin, clockpin, LSBFIRST, segdisp[z]); // sends the digit down the serial path
      shiftOut(datapin, clockpin, LSBFIRST, 255); // sends a blank down the serial path to push the digit to the right
      digitalWrite(latchpin, HIGH);
    }
    if (z==0){
      Bpressed = false;
      Rpressed = false;
      return;
    }
    else if (z>=10)
    {
      d = z%10; // find the remainder of dividing z by 10, this will be the right-hand digit
      c = int(d); // make it an integer, c is the right hand digit
      b = z/10; // divide z by 10 - the whole number value will be the left-hand digit
      e = int(b); // e is the left hand digit
      digitalWrite(latchpin, LOW); // send the digits down to the shift registers!
      shiftOut(datapin, clockpin, LSBFIRST, segdisp[c]); 
      shiftOut(datapin, clockpin, LSBFIRST, segdisp[e]); 
      digitalWrite(latchpin, HIGH);
    }
    else{
      delay(speed);
    }

    delay(1000);
  }
}

Scorer Script:

const long interval = 70000 ;      

int y         = 0;
int sensor    = 5;
int sensor2   = 4;
int button    = 3;
int latchpin2 = 12; // connect to pin 12 on the '595
int clockpin2 = 11; // connect to pin 11 on the '595
int datapin2  = 13;// connect to pin 14 on the '595

float b       = 0;
int   c       = 0;  
float d       = 0;
int   e       = 0;
int   speed   = 2000; // used to control speed of counting
int   segdisp[10] = { 3,159,37,13,153,73,65,27,1,9 };

boolean Bpressed = false;  
boolean scoretiming = false;
void setup()
{
  pinMode(button, INPUT_PULLUP);
  pinMode(latchpin2, OUTPUT);
  pinMode(clockpin2, OUTPUT);
  pinMode(datapin2, OUTPUT);
  pinMode(sensor, INPUT_PULLUP);
  pinMode(sensor2, INPUT_PULLUP);

  timer0();
}

void loop() {

  if(digitalRead(button) == HIGH && scoretiming == false) {
  Bpressed = true;   
  y = 0;
  scorer();
  scoretiming = true;
  delay(150);

  }

unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval && scoretiming == true) {
    previousMillis = currentMillis;

    if (scoretiming == true) {
      scoretiming = false;
    } else {
      scoretiming = true;

    }
    return scoretiming;
    }





   if( digitalRead(sensor) == LOW && digitalRead(sensor2) == LOW && Bpressed == true ) {      
      y = (y < 99) ? y+1 : 0;
      scorer();  
      delay(500);

      if(y>99){
          Bpressed = false;
          return; 
      }
    }


}
void timer0() {
    digitalWrite(latchpin2, LOW);
    shiftOut(datapin2, clockpin2, LSBFIRST, segdisp[0]); // clears the right display
    shiftOut(datapin2, clockpin2, LSBFIRST, segdisp[0]); // clears the left display
    digitalWrite(latchpin2, HIGH);
}

void scorer() {


  digitalWrite(latchpin2, LOW);
  shiftOut(datapin2, clockpin2, LSBFIRST, segdisp[y % 10]);  // ones digit
  shiftOut(datapin2, clockpin2, LSBFIRST, segdisp[y / 10]);  // tens digit
  digitalWrite(latchpin2, HIGH);

  return ;
}

Is it possible to write the If() function of Scorer() while ForLoop() of Timer() is counting down?

Please Help me! Thank you

  • Yes it's possible to write the If() function of Scorer() while ForLoop() of Timer() is counting down. What have you tried so far? – Filip Franik Jan 17 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.