Pages: [1]   Go Down
Author Topic: My 1 LED Segment Code.  (Read 431 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Faraday Member
**
Karma: 30
Posts: 2532
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

LOL - I thought i'd write one from scratch using no help at all just to test myself and how well i'm getting used to C.

Code:
//written by Craig Capel, free to use, this is using an 8 Segment LED digit thingy
//I simply have pins 2 through to pin 9
//pin 2, pin1 (bottom left of the LED Segment)
//pin 3, pin2 .... and so on. when you get to the last pin at the bottom of the LED segment
//pin 6 goes top left of the LED segment, pin 7, next to it...


byte v0[] = {2,3,4,7,9,8,0};   //0 Value
byte v1[] = {9,4,0};            //1 etc....
byte v2[] = {8,9,5,2,3,0};
byte v3[] = {8,9,5,4,3,0};
byte v4[] = {7,9,5,4,0};
byte v5[] = {8,7,5,4,3,0};
byte v6[] = {7,2,3,4,5,0};
byte v7[] = {8,9,4,0};
byte v8[] = {2,3,4,5,7,8,9,0};
byte v9[] = {4,9,8,7,5,0};

//byte v2[] = {8,9,5,2,3};
//byte v3[] = {2,3,4,5,6,7,8,9};

byte LEDValue = 0;

void setup()
{
 int n;
 Serial.begin(9600);
 for(n=2; n<=9; n++)
  {
    pinMode(n,OUTPUT);   
    digitalWrite(n,HIGH);
 //   Serial.println(n);
 //   delay(4000);
 //   digitalWrite(n,LOW);
   
  } 
}


void loop()

//  PatternSeq1();
// for(int n=2; n<=9; n++)
 //  digitalWrite(n,HIGH);
 // delay(2000);
  if(LEDValue==10) LEDValue=0;
  Serial.print(LEDValue); 
  Serial.print(":");
//  Serial.println(
  Display(LEDValue);
  delay(500);   
  LEDValue++; 

}




int terminator(byte req[])
{
  int n = 0;
  int v = 0;
  do
  {
    v = req[n];
//     Serial.print("array index: ");
//   Serial.print(n);
//    Serial.println();
//   Serial.print("Value: ");
//   Serial.println(v);   //does not stop when it hits 0 in the array?!
//   delay(100);
//   Serial.println("!!!!");
   n++;
   } while(v!=0);

 // Serial.print("return elements: ");
 // Serial.println(n-1);
  // delay(2000);
  return n-1; 
}

void Display(byte Val)
{
  int items,V,n = 0;
 
  Clear();
 
    if (Val==0) { items=terminator(v0);  for(n=0; n<items; n++)  digitalWrite(v0[n],HIGH); }       
    if (Val==1) { items=terminator(v1);  for(n=0; n<items; n++)  digitalWrite(v1[n],HIGH); }           
    if (Val==2) { items=terminator(v2);  for(n=0; n<items; n++)  digitalWrite(v2[n],HIGH); }       
    if (Val==3) { items=terminator(v3);  for(n=0; n<items; n++)  digitalWrite(v3[n],HIGH); }       
    if (Val==4) { items=terminator(v4);  for(n=0; n<items; n++)  digitalWrite(v4[n],HIGH); }       
    if (Val==5) { items=terminator(v5);  for(n=0; n<items; n++)  digitalWrite(v5[n],HIGH); }       
    if (Val==6) { items=terminator(v6);  for(n=0; n<items; n++)  digitalWrite(v6[n],HIGH); }       
    if (Val==7) { items=terminator(v7);  for(n=0; n<items; n++)  digitalWrite(v7[n],HIGH); }       
    if (Val==8) { items=terminator(v8);  for(n=0; n<items; n++)  digitalWrite(v8[n],HIGH); }           
    if (Val==9) { items=terminator(v9);  for(n=0; n<items; n++)  digitalWrite(v9[n],HIGH); }           
//   delay(5000);
}



void Clear()
{
  int n;
  for(n=2; n<=9;)
  {
    digitalWrite(n,LOW);
//    delay(90);
    n++;
  }
}

void PatternSeq1()
{
  int n;
  for(n=2; n<=9; n++)
  {
    digitalWrite(n,HIGH);
    delay(500);
  }
 
 
 for(n=9; n>=2; n--)
  {
    digitalWrite(n,LOW);
    delay(500);
  }
 
 
}


once compiled, it counts from 0 - 9 and outputs it using pins 2-9 (giving a sequence from 0..9)


OK so it works, but now what lol, i have a way of producing a number, 10?..9?...?8 last 10 seconds before new year?... bit of fun smiley


also, posting my code here is a good way of keeping a backup smiley
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 130
Posts: 8623
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
a good way of keeping a backup
Not necessary, in 6 months time you'll look back at the code and realise it could be done in maybe 5 lines smiley

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Norfolk UK
Offline Offline
Faraday Member
**
Karma: 71
Posts: 2614
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
posting my code here is a good way of keeping a backup
Get a Dropbox (or other similar) account. Free for up to 2 gigabytes. It's a great help to me as I work on 3 separate Pc's so no more problems remembering to backup from work PC to restore to home PC.
Logged


Offline Offline
Faraday Member
**
Karma: 30
Posts: 2532
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it's not my fault C is so convoluted, i'd have managed it in 10 minutes If i had access to a Pascal compiler for the Atmega chip.
Logged

Offline Offline
Faraday Member
**
Karma: 30
Posts: 2532
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

cool, i've not tried dropbox, i use box.net (up to 2 gig) i was mainly referring to useless code i write and placing it here smiley

Code:
byte v0[] = {2,3,4,7,9,8,0};   //0 Value
byte v1[] = {9,4,0};            //1 etc....
byte v2[] = {8,9,5,2,3,0};
byte v3[] = {8,9,5,4,3,0};
byte v4[] = {7,9,5,4,0};
byte v5[] = {8,7,5,4,3,0};
byte v6[] = {7,2,3,4,5,0};
byte v7[] = {8,9,4,0};
byte v8[] = {2,3,4,5,7,8,9,0};
byte v9[] = {4,9,8,7,5,0};



byte LEDValue = 0;

void setup()
{
 int n;
 Serial.begin(9600);
 for(n=2; n<=9; n++)
    pinMode(n,OUTPUT);   
  PatternSeq1();
}


void loop()

  if(LEDValue==10) LEDValue=0;
  Serial.print(LEDValue); 
  Serial.print(":");
  Display(LEDValue);
  delay(500);   
  LEDValue++; 

}




int terminator(byte req[])
{
  int n = 0;
  int v = 0;
  do
  {
    v = req[n];
    n++;
   } while(v!=0);
  return n-1; 
}

void Display(byte Val)
{
  int items,V,n = 0;
 
  Clear();
 
    if (Val==0) { items=terminator(v0);  for(n=0; n<items; n++)  digitalWrite(v0[n],HIGH); }       
    if (Val==1) { items=terminator(v1);  for(n=0; n<items; n++)  digitalWrite(v1[n],HIGH); }           
    if (Val==2) { items=terminator(v2);  for(n=0; n<items; n++)  digitalWrite(v2[n],HIGH); }       
    if (Val==3) { items=terminator(v3);  for(n=0; n<items; n++)  digitalWrite(v3[n],HIGH); }       
    if (Val==4) { items=terminator(v4);  for(n=0; n<items; n++)  digitalWrite(v4[n],HIGH); }       
    if (Val==5) { items=terminator(v5);  for(n=0; n<items; n++)  digitalWrite(v5[n],HIGH); }       
    if (Val==6) { items=terminator(v6);  for(n=0; n<items; n++)  digitalWrite(v6[n],HIGH); }       
    if (Val==7) { items=terminator(v7);  for(n=0; n<items; n++)  digitalWrite(v7[n],HIGH); }       
    if (Val==8) { items=terminator(v8);  for(n=0; n<items; n++)  digitalWrite(v8[n],HIGH); }           
    if (Val==9) { items=terminator(v9);  for(n=0; n<items; n++)  digitalWrite(v9[n],HIGH); }           
}



void Clear()
{
  int n;
  for(n=2; n<=9;)
  {
    digitalWrite(n,LOW);
//    delay(90);
    n++;
  }
}

void PatternSeq1()
{
  int n;
  for(n=2; n<=9; n++)
  {
    digitalWrite(n,HIGH);
    delay(500);
  }
 
 
 for(n=9; n>=2; n--)
  {
    digitalWrite(n,LOW);
    delay(500);
  }
 
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 130
Posts: 8623
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
10 minutes If i had access to a Pascal compiler
So that explains your forum name then smiley

Yes C is not straightforward to learn, but it's worth the effort.

_____
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Pages: [1]   Go Up
Jump to: