Pinball Machine: Programming the Machine

This page shows how to program the Adruino and the sensors in order to register a point system for the game. The programming also shows how the machine is signaled when the game ends, the ball falls past the flippers three times, and how to use different sensors to generate points for the scoring mechanism. Each individual code for each sensors is displayed on this page and should be tested for each sensor in order to make sure that the sensors work. After each sensor is tested, the code which utilizes multiple sensors should be used.

Programming the sensors
  Code that will allow you to generate points using the flex sensors
//FLEX SENSOR CODE
// INSTRUCTIONS:
// Upload this sketch to your Arduino, then activate the Serial Monitor

Flex sensor set up
// (set the Serial Monitor to 9600 baud)
int counter = 0;
void setup()
{
    // initialize serial communications
    Serial.begin(9600); 
}
void loop()
{
    int sensor, degrees
    // read the voltage from the voltage divider (sensor plus resistor)
    sensor = analogRead(0);
    // convert the voltage reading to inches
    // the first two numbers are the sensor values for straight (768) and bent (853)
    // the second two numbers are the degree readings we'll map that to (0 to 90 degrees)
degrees = map(sensor, 768, 853, 0, 90);
    // note that the above numbers are ideal, your sensor's values will vary
    // to improve the accuracy, run the program, note your sensor's analog values
    // when it's straight and bent, and insert those values into the above function.
if (sensor > 0){
Setup for multiple sensors that have same input
    counter = counter + 1000;
}
else {
  counter = counter;
}
    // print out the result
    Serial.print("score: ");
    Serial.print(counter,DEC);
    Serial.print(" analog input: ");
    Serial.print(sensor,DEC);
    Serial.print(" degrees: ");
    Serial.println(degrees,DEC);
    // pause before taking the next reading
    delay(100);


}
Coding using the ultrasonic sensor that will signal when the ball has passed three times
#define trigPin 12
#define echoPin 11

Set up for Ultrasonic sensor
int ledPin = 13;
int counter = 0;
int score = 0;

void setup() {
  Serial.begin (9600);
  pinMode(ledPin, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  if (distance < 4) {
    counter = counter + 1;
    Serial.print (counter);
    Serial.print('\n');
    score = score + 100;
    Serial.print (score);
    Serial.print('\n');
  }
  else {
 counter = counter;
  }
if (counter < 3) {
digitalWrite(ledPin, HIGH);
}
 else {
   counter = 0;
   score = 0;
   digitalWrite (ledPin, LOW);
 }

  delay(150);
  }

Coding for the touch sensors that will be used on the three doors the player has to hit

int fsrAnalogPin = 0; // FSR is connected to analog 0
int LEDpin = 11;      // connect Red LED to pin 11 (PWM pin)
int fsrReading;      // the analog reading from the FSR resistor divider
int LEDbrightness;

void setup(void) {
  Serial.begin(9600);   // We'll send debugging information via the Serial monitor
  pinMode(LEDpin, OUTPUT);
}

void loop(void) {
  fsrReading = analogRead(fsrAnalogPin);
  Serial.print("Analog reading = ");
  Serial.println(fsrReading);

  // we'll need to change the range from the analog reading (0-1023) down to the range
  // used by analogWrite (0-255) with map!
  LEDbrightness = map(fsrReading, 0, 1023, 0, 255);
  // LED gets brighter the harder you press
  analogWrite(LEDpin, LEDbrightness);

  delay(100);
}


No comments:

Post a Comment