ALANNA MANFREDINI'S DESIGN KITCHEN
  • Home
  • About
  • Machine Learning
  • Robotics
  • PINBALL MACHINE
  • DESIGN
  • Math

PINBALL MACHINE

Integration of Code Parts

12/18/2022

0 Comments

 
I have combined all of my previous code into a single script. There are no delays included so any button presses will not be missed due to the code waiting at the delay. The lights are activated once the points reach a certain threshold and the flipper drops from 50V to 5V to avoid the coil burning out (these are both described in more detail in previous blog posts).

In future, I would like to change my code to switch statements so the if/else statements that have already been confirmed do not need to be asked each loop. Additionally, since the Arduino uno does not have enough pins for the full pinball machine I would like to eventually use shift registers to control the different pins.

Working Code with Lights, Point Calculation, and Flipper Actuation 

To see code please press Read More --->

//FLIPPER VARIABLES
int LsolenoidPin = 6;     // pin for left solenoid
int LbuttonPin = 2;       //pin for the left flipper button
bool pushed = LOW;        //value of button pin when the flipper button is pushed
bool firstLoop = true;

                          // these are global variables that counts the initial time the flipper button has been pushed
int LFlipperTimer = 0;
int RFlipperTimer = 0;


int actuationValue = 255; //voltage needed to actuate the flipper
int actuationTime = 1000;      //length of time the flipper is actuated for




//ROLLOVER VARIABLES
int ptTally = 0;          // total point tally
int Rollover100val = 4;   //pin number for the rollover that gives a point addition of 100





//LIGHT VARIABLES
int LED1 = A5;            //pin numbers for LEDS
int LED2 = A4;
int LED3 = A3;
bool lightMode = false;   //initialise the lights as not flashing              SHOULD THIS BE DONE IN SETUP INSTEAD????
int lightTimer = 0;       //variable that counts the initial time the lights went on
int lightFlashTime = 250;// how long the lights are on for
int lightIndex = 1; //says whether it is the first time we are in the light loop








void setup() {
  //SETUP FLIPPERS
  
  //establish whether the pin for the solenoid and button are an input or an output
  pinMode(LsolenoidPin, OUTPUT);
  pinMode(LbuttonPin, INPUT);
  //ensure the solenoids start off w/ no voltage
  analogWrite(LsolenoidPin, 0);

  /*//Right version of above
    pinMode(RsolenoidPin,OUTPUT);
    pinMode(RbuttonPin, INPUT);
    analogWrite(RsolenoidPin, 0);
  */


  //SETUP ROLLOVERS
  //establish input vs output for the pins
  pinMode(Rollover100val, INPUT);



  //SETUP LIGHTS
  //establish input vs output for the pins
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
  pinMode(LED3,OUTPUT);

  //make sure all the lights are off initially
  digitalWrite(LED1,LOW);
  digitalWrite(LED2,LOW);
  digitalWrite(LED3,LOW);




  

  Serial.begin(9600);                                       //WHAT DOES THIS MEAN
  Serial.println("Program Started");
}





void loop() {
  //Serial.println(LFlipperTimer);
  
  // if left flipper is pushed or has been pushed in the past (so the timer is running) call the actuate flipper command for the left flipper
  if (digitalRead(LbuttonPin) == pushed || LFlipperTimer != 0) {
    LFlipperTimer = actuateFlipper(LsolenoidPin, LbuttonPin, LFlipperTimer);
  }


  // if right flipper is pushed call the actuate flipper command for the right flipper
  /*if(digitalRead(RbuttonPin) == pushed){
     actuateFlipper(RsolenoidPin,RbuttonPin, RFlipperTimer);
    }*/
  

  //when the 100 pt rollover is hit, add pts
  if (digitalRead(Rollover100val) == pushed) {
    addPts(100);
  }

  //display the point tally


  //display fun things


  //turn LED Underground on


  //EM coil


  //timing belt

  if(ptTally > 10000 && ptTally < 20000) {
    lightMode = true;
  }

  if(ptTally > 20000) {
    lightMode = false;
  }

  //go into flashing light mode
  if(lightMode == true) {
    lightsGo();
  }

  
  //come out of flashing light mode
  if(lightMode == false) {
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,LOW);
    digitalWrite(LED3,LOW);
  }
}



//add points to the total point tally
void addPts(int ptNum)  {
  ptTally = ptTally + ptNum;
  Serial.println(ptTally);
}




void lightsGo() {
  if(lightIndex == 1){
    lightTimer = millis();
    lightIndex = 0;
  }
  //for lightFlashTime amount of milliseconds have the first LED on
  if(millis() <= lightTimer + lightFlashTime){
    digitalWrite(LED1,HIGH);
    digitalWrite(LED2,LOW);
    digitalWrite(LED3,LOW);
  }
  
  //for lightFlashTime amount of milliseconds have the second LED on - after 1st has been on
  if(millis() > lightTimer + lightFlashTime && millis() <= lightTimer + 2*lightFlashTime){
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,HIGH);
    digitalWrite(LED3,LOW);
  }


  //for lightFlashTime amount of milliseconds have the second LED on - after 2nd has been on
  if(millis() > lightTimer + 2*lightFlashTime && millis() <= lightTimer + 3*lightFlashTime){
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,LOW);
    digitalWrite(LED3,HIGH);
  }

  //reinitialise current time so that I don't have to do modulo
  if(millis() > lightTimer + 3*lightFlashTime) {
    lightTimer = millis();
    lightIndex = 1;
  }
}



int actuateFlipper(int solenoidPin, int buttonPin, int flipperTimer) {
  //Serial.println(digitalRead(buttonPin));
  //turn the solenoid onto actuation "high volt" value
  // to stop it from actuating each time, make sure this is the first instance of the button being in the state pushed
  if (firstLoop == true) {
    //Serial.println("a");
    //to start the actuation, put the voltage up to actuation val
    analogWrite(solenoidPin, actuationValue);
    //start time                                                          MAKE SURE THIS IS CHANGING THE GLOBAL VARIABLE
    flipperTimer = millis();
    firstLoop = false;
  }


  //if the flipper button is no longer pushed make voltage back to zero and reinitialise the flipper timer
  if (digitalRead(buttonPin) != pushed) {
    //Serial.println("b");
    // drop voltage to zero
    analogWrite(solenoidPin, 0);
    //reinitialise flipper timer
    flipperTimer = 0;
    firstLoop = true;
  }

  //immediately drop to "low volt" value while the solenoid is still being held
  if (digitalRead(buttonPin) == pushed && millis() >= flipperTimer + actuationTime) {
    //Serial.println("c");
    //drop to low volt value in solenoid
    analogWrite(solenoidPin, 100);
    delay(10);                        //idk why i have to have this, else the value must fluctuate - try removing once soldered
  }
  
 return flipperTimer;
  
}

0 Comments



Leave a Reply.

    Alanna Manfredini

    A blog of my progress and process of making a Pinball Machine

Site powered by Weebly. Managed by Porkbun
  • Home
  • About
  • Machine Learning
  • Robotics
  • PINBALL MACHINE
  • DESIGN
  • Math