Samstag, 18. Juni 2011

Nippelboard

Beschreibung:
Durch betätigen eines der 6 Knöpfe ertönt jeweils ein anderer Sound.
Die Knöpfe werden mit einem 10k Wiederstand an das Waveshield auf dem Arduino angeschlossen.

Schaltplan:


Code:
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"


SdReader card;    // This object holds the information for the card
FatVolume vol;    // This holds the information for the partition on the card
FatReader root;   // This holds the information for the filesystem on the card
FatReader f;      // This holds the information for the file we're play

WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

#define DEBOUNCE 5  // button debouncer

// here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc
byte buttons[] = {14, 15, 16, 17, 18, 19};
// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)
// we will track if a button is just pressed, just released, or 'pressed' (the current state
volatile byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];

// this handy function will return the number of bytes currently free in RAM, great for debugging! 
int freeRam(void)
{
  extern int  __bss_end;
  extern int  *__brkval;
  int free_memory;
  if((int)__brkval == 0) {
    free_memory = ((int)&free_memory) - ((int)&__bss_end);
  }
  else {
    free_memory = ((int)&free_memory) - ((int)__brkval);
  }
  return free_memory;
}

void sdErrorCheck(void)
{
  if (!card.errorCode()) return;
  putstring("\n\rSD I/O error: ");
  Serial.print(card.errorCode(), HEX);
  putstring(", ");
  Serial.println(card.errorData(), HEX);
  while(1);
}

void setup() {
  byte i;
 
  // set up serial port
  Serial.begin(9600);
  putstring_nl("WaveHC with ");
  Serial.print(NUMBUTTONS, DEC);
  putstring_nl("buttons");
 
  putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(freeRam());      // if this is under 150 bytes it may spell trouble!
 
  // Set the output pins for the DAC control. This pins are defined in the library
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);

  // pin13 LED
  pinMode(13, OUTPUT);

  // Make input & enable pull-up resistors on switch pins
  for (i=0; i< NUMBUTTONS; i++) {
    pinMode(buttons[i], INPUT);
    digitalWrite(buttons[i], HIGH);
  }
 
  //  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
  if (!card.init()) {         //play with 8 MHz spi (default faster!)
    putstring_nl("Card init. failed!");  // Something went wrong, lets print out why
    sdErrorCheck();
    while(1);                            // then 'halt' - do nothing!
  }
 
  // enable optimize read - some cards may timeout. Disable if you're having problems
  card.partialBlockRead(true);

// Now we will look for a FAT partition!
  uint8_t part;
  for (part = 0; part < 5; part++) {     // we have up to 5 slots to look in
    if (vol.init(card, part))
      break;                             // we found one, lets bail
  }
  if (part == 5) {                       // if we ended up not finding one  :(
    putstring_nl("No valid FAT partition!");
    sdErrorCheck();      // Something went wrong, lets print out why
    while(1);                            // then 'halt' - do nothing!
  }
 
  // Lets tell the user about what we found
  putstring("Using partition ");
  Serial.print(part, DEC);
  putstring(", type is FAT");
  Serial.println(vol.fatType(),DEC);     // FAT16 or FAT32?
 
  // Try to open the root directory
  if (!root.openRoot(vol)) {
    putstring_nl("Can't open root dir!"); // Something went wrong,
    while(1);                             // then 'halt' - do nothing!
  }
 
  // Whew! We got past the tough parts.
  putstring_nl("Ready!");
 
  TCCR2A = 0;
  TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;

  //Timer2 Overflow Interrupt Enable
  TIMSK2 |= 1<<TOIE2;


}

SIGNAL(TIMER2_OVF_vect) {
  check_switches();
}

void check_switches()
{
  static byte previousstate[NUMBUTTONS];
  static byte currentstate[NUMBUTTONS];
  byte index;

  for (index = 0; index < NUMBUTTONS; index++) {
    currentstate[index] = digitalRead(buttons[index]);   // read the button
  
    /*   
    Serial.print(index, DEC);
    Serial.print(": cstate=");
    Serial.print(currentstate[index], DEC);
    Serial.print(", pstate=");
    Serial.print(previousstate[index], DEC);
    Serial.print(", press=");
    */
  
    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
          // just pressed
          justpressed[index] = 1;
      }
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
          // just released
          justreleased[index] = 1;
      }
      pressed[index] = !currentstate[index];  // remember, digital HIGH means NOT pressed
    }
    //Serial.println(pressed[index], DEC);
    previousstate[index] = currentstate[index];   // keep a running tally of the buttons
  }
}


void loop() {
  byte i;
 
  if(digitalRead(14)){
    playcomplete("amelie.wav");
  }
  else if(digitalRead(15)){
    playcomplete("hehe.wav");
  }
  else if(digitalRead(16)){
    playcomplete("kommts.wav");
  }
  else if(digitalRead(17)){
    playcomplete("coolste.wav");
  }
  else if(digitalRead(18)){
    playcomplete("arsch.wav");
  }
  else if(digitalRead(19)){
    playcomplete("nein.wav");
  }
}



// Plays a full file from beginning to end with no pause.
void playcomplete(char *name) {
  // call our helper to find and play this name
  playfile(name);
  while (wave.isplaying) {
  // do nothing while its playing
  }
  // now its done playing
}

void playfile(char *name) {
  // see if the wave object is currently doing something
  if (wave.isplaying) {// already playing something, so stop it!
    wave.stop(); // stop it
  }
  // look in the root directory and open the file
  if (!f.open(root, name)) {
    putstring("Couldn't open file "); Serial.print(name); return;
  }
  // OK read the file and turn it into a wave object
  if (!wave.create(f)) {
    putstring_nl("Not a valid WAV"); return;
  }
 
  // ok time to play! start playback
  wave.play();
}


Samstag, 30. April 2011

Temperatur wird getwittert

Beschreibung:
Die Temperatur wird mit dem NTC Thermistor gemessen und an Twitter gesendet.

Der NTC Thermistor wird mit einem 1k Widerstand an das Ethernet Shield auf dem Arduino angeschlossen.

Die Werte für B und R25 im Quellcode müssen mit den Werten des NTC Thermistors ersetzt werden (im Datenblatt zu finden).

Schaltplan:



Code:
/*
Vorlage für die Temperaturmessung:
Temperaturmessung mit einem NTC Thermistor
von Thomas Ecker
www.physicalcomputing.at
Mai 2010
*/

#if defined(ARDUINO) && ARDUINO > 18   // Arduino 0019 or later
#include 
#endif
#include 
#include 
#include 
#include 
#include 

// Ethernet Shield Settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("DEIN TWITTER TOKEN");

// Message to post
char temp[5];
char text[50];

int SensorPin = 0; // Für den Senoreingang wird Analog 0 gewählt
float sensorWert = 0; // Variable, die den Sensor Wert annimmt
float u1 =0; // Spannung u1 am Spannungsteiler
float u2 = 0; // Spannung u2 am Spannungsteiler
float i = 0; // Strom in A
float Rntc = 0; // Widerstand des Thermistors zum Zeitpunkt der Messung
float T = 0; // Variable für gemessene Temperatur
float B = 3950; // Wert aus Datenblatt des Thermistors
float Tn = 298.16; // Nenntemperatur in K
float R25 = 4700; // Nennwiderstand in Ohm bei Nenntemperatur (aus Datenblatt)

void setup()
{
  delay(1000);
  Serial.begin(9600);
  EthernetDHCP.begin(mac);
}

void loop()
{
  EthernetDHCP.maintain();
  
  sensorWert = analogRead(SensorPin); // Wert am Sensor wird gelesen
  u2 = (sensorWert * 5)/1024; // Spannung am Sensor wird berechnet
  u1 = 5-u2; // Spannung am Vorwiderstand wird berechnet
  i = u1/1000; // Strom wird berechnet
  Rntc = u2/i; // Widerstand des Thermistors zum Zeitpunkt der Messung

  T = (B*Tn)/(B+(log(Rntc/R25)*Tn)); // Berechnung der Temperatur
  T = T-273.15; // Umrechnung von K in °C

  Serial.print ("Temperatur = ");
  Serial.print (T);
  Serial.println (" C");
  
  ftoa(temp, (double)T, 2);
  
  sprintf(text,"%s Grad Celsius <- Aktuelle Temperatur in meinem Zimmer",temp);
  
  Serial.println (text);

  Serial.println("connecting ...");
  if (twitter.post(text)) {
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    // int status = twitter.wait();
    int status = twitter.wait(&Serial);
    if (status == 200) {
      Serial.println("OK.");
    } else {
      Serial.print("failed : code ");
      //Serial.println(status);
    }
  } else {
    Serial.println("connection failed.");
  }
  
  delay (1800000); // Temperatur wird alle halbe Std gemessen
}

char *ftoa(char *a, double f, int precision)
{
  long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
  
  char *ret = a;
  long heiltal = (long)f;
  itoa(heiltal, a, 10);
  while (*a != '\0') a++;
  *a++ = '.';
  long desimal = abs((long)((f - heiltal) * p[precision]));
  itoa(desimal, a, 10);
  return ret;
}

Freitag, 29. April 2011

Lichtsensor

Beschreibung:
Wenn Licht auf den Lichtsensor scheint, bleibt die LED aus. Trifft kein Licht auf den Sensor, leuchtet die LED.

Der Lichtsensor wird mit einem 10k Widerstand angeschlossen und die LED wird mit einem 150 Ohm Vorwiderstand angeschlossen.

Schaltplan:


Code:
int val;

void setup(){
  pinMode(13,OUTPUT);
}

void loop(){
  val = analogRead(0);
  if(val < 400)
  {
    digitalWrite(13, HIGH);
  }
  else
  {
    digitalWrite(13, LOW);
  }
}

Sonntag, 24. April 2011

TicTacToe Game

Beschreibung:
Ein simples TicTacToe Game für 2 Spieler, welches auf dem Arduino mit dem VideoGameShield von wayneandlayne.com ausgeführt wird. Das Spiel wird mit 2 Nunchucks gesteuert.

Schaltplan:
Nicht erforderlich da nur das VideoGameShield aufgesetzt wird

Code:
/*
  Two Player TicTacToe
  by Pascal König
  v1.0, 24/04/2011
*/
#include 
#include 
#include 
#include 
#include 
#include 

TVout TV;
Nunchuck player1;
Nunchuck player2;

byte hres, vres;

byte cursor_x, cursor_y;

int field[9] = {0,0,0,0,0,0,0,0,0}; //Felder: 0 = frei, 1 = Player 1, 2 = Player 2 ----- von links nach rechts und dann von oben nach unten

byte player = 0; // 1 = Player 1, 2 = Player 2 - wird am Anfang random entschieden

#define STATE_TEST    0
#define STATE_START   1
#define STATE_PLAY    2
byte state = STATE_TEST;

void setup()
{
  TV.begin(_NTSC);
  TV.select_font(font4x6);
  TV.delay_frame(60);
  state = STATE_START;
  
  randomSeed(analogRead(0));
  player = random(1, 3);
}

void init_display()
{
  // draw field
  for (byte y = 0; y < vres; y += 6)
    TV.draw_line(hres / 3, y, hres / 3, y + 2, 1);
  for (byte y = 0; y < vres; y += 6)
    TV.draw_line((hres / 3) + (hres / 3), y, (hres / 3) + (hres / 3), y + 2, 1);
  for (byte x = 0; x < hres; x += 6)
    TV.draw_line(x, vres / 3, x + 2, vres / 3, 1);
  for (byte x = 0; x < hres; x += 6)
    TV.draw_line(x, (vres / 3) + (vres / 3), x + 2, (vres / 3) + (vres / 3), 1);
}

void draw_cursor()
{
  TV.set_pixel(cursor_x, cursor_y, 2);
}

int set_field(int player_nummer) //gibt zurück ob gesetzt wurde oder nicht ( 1 oder 0)
{
  switch (player_nummer)
  {
    case 1:
      if(player1.button_z())
      {
        if(cursor_x > 0 && cursor_x < hres / 3 && cursor_y > 0 && cursor_y < vres / 3) // links oben
        {
          if(field[0] == 0)
          {
            TV.draw_line(0 + 5, 0 + 5, hres / 3 - 5, vres / 3 - 5, 2);
            TV.draw_line(0 + 5, vres / 3 - 5, hres / 3 - 5, 0 + 5, 2); 
            field[0] = 1;
            return 1;
          }          
        }
        if(cursor_x > 0 && cursor_x < hres / 3 && cursor_y > vres / 3 && cursor_y < (vres / 3) + (vres / 3)) // links mitte
        {
          if(field[3] == 0)
          {
            TV.draw_line(0 + 5, vres / 3 + 5, hres / 3 - 5, vres / 3 + vres / 3 - 5, 2);
            TV.draw_line(0 + 5, vres / 3 + vres / 3 - 5, hres / 3 - 5, vres / 3 + 5, 2);
            field[3] = 1;
            return 1;
          }
        }
        if(cursor_x > 0 && cursor_x < hres / 3 && cursor_y > (vres / 3) + (vres / 3) && cursor_y < vres) // links unten
        {
          if(field[6] == 0)
          {
            TV.draw_line(0 + 5, vres / 3 + vres / 3 + 5, hres / 3 - 5, vres - 5, 2);
            TV.draw_line(0 + 5, vres - 5, hres / 3 - 5, vres / 3 + vres / 3 + 5, 2);
            field[6] = 1;
            return 1;
          }
        }
        if(cursor_x > hres / 3 && cursor_x < (hres / 3) + (hres / 3) && cursor_y > 0 && cursor_y < vres / 3) // mitte oben
        {
          if(field[1] == 0)
          {
            TV.draw_line(hres / 3 + 5, 0 + 5, hres / 3 + hres / 3 - 5, vres / 3 - 5, 2);
            TV.draw_line(hres / 3 + 5, vres / 3 - 5, hres / 3 + hres / 3 - 5, 0 + 5, 2);
            field[1] = 1;
            return 1;
          }
        }
        if(cursor_x > hres / 3 && cursor_x < (hres / 3) + (hres / 3) && cursor_y > vres / 3 && cursor_y < (vres / 3) + (vres / 3)) // mitte mitte
        {
          if(field[4] == 0)
          {
            TV.draw_line(hres / 3 + 5, vres / 3 + 5, hres / 3 + hres / 3 - 5, vres / 3 + vres / 3 - 5, 2);
            TV.draw_line(hres / 3 + 5, vres / 3 + vres / 3 - 5, hres / 3 + hres / 3 - 5, vres / 3 + 5, 2);
            field[4] = 1;
            return 1;
          }
        }
        if(cursor_x > hres / 3 && cursor_x < (hres / 3) + (hres / 3) && cursor_y > (vres / 3) + (vres / 3) && cursor_y < vres) // mitte unten
        {
          if(field[7] == 0)
          {
            TV.draw_line(hres / 3 + 5, vres / 3 + vres / 3 + 5, hres / 3 + hres / 3 - 5, vres - 5, 2);
            TV.draw_line(hres / 3 + 5, vres - 5, hres / 3 + hres / 3 - 5, vres / 3 + vres / 3 + 5, 2);
            field[7] = 1;
            return 1;
          }
        }
        if(cursor_x > (hres / 3) + (hres / 3) && cursor_x < hres && cursor_y > 0 && cursor_y < vres / 3) // rechts oben
        {
          if(field[2] == 0)
          {
            TV.draw_line(hres / 3 + hres / 3 + 5, 0 + 5, hres - 5, vres / 3 - 5, 2);
            TV.draw_line(hres / 3 + hres / 3 + 5, vres / 3 - 5, hres - 5, 0 + 5, 2);
            field[2] = 1;
            return 1;
          }
        }
        if(cursor_x > (hres / 3) + (hres / 3) && cursor_x < hres && cursor_y > vres / 3 && cursor_y < (vres / 3) + (vres / 3)) // rechts mitte
        {
          if(field[5] == 0)
          {
            TV.draw_line(hres / 3 + hres / 3 + 5, vres / 3 + 5, hres - 5, vres / 3 + vres / 3 - 5, 2);
            TV.draw_line(hres / 3 + hres / 3 + 5, vres / 3 + vres / 3 - 5, hres - 5, vres / 3 + 5, 2);
            field[5] = 1;
            return 1;
          }
        }
        if(cursor_x > (hres / 3) + (hres / 3) && cursor_x < hres && cursor_y > (vres / 3) + (vres / 3) && cursor_y < vres) // rechts unten
        {
          if(field[8] == 0)
          {
            TV.draw_line(hres / 3 + hres / 3 + 5, vres / 3 + vres / 3 + 5, hres - 5, vres - 5, 2);
            TV.draw_line(hres / 3 + hres / 3 + 5, vres - 5, hres - 5, vres / 3 + vres / 3 + 5, 2);
            field[8] = 1;
            return 1;
          }
        }
      }
      break;
    case 2:
    if(player2.button_z())
      {
        if(cursor_x > 0 && cursor_x < hres / 3 && cursor_y > 0 && cursor_y < vres / 3) // links oben
        {
          if(field[0] == 0)
          {
            TV.draw_circle((hres / 3) / 2, (vres / 3) / 2, (hres / 3) / 2 - 10,1,-1);
            field[0] = 2;
            return 1;
          }          
        }
        if(cursor_x > 0 && cursor_x < hres / 3 && cursor_y > vres / 3 && cursor_y < (vres / 3) + (vres / 3)) // links mitte
        {
          if(field[3] == 0)
          {
            TV.draw_circle((hres / 3) / 2, vres / 2, hres / 6 - 10, 1, -1);
            field[3] = 2;
            return 1;
          }
        }
        if(cursor_x > 0 && cursor_x < hres / 3 && cursor_y > (vres / 3) + (vres / 3) && cursor_y < vres) // links unten
        {
          if(field[6] == 0)
          {
            TV.draw_circle(hres / 6, (vres / 3 + vres / 3 + vres) / 2, hres / 6 - 10, 1, -1);
            field[6] = 2;
            return 1;
          }
        }
        if(cursor_x > hres / 3 && cursor_x < (hres / 3) + (hres / 3) && cursor_y > 0 && cursor_y < vres / 3) // mitte oben
        {
          if(field[1] == 0)
          {
            TV.draw_circle(hres / 2, vres / 6, hres / 6 - 10, 1, -1);
            field[1] = 2;
            return 1;
          }
        }
        if(cursor_x > hres / 3 && cursor_x < (hres / 3) + (hres / 3) && cursor_y > vres / 3 && cursor_y < (vres / 3) + (vres / 3)) // mitte mitte
        {
          if(field[4] == 0)
          {
            TV.draw_circle(hres / 2, vres / 2, hres / 6 - 10, 1, -1);
            field[4] = 2;
            return 1;
          }
        }
        if(cursor_x > hres / 3 && cursor_x < (hres / 3) + (hres / 3) && cursor_y > (vres / 3) + (vres / 3) && cursor_y < vres) // mitte unten
        {
          if(field[7] == 0)
          {
            TV.draw_circle(hres / 2, (vres / 3 + vres / 3 + vres) / 2, hres / 6 - 10, 1, -1);
            field[7] = 2;
            return 1;
          }
        }
        if(cursor_x > (hres / 3) + (hres / 3) && cursor_x < hres && cursor_y > 0 && cursor_y < vres / 3) // rechts oben
        {
          if(field[2] == 0)
          {
            TV.draw_circle((hres / 3 + hres / 3 + hres) / 2, vres / 6, hres / 6 - 10, 1, -1);
            field[2] = 2;
            return 1;
          }
        }
        if(cursor_x > (hres / 3) + (hres / 3) && cursor_x < hres && cursor_y > vres / 3 && cursor_y < (vres / 3) + (vres / 3)) // rechts mitte
        {
          if(field[5] == 0)
          {
            TV.draw_circle((hres / 3 + hres / 3 + hres) / 2, vres / 2, hres / 6 - 10, 1, -1);
            field[5] = 2;
            return 1;
          }
        }
        if(cursor_x > (hres / 3) + (hres / 3) && cursor_x < hres && cursor_y > (vres / 3) + (vres / 3) && cursor_y < vres) // rechts unten
        {
          if(field[8] == 0)
          {
            TV.draw_circle((hres / 3 + hres / 3 + hres) / 2, (vres / 3 + vres / 3 + vres) / 2, hres / 6 - 10, 1, -1);
            field[8] = 2;
            return 1;
          }
        }
      }
      break;
  }
  return 0;
}

void update_cursor(int player_nummer)
{
  switch (player_nummer)
  {
    case 1:
      if ( player1.joy_up())
      {
        cursor_y -= 1;
        if ( cursor_y < 1)
          cursor_y = 0;
      }
      if ( player1.joy_down())
      {
        cursor_y+=1;
        if ( cursor_y > (vres - 1))
          cursor_y = vres;
      }
      if (player1.joy_left())
      {
        cursor_x-=1;
        if(cursor_x < 1)
          cursor_x = 0;
      }
      if(player1.joy_right())
      {
        cursor_x+=1;
        if(cursor_x > (hres -1))
          cursor_x = hres;
      }
      break;
    case 2:
      if ( player2.joy_up())
      {
        cursor_y -= 1;
        if ( cursor_y < 1)
          cursor_y = 0;
      }
      if ( player2.joy_down())
      {
        cursor_y+=1;
        if ( cursor_y > (vres - 1))
          cursor_y = vres;
      }
      if (player2.joy_left())
      {
        cursor_x-=1;
        if(cursor_x < 1)
          cursor_x = 0;
      }
      if(player2.joy_right())
      {
        cursor_x+=1;
        if(cursor_x > (hres -1))
          cursor_x = hres;
      }
      break;
  }
}

int sieg_ermitteln() //gibt den Sieger zurück: 1 = player 1 - 2 = player 2 - 3 = kein sieger - 0 = noch kein sieger
{
  if((field[0] == 1 && field[1] == 1 && field[2] == 1)||(field[3]==1&&field[4]==1&&field[5]==1)||(field[6]==1&&field[7]==1&&field[8]==1)||(field[0] == 1 && field[3] == 1 && field[6] == 1)||(field[1]==1&&field[4]==1&&field[7]==1)||(field[2]==1&&field[5]==1&&field[8]==1)||(field[0]==1&&field[4]==1&&field[8]==1)||(field[2]==1&&field[4]==1&&field[6]==1))
    return 1;
  else if((field[0] == 2 && field[1] == 2 && field[2] == 2)||(field[3]==2&&field[4]==2&&field[5]==2)||(field[6]==2&&field[7]==2&&field[8]==2)||(field[0] == 2 && field[3] == 2 && field[6] == 2)||(field[1]==2&&field[4]==2&&field[7]==2)||(field[2]==2&&field[5]==2&&field[8]==2)||(field[0]==2&&field[4]==2&&field[8]==2)||(field[2]==2&&field[4]==2&&field[6]==2))
    return 2;
  else if(field[0]!=0&&field[1]!=0&&field[2]!=0&&field[3]!=0&&field[4]!=0&&field[5]!=0&&field[6]!=0&&field[7]!=0&&field[8]!=0)
    return 3;
  else
    return 0;
}

void zuruecksetzten()
{
  for(int i = 0; i < 10; i++)
  {
    field[i] = 0;
  }
}

void loop()
{
  switch (state)
  {
    case STATE_TEST:
      for (byte t = 0; t < 10; t++)
      {        
        TV.delay_frame(20);
      }
      break;
    case STATE_START:
      title_screen_init_nunchucks(&TV, "TicTacToe", &player1, &player2, true);
      
      hres = TV.hres() - 6; // this is based on what's visible on my tv
      vres = TV.vres();

      TV.clear_screen();
      init_display();
      draw_cursor(); // pre-draw the cursor so we can erase it
      
      state = STATE_PLAY;
      break;
    case STATE_PLAY:
      draw_cursor();
      if (player == 1)
      {
        TV.printPGM((hres / 2) - 16, 0, PSTR("Player 1"));
        player1.update();
        update_cursor(1);
        if(set_field(1) == 1)        
          player = 2;
      }
      else if (player == 2)
      {
        TV.printPGM((hres / 2) - 16, 0, PSTR("Player 2"));
        player2.update();
        update_cursor(2);
        if(set_field(2) == 1)        
          player = 1;
      }
      if(sieg_ermitteln() == 1)
      {
        TV.printPGM((hres / 2) - 16, vres / 2, PSTR("Player 1 win!"));
        zuruecksetzten();
        TV.delay_frame(240);
        state = STATE_START;
      }
      else if(sieg_ermitteln() == 2)
      {
        TV.printPGM((hres / 2) - 16, vres / 2, PSTR("Player 2 win!"));
        zuruecksetzten();
        TV.delay_frame(240);
        state = STATE_START;
      }
      else if(sieg_ermitteln() == 3)
      {
        TV.printPGM((hres / 2) - 16, vres / 2, PSTR("Nobody win!"));
        zuruecksetzten();
        TV.delay_frame(240);
        state = STATE_START;
      }
      draw_cursor();
      
      TV.delay_frame(1);
      break;
  }
}

Mittwoch, 20. April 2011

LED Roulette

Beschreibung:
Hier habe ich ein simples Lauflicht erstellt welches langsamer wird und zufällig stehen bleibt.

Schaltplan:



Code:
int led[7] = {13,12,11,10,9,8,7};

int del = 50; //delay

int aktuell = 0;

int zufall;

void setup(){
  for(int i = 0;i <= 6;i++){
    pinMode(led[i], OUTPUT);
  }
  randomSeed(analogRead(0));
  zufall = random(190, 210);
  
  Serial.begin(9600);
  Serial.println(zufall);
}

void loop(){  
  for(int i = 0;i <= 6;i++){
    if (del <= zufall)
    {
      laufen(i);
    }
    else
    {
      digitalWrite(led[aktuell], HIGH);
    }
  }
}

void laufen(int i){
    digitalWrite(led[i], HIGH);
    delay(del);
    digitalWrite(led[i], LOW);
    del+=2;
    aktuell = i;
}