Go Down

Topic: Intro with a twist (Read 93 times) previous topic - next topic

kpmzam

Hi guys, first post...
I'm finishing an electronics degree with a capstone project soon, so figured I may need guidance at some point, and might as well start out by showing off a project and contributing some functioning code to the community.  :smiley-money:
Apologies if this thread is misplaced, I didn't see a finished project (bragging) section.

http://youtu.be/ZnX4I5hxbjY
This was my final project I did at the end of Summer '14 for the Embedded systems class. It's an RPM counter that displays an accurate measurement in MPH. It's mostly designed by me, but I spliced different bits of code together until it worked. I would've copy-pasted the entire thing if it were available, but since it wasn't I ended up learning a lot. It was originally intended to be used on a bicycle and took me like 18 solid hours to complete. The project is pretty much over at this point, but I was wondering if people had suggestions to make the code more efficient, or even just wanted to use it for themselves. I'm not sure where the schematic for the hardware went, or if I even made one.
Cheers.


Code: [Select]
/*Bike Tachometer with Multiplexed 2-digit 7 segment display
    Arduino pin    7 segment
        2              a
        3              b
        4              c
        5              d
        6              e
        7              f
        8              g
        9              dp(dot pin)
        10             digit 0
        11             digit 1
*/
byte digit0 = 4; //tens
byte digit1 = 3; //ones
byte dotPin = 13;

int count, count1, count2, a, b = 0; //initialize count variables

//long lastTime, millisTime, deltaTime = 0; //initialize hardware clock that is unaffected by software delays


//array for segment pins
byte sevenSegmentPins[] = {6,7,8,9,10,11,12};

//2D array for numbers
byte sevenSegment[10][7] = {
    //a b c d e f g
  { 1,1,1,1,1,1,0 },  // = 0
  { 0,1,1,0,0,0,0 },  // = 1
  { 1,1,0,1,1,0,1 },  // = 2
  { 1,1,1,1,0,0,1 },  // = 3
  { 0,1,1,0,0,1,1 },  // = 4
  { 1,0,1,1,0,1,1 },  // = 5
  { 1,0,1,1,1,1,1 },  // = 6
  { 1,1,1,0,0,0,0 },  // = 7
  { 1,1,1,1,1,1,1 },  // = 8
  { 1,1,1,1,0,1,1 }   // = 9

};


//speedometer
float start, finished;
float elapsed, time;
float circ=7.2;       // wheel circumference relative to sensor position (in meters)
float speedmph;       // holds calculated speed values in imperial
float ft2mi=0.00136; //=7.2ft/5280
float ms2hr=3600000; //ms per hour ft2mi/(elapsed/ms2hr)

void setup()
{
   pinMode(dotPin, OUTPUT); //pin 13
   pinMode(digit0, OUTPUT); //pin 4
   pinMode(digit1, OUTPUT); //pin 3                               
//   Serial.begin(9600);
   
   //make segment pins an OUTPUT
   for(int i=0; i<7; i++)
  {
    pinMode(sevenSegmentPins[i], OUTPUT);
  }
 
  //speedometer
  attachInterrupt(0, speedCalc, FALLING); // interrupt called when sensors sends digital 2 low (every wheel rotation)
  start=millis();
  pinMode(2, INPUT); // sensor on pin 4
}


//function that writes numbers to 7seg according to the 2D array for numbers
void segmentWrite(byte digit)
{
  byte pin = 6;
  for (byte i=0; i<7; i++)
  {
    digitalWrite(pin, sevenSegment[digit][i]);
      ++pin;
  }
}

//function that counts milliseconds and calculates MPH
void speedCalc()
{
  elapsed=millis()-start;
  start=millis();
  count=ft2mi*(ms2hr/elapsed); // original was (3600*circ)/elapsed
}

void loop()
{

  digitalWrite(dotPin, HIGH);
  digitalWrite(digit0, HIGH);
  digitalWrite(digit1, HIGH);
 
  //disect the count variable to get ones and tenths
  a = count % 10; //get the modulo(remainder) of count then
    count1 = int (a); //convert it to integer
  b = count / 10; //divide count by 10 then
    count2 = int (b); //convert it to integer
 
  //multiplexing technique
  if (count <= 99)   
    {
      digitalWrite(digit1, LOW);
      segmentWrite(count1);
      delay(10);
      digitalWrite(digit1, HIGH);
      digitalWrite(digit0, LOW);
      segmentWrite(count2);
      delay(10);
      digitalWrite(digit0, HIGH);
    }
//      //count up using hardware timer
//      millisTime = millis();
//      deltaTime = millisTime-lastTime;
//      if (deltaTime>100)
//       {
//         count++;
//         lastTime = millisTime;
//       }
    //reset the counter once the elapsed time is greater than 5 seconds
    if(millis() - start > 5000)  //start = time of last pulse, millis() = time since board turned on, -5000 = difference (start - millis() < -5000)
    {                            //can't use elapsed since it's calculated after a pulse triggered interupt
    count = 00;
    }
//  Serial.println(count);
//  Serial.println(elapsed);
//  delay(1000);

}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy