This wiki has been archived and made read-only.
For up-to-date information about TkkrLab and it's projects please visit our main website at tkkrlab.nl.

Defcon

From

Revision as of 22:12, 28 June 2011 by Zeno (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Defcon is a board with 5 LEDS that can be controlled with ethernet interface.

Here the source of the arduino with ethernet interface.


#include <WString.h>
#include <Ethernet.h>
#include <LedControl.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 2,5 };
byte server[] = { 192, 168, 2, 234 };
byte gateway[] = { 192, 168, 2, 1 };
byte subnet[] = { 255, 255, 255, 0 };
int score = 0,mes = 0;

boolean activee = false, readit = false, cmdfull =  false,done = false,conn = false;
Client client(server, 80);

//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 7;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 6;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 5;

LedControl lc=LedControl(clockPin,4,3,1);
int i;

String cmdstring;




void setup()
{
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(9600);
  
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  
    /* Haal de maxim uit power saving mode.   */
  lc.shutdown(0,false);
  /* Set de max72xx om gemiddelde helderheid */
  lc.setIntensity(0,8);
  /* Het opscjonen van de display */
  lc.clearDisplay(0);
  
  delay(1000);
  
  
  if (client.connect()) {
    client.println("GET /index.html HTTP/1.0");
    client.println();
    conn=true;
  }
 
}

void loop()
{
  if (activee)
  {
   if (client.connect() ) {
   Serial.println("connected 2");
   client.println("GET /index.html HTTP/1.0");
   client.println(); 
   conn=true;
  }
  
  }
  
  if (!conn)
  {
   Serial.println("connection failed");
   shiftallout(43690,31);
   tone(10,1200,1000);
  }
 
  
  
  if (client.available()) {
   
    char c = client.read();
    if (c == '$')
    {
    readit = true; 
    }
    if (readit)
    {
    cmdstring.append(c);
    
    if (c == '#')
    {
    cmdfull = true; 
    }
    
    Serial.println(cmdstring);
    
    if (cmdfull && !done)
    {
    shiftallout(statusupdate(cmdstring),0);
    
    if (mes == 3)
    mes = 0;
    displaymatrix(indexchar(cmdstring,',',11 + mes));
    
    mes++;
    
  
    
    done = true;
    }
    }
    activee = false;
  }
  
  if (!client.connected()) {
    
    Serial.println("disconnecting.");
    client.stop();
    readit = false;
    cmdfull= false;
    activee = true;
    done = false;
    conn=false;
    
    cmdstring = "";
    delay(2000);
  }
}


String indexchar(String stri,char seperator,unsigned int index)
 {
   int sepPos = -1;
   int offset = 0;
   int i;

   for(i = 0; i < index; i++)
   {
     if(sepPos < stri.length())
       offset = sepPos + 1;
     else
       return "";

     sepPos = stri.indexOf(seperator, offset);
     if(sepPos == -1)
       sepPos = stri.length();
   }
   if(!i)
     return "";
   return stri.substring(offset, sepPos);
 }

int statusupdate(String statuss)
{
int i=1,j=1;
String getal;
unsigned long complete = 0;
while (i != 9)
{ 

 i++;
 switch (atoi(indexchar(statuss,',',i))) {
   case 0:
    getal.append("00");
    score+=0;
    break; 
   case 1:
    getal.append("01");
    score+=2;
    break;
     case 2:
    getal.append("11");
    score+=4;
    break;
      case 3:
    getal.append("10");
    score+=5;
    break;
    
 }

}


i = 0;

for (i=0;i<getal.length();i++)
{
 
  complete += (int(getal.charAt(i)) - 48) * j;
   
j *=2;  
   
}

return complete;

}

void shiftallout(unsigned long data, int panels)
{
  byte a,b,c;
  
  a = data >> 0;
  b = data >> 8;



  
  digitalWrite(latchPin, LOW); 
   // shift the bytes out:
  if (panels == 0)
  {
  c = calcdanger(score,atoi(indexchar(cmdstring,',',10)));   
  shiftOut(dataPin, clockPin, c);
  }
  else
  {
  shiftOut(dataPin, clockPin, panels);  
  }
  
  shiftOut(dataPin, clockPin, b);
  shiftOut(dataPin, clockPin, a);
  // turn on the output so the LEDs can light up:
  digitalWrite(latchPin, HIGH);
  


}

void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first, 
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  //for each bit in the byte myDataOut�
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that %00000001 or "1" will go through such
  //that it will be pin Q0 that lights. 
  for (i=7; i>=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result 
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000 
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else {	
      pinState= 0;
    }
      //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin  
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}


byte calcdanger(int scoro, int servers)
{
int save = 0;
byte output = 1;
  save = int( 100 * float(float(scoro) / float(servers * 5)));
  Serial.print("save ");
  Serial.println(save);

  if (save > 0 && save <= 10)
  {
    output = 1;
    tone(10,1200,1000);
  }
  else if (save > 10 && save <= 20)
  {
    output = 3;
    tone(10,1200,1000);
  }
  else if (save > 20 && save <= 40)
  {
    output = 2;
    tone(10,1200,500);
  }
  else if(save > 40 && save <= 60)
   {
    output = 6;
    tone(10,600,500);
  }
  else if(save > 60 && save <= 80)
   {
    output = 4;
    tone(10,300,200);
  }
  else if(save > 80 && save <= 90)
  {
    output = 8;
    
  }
  else if(save > 90 && save <= 95)
  {
    output = 24;
    
  }
   else if(save > 95)
  {
    output = 16;
    
  }
score = 0;     
return output;  
}





void displaymatrix(String message)
{
 int i = 0;
 

 lc.setIntensity(0,0); 
  lc.clearDisplay(0);
 while(message.length() > i)
 {
  switch (message.charAt(i))
  {
  case'R':
  lc.setRow(0,i,0x05);
  break;
  case'U':
   lc.setRow(0,i,0x3e);
  break;
  case'N':
  lc.setRow(0,i,0x76);
  break;
   case' ':
  lc.setRow(0,i,0x00);
  break;
  case'O':
  lc.setRow(0,i,0x1D);
   break;
    case'-':
  lc.setRow(0,i,0x01);
   break;
     case'?':
  lc.setRow(0,i,0xE0);
   break;
   case'G':
  lc.setRow(0,i,0x5E);
   break;
    case'T':
  lc.setRow(0,i,0xF);
   break;
  case'C':
  lc.setRow(0,i,0x4E);
   break;
    case'J':
  lc.setRow(0,i,0x3E);
   break;
   case'I':
  lc.setRow(0,i,0x10);
   break;
   case'Q':
  lc.setRow(0,i,0x73);
   break;
   case'P':
  lc.setRow(0,i,0x67);
   break;
   
  default:
  lc.setChar(0,i,message.charAt(i),false);
  break;
  
  
 }
 i++;
  delay(100);
 }
 
 for (int i=0;i<14;i++)
 {
   delay(150);
     lc.setIntensity(0,i); 
 }
 delay(1000);
 for (int i=14;i>0;i--)
 {
   delay(150);
     lc.setIntensity(0,i); 
 }
 
}