Go Down

Topic: Number two for me... (Read 3972 times) previous topic - next topic

twelch

This is my second project taken to full completion and it is certainly my most coplex to date. It is a pretend gas pump, user can select grade and the pump is triggered by a magnet in the play car. I even used a few trimpots on the board so I can change the price of gas with ease....







and here's a quick video...

https://www.facebook.com/photo.php?v=772319732799694&l=2785802498986255776


twelch

and of course for here's the code....

Code: [Select]
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include <Wire.h>

Adafruit_7segment matrix1 = Adafruit_7segment();
Adafruit_7segment matrix2 = Adafruit_7segment();
Adafruit_7segment matrix3 = Adafruit_7segment();
Adafruit_7segment matrix4 = Adafruit_7segment();
Adafruit_7segment matrix5 = Adafruit_7segment();
// program uses Adafruit 7 segment LED diplays w/ I2C backback


//define pin names
int regularLed = 2;
int superLed = 3;
int premiumLed = 4;
int regularPin = 5;
int superPin = 6;
int premiumPin = 7;
int pumpPin = 8;
int pricePin = 10;
int setLED = 11;

//analog pins for setting current prices using trim pots
int RegularPricePin = 15;
int SuperPricePin = 16;
int PremiumPricePin = 17;

int gradeselected = 0;  //tells us whether a grade has been chosen

int sample = 100;  //number of samples taken from trim pots to normalize readings

//varibles for price and purchase calculations
float RegularPrice = 0.00;
float SuperPrice = 0.00;
float PremiumPrice = 0.00;
float price = 0.00;
float gallons = 0.00;
float total = 0.00;
float RegularSum = 0;
float SuperSum = 0;
float PremiumSum = 0;


void setup()
{
 //i dont't know what this does but it is needed for the I2C backback to work
 #ifndef __AVR_ATtiny85__
 Serial.begin(9600);
 #endif
 
 //initializing the 7 segment displays
 matrix1.begin(0x70); //purchase total
 matrix2.begin(0x71); //gallons per purchase
 matrix3.begin(0x72); //regular
 matrix4.begin(0x73); //super
 matrix5.begin(0x74); //premium
 
 //set all pins to input and connect to internal pull up resistor
 for (int x = 0; x < 20; x++)
 {
   pinMode(x, INPUT_PULLUP);
 }
 
 //reset analog pin for setting prices
 pinMode(15, INPUT);
 pinMode(16, INPUT);
 pinMode(17, INPUT);
 
 //reset pins to output  for LED's
 pinMode(setLED, OUTPUT);
 pinMode(regularLed, OUTPUT);
 pinMode(superLed, OUTPUT);
 pinMode(premiumLed, OUTPUT);
 
 //turn the LEDs on
 digitalWrite(regularLed, HIGH);
 digitalWrite(superLed, HIGH);
 digitalWrite(premiumLed, HIGH);
 
 //all zero's on Gallons and Total screens
 matrix1.writeDigitNum(1,0,true);
 matrix1.writeDigitNum(3,0,true);
 matrix1.writeDigitNum(4,0,true);
 matrix1.writeDigitNum(0,0,true);
 matrix1.writeDigitRaw(2, 0x002);
 matrix1.writeDisplay();
 matrix2.writeDigitNum(1,0,true);
 matrix2.writeDigitNum(3,0,true);
 matrix2.writeDigitNum(4,0,true);
 matrix2.writeDigitNum(0,0,true);
 matrix2.writeDigitRaw(2, 0x002);
 matrix2.writeDisplay();
 
 //set prices for the grades
 delay(500);
 RegularPrice = (analogRead(RegularPricePin) / 100.00);
 delay(500);
 SuperPrice = (analogRead(SuperPricePin) / 100.00);
 delay(500);
 PremiumPrice = (analogRead(PremiumPricePin) / 100.00);
 delay(500);
 
 //display current prices
 matrix3.println(RegularPrice);
 matrix3.writeDigitRaw(2, 0x002);
 matrix3.writeDisplay();
 matrix4.println(SuperPrice);
 matrix4.writeDigitRaw(2, 0x002);
 matrix4.writeDisplay();
 matrix5.println(PremiumPrice);
 matrix5.writeDigitRaw(2, 0x002);
 matrix5.writeDisplay();
 
 //take a break for everything to settle
 delay(100);  
}

void loop()
{
 setprices();  //set prices funtion does nothing unless the "programming" button is held
 
 //if grade selected is regular
 if (digitalRead(regularPin) == LOW)
 {
   //turn off the other lights
   digitalWrite(regularLed, HIGH);
   digitalWrite(superLed, LOW);
   digitalWrite(premiumLed, LOW);
   //Serial.println("Pumping Regular");
   
   //reset gallons and total displays to zero's
   matrix1.println(total);    
   matrix1.writeDigitNum(1,0,true);
   matrix1.writeDigitNum(3,0,true);
   matrix1.writeDigitNum(4,0,true);
   matrix1.writeDigitRaw(2, 0x002);
   matrix1.writeDisplay();
   matrix2.println(gallons);
   matrix2.writeDigitNum(1,0,true);
   matrix2.writeDigitNum(3,0,true);
   matrix2.writeDigitNum(4,0,true);
   matrix2.writeDigitRaw(2, 0x002);
   matrix2.writeDisplay();
   
   //dim display for grades not being used
   matrix3.setBrightness(15);
   matrix4.setBrightness(0);
   matrix5.setBrightness(0);
   
   // prepare variables for new purchase
   gallons = 0;
   price = RegularPrice;
   gradeselected = 1;
 }
 
 // if grade selected is super
 if (digitalRead(superPin) == LOW)
 {
   //turn of the other lights
   digitalWrite(regularLed, LOW);
   digitalWrite(superLed, HIGH);
   digitalWrite(premiumLed, LOW);
   //Serial.println("Pumping Super");
   
   //reset gallons and total display to all zero's
   matrix1.println(total);
   matrix1.writeDigitNum(1,0,true);
   matrix1.writeDigitNum(3,0,true);
   matrix1.writeDigitNum(4,0,true);
   matrix1.writeDigitRaw(2, 0x002);
   matrix1.writeDisplay();
   matrix2.println(gallons);
   matrix2.writeDigitNum(1,0,true);
   matrix2.writeDigitNum(3,0,true);
   matrix2.writeDigitNum(4,0,true);
   matrix2.writeDigitRaw(2, 0x002);
   matrix2.writeDisplay();
   
   //dim displays for grades not being used
   matrix3.setBrightness(0);
   matrix4.setBrightness(15);
   matrix5.setBrightness(0);
   
   //prepare variables for new purchase
   gallons = 0;
   price = SuperPrice;
   gradeselected = 1;
 }
 
 // if grade selected is premium
 if (digitalRead(premiumPin) == LOW)
 {
   //turn off the other lights    
   digitalWrite(regularLed, LOW);
   digitalWrite(superLed, LOW);
   digitalWrite(premiumLed, HIGH);
   //Serial.println("Pumping Premium");
   
   //reset grade and total displays to zeros
   matrix1.println(total);
   matrix1.writeDigitNum(1,0,true);
   matrix1.writeDigitNum(3,0,true);
   matrix1.writeDigitNum(4,0,true);
   matrix1.writeDigitRaw(2, 0x002);
   matrix1.writeDisplay();
   matrix2.println(gallons);
   matrix2.writeDigitNum(1,0,true);
   matrix2.writeDigitNum(3,0,true);
   matrix2.writeDigitNum(4,0,true);
   matrix2.writeDigitRaw(2, 0x002);
   matrix2.writeDisplay();
   
   //dim displays for grades not being used
   matrix3.setBrightness(0);
   matrix4.setBrightness(0);
   matrix5.setBrightness(15);
   
   //prepare variables for new purchase
   gallons = 0;
   price = PremiumPrice;
   gradeselected = 1;
 }
 
 //if pump handle is placed in the car (used magnetic reed sensor
 if ((digitalRead(pumpPin) == LOW) && (gradeselected == 1))
 {
   pumping();//funtion to simulate fillin' 'er up!
 }
}

void pumping()
{
 //run as long as pump stays in car maxes out at 16 gallons
 while((digitalRead(pumpPin) == LOW) && (gallons < 16))
 {
   gallons = gallons + .1; //increment gallons by .1
   total = gallons * price; //calculate current total
   
   //display purchase total
   matrix1.println(total);
   matrix1.writeDigitRaw(2, 0x002);
   matrix1.writeDisplay();
   
   //display gallons for current purchase
   matrix2.println(gallons);
   matrix2.writeDigitRaw(2, 0x002);
   matrix2.writeDisplay();
   
   //slow it down to be realistic
   delay(100);
 }
 tone(9,400,1000);  //play a sound to indicate end of purchase
 //Serial.println("DONE");
 gradeselected = 0; //reset to no grade selected
 
 //turn all lights back on
 digitalWrite(regularLed, HIGH);
 digitalWrite(superLed, HIGH);
 digitalWrite(premiumLed, HIGH);
 
 //turn all displays back up
 matrix3.setBrightness(15);
 matrix4.setBrightness(15);
 matrix5.setBrightness(15);
 
 //reset for next purchase
 total = 0.00;
 gallons = 0.00;
 
}

void setprices()
{
 //runs if programing button is held in
 while((digitalRead(pricePin) == LOW))
 {
   digitalWrite(setLED , HIGH); //LED on pcb indicates programming mode is active
   
   //takes set number of readings from regular trim pot and calculates average
   for (int x = 0; x < sample; x++)
   {
     RegularSum += (analogRead(RegularPricePin) / 100.00);
   }
   RegularPrice = RegularSum / sample;
   RegularSum = 0; //reset for next sampling
   
   //takes set number of readings from super trim pot and calculates average
   for (int x = 0; x < sample; x++)
   {
     SuperSum += (analogRead(SuperPricePin) / 100.00);
   }
   SuperPrice = SuperSum / sample;
   SuperSum = 0;//reset for next sampling
   
   //takes set number of readings from premium trim pot and calculates average
   for (int x = 0; x < sample; x++)
   {
     PremiumSum += (analogRead(PremiumPricePin) / 100.00);
   }
   PremiumPrice = PremiumSum / sample;
   PremiumSum = 0;//reset for next sampling
   
   //display current prices
   matrix3.println(RegularPrice);
   matrix3.writeDigitRaw(2, 0x002);
   matrix3.writeDisplay();
   matrix4.println(SuperPrice);
   matrix4.writeDigitRaw(2, 0x002);
   matrix4.writeDisplay();
   matrix5.println(PremiumPrice);
   matrix5.writeDigitRaw(2, 0x002);
   matrix5.writeDisplay();
   delay(100); //don't get ahead of your self!
 }
 digitalWrite(setLED, LOW); //turn LED off to indicate programming mode exit
}


any thing you see that you might now a simpler way to do I would love to here about.

Paul__B


I even used a few trimpots on the board so I can change the price of gas with ease....


You will definitely need that feature to maintain realism.   XD

twelch

I considered setting the prices to "arm", "leg", "soul" but figured kids wouldn't get the joke...

Jantje


I considered setting the prices to "arm", "leg", "soul" but figured kids wouldn't get the joke...

rofl
You would not be ale to fill up as you only have 2 arms  ]:D So give me your soul  ]:D

Jantje
Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Go Up