Go Down

Topic: My 1 LED Segment Code. (Read 525 times) previous topic - next topic

cjdelphi

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: [Select]

//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 :)


also, posting my code here is a good way of keeping a backup :)

Graynomad

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 :)

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Riva

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.
I might seem like an idiot but remember it takes one to know one.
Forum user generally fall into three categories (the good, the bad & the a**hole) what category do you fit in?

cjdelphi

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.

cjdelphi

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 :)

Code: [Select]

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);
  }


Graynomad

Quote
10 minutes If i had access to a Pascal compiler

So that explains your forum name then :)

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

_____
Rob
Rob Gray aka the GRAYnomad www.robgray.com

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