Piezoelectric sensors & Real Game - mcworkshop

MC        WORKSHOP
Vai ai contenuti

Menu principale:

Piezoelectric sensors & Real Game

Projects
The piezoelectric transducer is activated by the flexor tendons. In this example, a servomotor is rotated.
Il trasduttore piezoelettrico viene attivato dai tendini del flessore. In questo esempio si fa ruotare un servomotore.

Test / Game #1 / Real Game


Sketch di test del sensore

const int sensorPin = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
//leggo dal pin A0
int val = analogRead(sensorPin);
//invio il dato al serial monitor
Serial.println(val);
delay(200);
}


clic to enlarge
Sketch

//Piezoelectric sensor
//by M.C. maggio 2019
//This example code is in the public domain.
#include <Servo.h>
Servo servo;
const int analogInPin1 = A0;
const int analogInPin2 = A1;
int sensorValue1 = 0;
int sensorValue2 = 0;
void setup()
{
 Serial.begin(9600);
 servo.attach (3);
 pinMode(7, OUTPUT);
 pinMode(8, OUTPUT);
 pinMode(9, OUTPUT);
}
void loop()
{
 sensorValue1 = analogRead(analogInPin1);
 sensorValue2 = analogRead(analogInPin2);
 Serial.println (" ");
 Serial.print("sensor1  = ");
 Serial.println(sensorValue1);
 Serial.print("sensor2  = ");
 Serial.println(sensorValue2);
 delay(50);
 //Piezo 1
 if (sensorValue1 >= 1) //sensibility
 {
   digitalWrite(7, HIGH);
   digitalWrite(9, HIGH);
   servo.write (-80);
   delay (400);
 }
 if (sensorValue1 < 1);
 {
   digitalWrite(7, LOW);
   digitalWrite(10, LOW);
 }
 //Piezo 2
 if (sensorValue2 >= 1)
 {
   digitalWrite(8, HIGH);
   digitalWrite(9, HIGH);
   servo.write (+80);
   delay (400);
 }
 if (sensorValue2 < 1);
 {
   digitalWrite(8, LOW);
   digitalWrite(9, LOW);
 }
}
GAME #1
SDA PIN A4
SCL PIN A5
PULSE SERVO PIN 3
PULSE SERVO2 PIN 5
PIEZO PIN A0
PIEZO PIN A1
PIEZO PIN A2



//Piezoelectric sensor GAME
//by M.C. maggio 2019 - revisione luglio 2019
//This example code is in the public domain.

#include <avr/io.h>
#include <avr/wdt.h>

#define Reset_AVR() wdt_enable(WDTO_30MS); while(1) {}

#include <Servo.h>
Servo servooriz;
Servo servovert;
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

const int analogInPin0 = A0;
const int analogInPin1 = A1;
const int analogInPin2 = A2;

int sensorValue0 = 0;
int sensorValue1 = 0;
int sensorValue2 = 0;

void setup()
{
 Serial.begin(9600);
 servooriz.attach (3);
 servovert.attach (5);
 pinMode(8, OUTPUT);
 lcd.clear();
 lcd.init();
 lcd.backlight();
 lcd.print("  SYSTEM READY");
}

void loop()
{
 sensorValue0 = analogRead(analogInPin0);
 sensorValue1 = analogRead(analogInPin1);
 sensorValue2 = analogRead(analogInPin2);

 Serial.println (" ");
 Serial.print("alzo in0   = ");
 Serial.println(sensorValue0);
 Serial.print("fuoco in1 = ");
 Serial.println(sensorValue1);
 Serial.print("spostamento in2 = ");
 Serial.println(sensorValue2);
 delay(50);


 if (sensorValue2 >= 40)//spostamento
 {
   lcd.clear();
   lcd.print("   SPOSTAMENTO   ");
    lcd.setCursor(0, 1);
    lcd.print("    Impulso ");
   lcd.print (sensorValue2);
   servooriz.write (0);
   delay (400);
   servooriz.write (180);
   delay (400);
   servooriz.write (90);
   delay (400);
   lcd.clear();
   lcd.print("   IN ATTESA   ");
   lcd.setCursor(0, 1);
   lcd.print(" Red Air Defens");
 }
 else
 {
   servooriz.write (90);
 }

 if (sensorValue0 >= 40) //alzo
 {
   lcd.clear();
   lcd.print("      ALZO    ");
   lcd.setCursor(0, 1);
   lcd.print("    Impulso ");
   lcd.print (sensorValue0);
   servovert.write (0);
   delay (400);
   servovert.write (180);
   delay (400);
   servovert.write (90);
   delay (400);
   lcd.clear();
   lcd.print("   IN ATTESA   ");
   lcd.setCursor(0, 1);
   lcd.print(" Red Air Defens");
 }
 else
 {
   servovert.write (90);
 }

 if (sensorValue1 >= 4)//fuoco
 {
   lcd.clear();
   lcd.print("     FUOCO !    ");
   lcd.setCursor(0, 1);
    lcd.print("    Impulso ");
   lcd.print (sensorValue1);
   digitalWrite(8, HIGH);
   delay (2000);
   digitalWrite(8, LOW);
   lcd.clear();
   lcd.print("   IN ATTESA   ");
   lcd.setCursor(0, 1);
   lcd.print(" Red Air Defens");
 }
 else
 {
   servooriz.write (90);
 }

 if (sensorValue2 >= 50 & sensorValue0 >= 50)
 {
   delay (50);
   lcd.clear();
   lcd.print("   KEEP CALM !   ");
   lcd.setCursor(0, 1);
   lcd.print("  System reset  ");
   delay (4000);
   Reset_AVR();
 }
}

-----------------
REAL GAME

info: robotichiamo@gmail.com

Controllo laser , sparo e atterraggio
laser control, firing and landing

//"Video" Game - sparo
//By M.C.W. 08/2019
//This example code is in the public domain.

#include <LiquidCrystal_I2C.h>

unsigned long time;

int analogInPinA0 = A0;
int sensorValue = 0;
int shot = 1;
int qshot = 10;
int inPin = 3;
int val = 0;

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
 Serial.begin(9600);
 lcd.backlight();
 lcd.init();
 lcd.backlight();
 Serial.println (" ");
 lcd.setCursor(0, 0);
 lcd.print("      READY     ");
 lcd.setCursor(0, 1);
 lcd.print("  WEAPON SYSTEM ");
 pinMode(5, OUTPUT);
 pinMode(inPin, INPUT);

}
void loop()
{
     time = millis();
 sensorValue = analogRead(analogInPinA0);
 if (sensorValue >= 20) //sensibilità
 {
  
   lcd.clear();
   delay (100);
   digitalWrite(5, HIGH);
   lcd.setCursor(0, 0);
   lcd.print(" SHOT!  ");
   lcd.print(" FI: ");//forza dell'impulso
   lcd.print(sensorValue);
   qshot = (qshot - shot);
   lcd.setCursor(0, 1);
   lcd.print(" SHOT # ");
   lcd.print(qshot);
   lcd.setCursor(0, 0);
   lcd.print("      READY     ");
   delay (1000);
   digitalWrite(5, LOW);
 }
 delay(100);
 {
   if (sensorValue >= 250)
   {
     lcd.clear();
     lcd.print("   KEEP CALM !   ");
     lcd.setCursor(0, 1);
     lcd.print("      wait      ");
     delay (4000);
     lcd.clear();
     lcd.print("      READY     ");
     lcd.setCursor(0, 1);
     lcd.print("  WEAPON SYSTEM ");
   }
   if (qshot == 00)
   {
     lcd.setCursor(0, 0);
     lcd.print("  WEAPON SYSTEM ");
     lcd.setCursor(0, 1);
     lcd.print("    OFF LINE    ");
   }
   if (time >= 240000)
   {
     lcd.setCursor(0, 0);
     lcd.print("    TO LAND !     ");
     lcd.setCursor(0, 1);
     lcd.print("lower the switch");
     delay (3000);
     lcd.clear();
   }
   val = digitalRead(inPin);
   if (val == 1)
   {
     lcd.clear();
     lcd.setCursor(0, 0);
     lcd.print("    LANDING    ");
     lcd.setCursor(0, 1);
     lcd.print("       OK      ");
     delay (3000);
   }
 }
}

Controllo servomotore orizzontale e tempo trascorso
Horizontal servomotor control and elapsed time

//"Video" Game - oriz - tempo
//by M.C. Agosto 2019
//This example code is in the public domain.

#include <LiquidCrystal_I2C.h>
#include <Servo.h>

unsigned long time;

Servo servo;
int analogInPinA0 = A0;
int sensorValue = 0;
int x = 1;
int i = 0;
int stato = 20; //da 2 a 20 max

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
 Serial.begin(9600);
 servo.attach (3);
 lcd.backlight();
 lcd.init();
 lcd.backlight();
 Serial.println (" ");
 lcd.setCursor(0, 0);
 lcd.print("      READY     ");
}
void loop()
{
 time = millis();
 time = (time / 1000);

 lcd.setCursor(0, 1);
 lcd.print("Time: ");
 lcd.print(time);
 sensorValue = analogRead(analogInPinA0);
 if (sensorValue >= 20) //sensibilità
 {
   delay (100);
   int x = 1;
   for (int i = 0; i > -1; i = i + (stato * x)) {
     servo.write(i);
     if (i == 180) {
       x = -1;
       delay (100);
       lcd.setCursor(0, 0);
       lcd.print("VSO: ");//velocità spostamento orizzontale
       lcd.print(stato);
       lcd.print(" FI: ");//forza dell'impulso
       lcd.print(sensorValue);
     }
     delay(100);
   }
   if (sensorValue >= 250)
   {
     lcd.clear();
     lcd.print("   KEEP CALM !   ");
     lcd.setCursor(0, 1);
     lcd.print("      wait      ");
     delay (4000);
     lcd.clear();
   }
 }
}
Controllo servomotore verticale e carburante
Vertical servomotor and fuel control

//"Video" Game - fuel - vert
//By M.C.W. 08/2019
//This example code is in the public domain.

#include <LiquidCrystal_I2C.h>
#include <Servo.h>
unsigned long time;
Servo servo;
int analogInPinA0 = A0;
int sensorValue = 0;
int x = 1;
int i = 0;
int stato = 20; //da 2 a 20 max

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
 Serial.begin(9600);
 servo.attach (3);
 lcd.backlight();
 lcd.init();
 lcd.backlight();
 Serial.println (" ");
 lcd.setCursor(0, 0);
 lcd.print("      READY     ");
}
void loop()
{
 time = millis();
//tempo limite 4 minuti
 if (time >= 0 && time <= 120000) {
   lcd.setCursor(0, 1);
   lcd.print("Fuel: ");
   lcd.print ("100 Full");
 }
 if (time >= 120000 && time <= 150000) {
   lcd.setCursor(0, 1);
   lcd.print("Fuel: ");
   lcd.print ("75  3/4    ");
 }
 if (time >= 150000 && time <= 180000) {
   lcd.setCursor(0, 1);
   lcd.print("Fuel: ");
   lcd.print ("50 Half ");
 }
 if (time >= 180000 && time <= 210000) {
   lcd.setCursor(0, 1);
   lcd.print("Fuel: ");
   lcd.print ("25 CAUTION");
 }
 if (time >= 210000 && time <= 240000) {
   lcd.setCursor(0, 1);
   lcd.print ("    TO LAND !     ");
 }
 if (time >= 280000) {
   lcd.setCursor(0, 1);
   lcd.print ("     CRASH     ");
 }
 sensorValue = analogRead(analogInPinA0);
 if (sensorValue >= 80) //sensibilità
 {
   delay (100);
   int x = 1;
   for (int i = 0; i > -1; i = i + (stato * x)) {
     servo.write(i);
     if (i == 180) {
       x = -1;
       lcd.setCursor(0, 0);
       lcd.print("VSV: ");//velocità spostamento verticale
       lcd.print(stato);
       lcd.print(" FI: ");//forza dell'impulso
       lcd.print(sensorValue);
     }
     delay(100);
   }
   if (sensorValue >= 400)
   {
     lcd.clear();
     lcd.print("   KEEP CALM !   ");
     lcd.setCursor(0, 1);
     lcd.print("      wait      ");
     delay (4000);
     lcd.clear();
   }
 }
}

Bersaglio mobile
Moving target

//By M.C.W. 08/2019
#include <Servo.h>
Servo myservo;
int pos = 90;
long randNumber;
void setup() {
 Serial.begin(9600);
 myservo.attach(3);
 randomSeed(analogRead(0));
}
void loop() {
 randNumber = random(10, 170);
 myservo.write (randNumber);
 delay (1000);
}
 
Copyright 2015. All rights reserved.
Torna ai contenuti | Torna al menu