Operácie

Si1143.h

Zo stránky SensorWiki

Verzia z 08:22, 19. máj 2016, ktorú vytvoril StudentDVPS (diskusia | príspevky) (Vytvorená stránka „<source lang="c"> →‎Si1143 Example: #define sleep 100 #include <Wire.h> #include "Si1143.h"; int bias1,bias2,bias3; unsigned int PS1,PS2,PS3; int blinktime,count...“)
(rozdiel) ← Staršia verzia | Aktuálna úprava (rozdiel) | Novšia verzia → (rozdiel)
/* Si1143 Example */


#define sleep  100
#include <Wire.h>
#include "Si1143.h";

int bias1,bias2,bias3;
unsigned int PS1,PS2,PS3;
int blinktime,counter,counter1,counter2,Ledposition;
unsigned int Light_Reading;
byte LowB,HighB;
bool selected;
unsigned long time;

void setup()
{
  Serial.begin(9600);
  delay(25);
  Wire.begin(); // join i2c bus (address optional for master)
  delay(25);
 
  write_reg(HW_KEY, 0x17); // Setting up LED Power to full
  write_reg(PS_LED21,0xFF);
  write_reg(PS_LED3, 0x0F);
  param_set(CHLIST,0b00010111);
 
  char parameter = read_reg(PARAM_RD,1);
  delay(1000);
 
  bias();
 
  counter = 0;
  counter1 = 0;
  counter2 = 0;
  selected = 0;
  StartGame();
}

void loop()
{
  write_reg(COMMAND,0b00000101); // Get a reading
  delay(5);
 
  LowB = read_reg(PS1_DATA0,1); // Read the data for the first LED
  HighB = read_reg(PS1_DATA1,1);
  PS1 = ((HighB * 255) + LowB) - bias1;
 
  LowB = read_reg(PS2_DATA0,1);  // Read the data for the second LED
  HighB = read_reg(PS2_DATA1,1);
  PS2 = (HighB * 255) + LowB - bias2;
 
  LowB = read_reg(PS3_DATA0,1);  // Read the data for the third LED
  HighB = read_reg(PS3_DATA1,1);
  PS3 = (HighB * 255) + LowB - bias3;
 
  Light_Reading = read_light();  
    if(selected != 1) {
    if (counter > 5 || counter1 > 5 || counter2 > 5){
      selected = 0;   
      
  }
   }
   
   do_magic();
   
 
                                                
  if (PS1 < 1000 || PS2 < 1000 || PS3 < 1000){
      if (PS1 < PS2 && PS1 < PS3){
          if (selected == 0){
          counter++;
          Ledposition = 1;
          counter1 = 0;
          counter2 = 0;
          
          }
      }else if(PS2 > PS1 && PS2 > PS3){
          if (selected == 0){
          counter1++;
          Ledposition = 2;
          counter = 0;
          counter2 = 0;
        }
      }else if(PS3 > PS1 && PS3 > PS2){
          if (selected == 0){
          counter2++;
          Ledposition = 3;
          counter = 0;
          counter1 = 0;
        }
        
      }
    }else{
    counter = 0;
    counter1 = 0;
    counter2 = 0;
    selected = 0;
  } 
}

unsigned int read_light(){  // Read light sensor
  write_reg(COMMAND,0b00000110);
  delay(sleep);
  byte LowB = read_reg(ALS_VIS_DATA0,1);
  byte HighB = read_reg(ALS_VIS_DATA1,1);
  return (HighB * 255) + LowB;
}

void param_set(byte address, byte val)  // Set Parameter
{
  write_reg(PARAM_WR, val);
  write_reg(COMMAND, 0xA0|address);
}

char read_reg(unsigned char address, int num_data) // Read a Register
{
  unsigned char data;

  Wire.beginTransmission(IR_ADDRESS);
  Wire.write(address);
  Wire.endTransmission();

  Wire.requestFrom(IR_ADDRESS, num_data);
 
  while(Wire.available() < num_data);
 
  return Wire.read();
}

void write_reg(byte address, byte val) {  // Write a resigter
  Wire.beginTransmission(IR_ADDRESS);
  Wire.write(address);     
  Wire.write(val);       
  Wire.endTransmission();     
}

void bias(void){  // Bias during start up
 
  for (int i=0; i<20; i++){
  write_reg(COMMAND,0b00000101);
  delay(50);
 
  byte LowB = read_reg(PS1_DATA0,1);
  byte HighB = read_reg(PS1_DATA1,1);
 
  bias1 += ((HighB * 255) + LowB) / 20;
 
  LowB = read_reg(PS2_DATA0,1);
  HighB = read_reg(PS2_DATA1,1);
 
  bias2 += ((HighB * 255) + LowB) / 20;
 
  LowB = read_reg(PS3_DATA0,1);
  HighB = read_reg(PS3_DATA1,1);
 
  bias3 += ((HighB * 255) + LowB) / 20;
} 
}

void touch_select(){    // just a blink routine for when something is selected
    switch (Ledposition) {
         
      case 1:
        Serial.println("Priloz prst k LED1");
        time = millis();
      break;
      
      case 2:
        Serial.println("Priloz prst k LED2");
      break;
      
      case 3:
        Serial.println("Priloz prst k LED3");    
      break;
      
    } 
}
unsigned int PS1_old=0,PS2_old=0, PS3_old=0;
int diff1=0,diff2=0,diff3=0, sum1=0,sum2=0,sum3=0;
int rand1 ; 
int aktual=0,timing=0;
boolean succes=true;

void do_magic(){
  
  
 
  diff1 = PS1-PS1_old;
  diff2 = PS2-PS2_old;
  diff3 = PS3-PS3_old;
  diff1=abs(diff1);
  diff2=abs(diff2);
  diff3=abs(diff3);
  
  PS1_old=PS1;
  PS2_old=PS2;
  PS3_old=PS3;
 
  
  
  if(diff1>diff2 && diff1 >diff3){
     if(sum2>0 || sum3 >0){
      sum2=0; sum3=0;
    }
    sum1=sum1+1;   
    
  }else if(diff2> diff1 && diff2 >diff3){
     if(sum1>0 || sum2 >0){
      sum1=0; sum3=0;
    }
    sum2=sum2+1;
    
  }else if(diff3>diff1 && diff3>diff2){    
    if(sum1>0 || sum2 >0){
      sum1=0; sum2=0;
    }
    sum3=sum3+1;
  }
if(sum1>0 || sum2>0 || sum3 > 0){
  if(sum1>4 && rand1==1){
    Serial.println("vybrata je LED1");
    sum1=0;
    succes=true;
    Serial.println("Vyhra");
  }
   if(sum2>4 && rand1==2){
    Serial.println("vybrata je LED2");
    sum2=0;
    succes=true;
    Serial.println("Vyhra");
  }
   if(sum3>4 && rand1==3){
      Serial.println("vybrata je LED3");
      sum3=0;
      succes=true;
      Serial.println("Vyhra");
  }
   if(succes){
  rand1=random(1,4);
  
  if(rand1>=1 && rand1<2){
    rand1=1;
  }else if(rand1>=2 && rand1<3){
    rand1 =2;
  }else{
    rand1=3;
  }
  
  time = millis();
  aktual=time-timing;
  timing=time;
  switch(rand1){
  case 1:
    Serial.println("Priloz prst k LED cislo 1");
   break;
   case 2:
    Serial.println("Priloz prst k LED cislo 2");
  break;  
   case 3:
    Serial.println("Priloz prst k LED cislo 3");
  break;
  
  }
   Serial.print("Time: ");
   Serial.println(aktual);
   succes=false;
  }
}
delay(10);
}


void StartGame(){
Serial.println("                            HRA: VYBER LED                ");
Serial.println("CIEL: Priloz prst co najrychlejsie k spravnej LEDke senzora podla  zobrazenia na monitore");
Serial.println("INSTRUKCIE: Rozlozenie LEDiek na snimaci:");
Serial.println("        LED2");
Serial.println("         - ");
Serial.println("         - ");
Serial.println("         - ");
Serial.println("         - ");
Serial.println("         - ");
Serial.println("         - ");
Serial.println("         - ");
Serial.println("         - ");
Serial.println("         - ");
Serial.println("        LED1 -------------------- LED3 ");
Serial.println(" ");
Serial.println("Hra zacne prilozenim prstu k  LED3 na senzore ");

}