sexta-feira, 19 de abril de 2019

Alvo eletrônico para airsoft.

Meu objetivo neste post é apresentar o avo eletrônico que fiz para treino e lazer usando Airsoft.


Esquema de Conexão da Montagem:





link para o projeto em Tinkercad




Sensor de pressão piezoelétrico:

Piezoelétrico é um tipo de material que responde com uma voltagem quando exercemos uma pressão sobre ele.


Código:


#include <LiquidCrystal.h>
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);

int time = 10;

const int buzzer = 13;

unsigned long prevMillis;
int count;
short int i;
int score = 0; 
short int generateNextNum = 0;
volatile int stage = 1;
short int ranNum = 0;


void setup(){
Serial.begin(9600);
lcd.begin(16, 2);
    pinMode(3, OUTPUT); 
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(buzzer, OUTPUT); // Set buzzer - pin 13 as an output
   // pinMode(2, INPUT_PULLUP);
    pinMode(2, INPUT);
    attachInterrupt(digitalPinToInterrupt(2), reset_routine, RISING);  
}

void reset_routine()             
{
if(stage == 1)
{
stage = 2;
} else 
if(stage == 3)
{
generateNextNum = 1;
digitalWrite(ranNum,LOW);
} else 
if(stage == 4)
{
stage = 1;
}
}


void welcome_routine()                       
{
digitalWrite(3,HIGH);
    lcd.clear();
lcd.setCursor(0, 0);
lcd.print("    T.A.M.C.  ");
lcd.setCursor(0, 1);
lcd.print("  Airsoft Tean ");
  tone(buzzer, 700);
delay(500);
noTone(buzzer);
  delay(500);
  tone(buzzer, 300);
delay(500);
noTone(buzzer);
  delay(300);
  tone(buzzer, 300);
delay(500);
noTone(buzzer);
  delay(300);
  tone(buzzer, 700);
delay(500);
noTone(buzzer);
  delay(300);
  tone(buzzer, 700);
delay(500);
noTone(buzzer);
    delay (3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome. Press");
lcd.setCursor(0, 1);
lcd.print("button to start.");
while(stage == 1){}
}

void start_routine()                       
{
lcd.clear();
    for(i=0; i<=2; i++)
    {
for(int x=3; x<=6; x++)
{
digitalWrite(x,HIGH);
}
lcd.setCursor(0, 0);
if(i==0){
lcd.print("Get ready");
          tone(buzzer, 500); // Send 1KHz sound signal...
delay(500);
noTone(buzzer);
          delay(500);          
}else if(i==1){
lcd.print("Set");
          tone(buzzer, 700); // Send 1KHz sound signal...
delay(500);
noTone(buzzer);
          delay(500);
}else if(i==2){
lcd.print("GO");
          tone(buzzer, 1000); // Send 1KHz sound signal...
delay(700);
noTone(buzzer);
          delay(500);
}
    
        lcd.clear();
for(int x=3; x<=6; x++)
{
digitalWrite(x,LOW);
}
delay(500);
    }
lcd.clear();
stage = 3;
score = 0; 
count = time;
ranNum = 0;
}

void finish_routine()                       
{
stage = 4;
digitalWrite(ranNum,LOW);
lcd.clear();
tone(buzzer, 500); // Send 1KHz sound signal...
delay(100);
  noTone(buzzer);
  delay(100);
    tone(buzzer, 300); // Send 1KHz sound signal...
delay(100);
  noTone(buzzer);
  delay(100);
    tone(buzzer, 100); // Send 1KHz sound signal...
delay(100);
  noTone(buzzer);
lcd.setCursor(0, 0);
lcd.print("Game Over.");
    lcd.setCursor(0, 1);
    lcd.print("Score:");
    lcd.setCursor(10, 1);
    lcd.print(score);
delay(3000);
digitalWrite(3,HIGH);
while(stage == 4){}
}


void loop(){
  
  welcome_routine();
  start_routine();
  prevMillis = millis();  
  
  lcd.setCursor(0, 0);
  lcd.print("Time:");
  lcd.setCursor(10, 0);
  lcd.print(count);
  
  lcd.setCursor(0, 1);
  lcd.print("Score:");
  lcd.setCursor(10, 1);
  lcd.print(score);
  char countString[] = "";
  
  while(count > 0){
if(ranNum != 0) 
{
score = score + 1;
           for (int i=0; i<1000; i++)
  {
    digitalWrite(buzzer, HIGH);
    delayMicroseconds(i);
    digitalWrite(buzzer, LOW);
    delayMicroseconds(i);
  }
          
}
    

    int ranNumOld = ranNum;
while(ranNumOld == ranNum)
  {
ranNum = random(3,7);
}  
        digitalWrite(ranNum,HIGH);
generateNextNum = 0;

while(generateNextNum == 0 && count > 0)
{
unsigned long curMillis = millis();
if(curMillis - prevMillis >= 300)
{
count -= 1;
              //countString[] = count+" ";
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time:");
lcd.setCursor(10, 0);
lcd.print(String(count)+" ");
lcd.setCursor(0, 1);
lcd.print("Score:");
lcd.setCursor(10, 1);
lcd.print(score);
prevMillis = prevMillis +300;
}
  }
  finish_routine();
}

Espero que tenha gostado desta postagem, dúvidas ou sugestões deixe nos comentários que serão respondidos. Ajude-me compartilhando este texto com seus amigos em suas redes sociais.