/*fonctionnement:
bouton rouge bouton de tire
bouton noir changement de mode bouton sur manchette bras gauche
resté appuyé au moins un seconde
car c'est le relachement ui est prit en compte
canon aimanté sur bras
led rouge viseur
led blanche tire
Par défaut au démarrage les servo ce mettent à 50% de débatement soit à 90 ° pour servo moteur de 180°
le canon ce met en position se degagement
bras bas canon droit devant
appuyer bt mode noir -> rentre le canon
appuyer bt mode noir -> sort le canon
si appuyer bt mode noir -> rentre le canon
si appuyez bt tire
si reste appuyé tire -> ca tire a chaque nouvelle position de canon !
au relachement bt de tire -> mode scan autoaléatoire
si bt mode noir -> arrêt des tire et rangement du canon
actuellement bug dans les changement de mode
et reste encore les sons à ajouter
*/
// variable tempo
int ancienmode = 0;
int debuttempo = 0;
int tempo = 200;
int tempofini = 0;
// bouton
int bt10 = 0; // etat bt 1 vient d'être relaché si 0 non si 1 oui
int bt11 = 0; // etat bt 1 vient d'être enfoncé
int bt20 = 0; // etat bt 2 vient d'être relaché
int bt21 = 0; // etat bt 2 vient d'être enfoncé
const int buttonmodePin = 8; // bouton mode
const int buttontirePin = 7; // bouton tire
//position degagement toutes les valeures servo moteur a 90°
const int pos1initin = 180; // variable init in axe z1 rotation de la base 90 avant 180 rangé
const int pos1degag = 90; // positon de dégagement z1
const int pos1initout = 90; // variable init out axe z1 position stanby
const int pos2initin = 90;// variable init in axe x1 levé première partie du bras
const int pos2degag = 90; // positon de dégagement x1
const int pos2initout = 45;// variable init out axe x1 position stanby
const int pos3initin = 90;// variable init in axe x2 levé deuxième partie du bras
const int pos3degag = 90; // positon de dégagement x2
const int pos3initout = pos2initout + 90;// variable init out axe x2 position stanby
const int pos4initin = 90;// variable init in axe x3 visé haut bas
const int pos4degag = 90; // positon de dégagement x3
const int pos4initout = 90 - pos2initout;// variable init out axe x3 position stanby
const int pos5initin = 90;// variable init in axe z2 visé gauche droite
const int pos5degag = 90; // positon de dégagement z2
const int pos5initout = 90;// variable init out axe z2 position stanby
int oldValmode = LOW; // default/idle value for pin 8 is low.
int oldValtire = LOW; // default/idle value for pin 8 is low.
//servom
#include <Servo.h>
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
Servo myservo3; // create servo object to control a servo
Servo myservo4; // create servo object to control a servo
Servo myservo5; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos1 = pos1degag; // variable to store the servo position
int pos2 = pos2degag; // variable to store the servo position
int pos3 = pos3degag; // variable to store the servo position
int pos4 = pos4degag; // variable to store the servo position
int pos5 = pos5degag; // variable to store the servo position
//mode bras
int modebras = 0;
int ancienmodebras = 0;
int modebras2 = -1;
int pos4rand = 0;
int pos5rand = 0;
int hasard = 0;
int modebrasmax = 10; // mode max(10) avant remise au mode zero
int pinledviseur = 4;
int pinledshot = 6;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//Serial.println("Press the button.");
// Initialize the pin for reading the button.
pinMode(buttonmodePin, INPUT);
pinMode(buttontirePin, INPUT);
// servo
myservo1.attach(3);
myservo2.attach(5);
myservo3.attach(9);
myservo4.attach(10);
myservo5.attach(11);
// initialize digital pin LED_BUILTIN as an output.
pinMode(pinledviseur, OUTPUT);
pinMode(pinledshot, OUTPUT);
}
void loop() {
//init bt
bt10 = 0;
bt11 = 0;
bt20 = 0;
bt21 = 0;
// lecture valeur bouton 1 et 2.
int newValmode = digitalRead(buttonmodePin);//delay(100);
int newValtire = digitalRead(buttontirePin);//delay(100);
// bouton 1
if(newValmode != oldValmode){ // Si changement d'état du bouton 1
if(newValmode == HIGH){ //action a l'enfoncement du bt1(mode)
bt11 = 1;
}
else{ //action au relachement du bt1(rouge)
bt10 = 1;
}
// Remember the value for the next time.
oldValmode = newValmode;
}
// bouton 2
if(newValtire != oldValtire){ // Si changement d'état du bouton 2
if(newValtire == HIGH){ //action a l'enfoncement du bt2 (tire)
bt21 = 1;
}
else{ // action au relachement du bt2 (noir)
bt20 = 1;
}
// Remember the value for the next time.
oldValtire = newValtire;
}
// si bug modebras mode -> 4 rentrer bras
//if(modebras >= modebrasmax){modebras = 4;}
if (bt10 == 1) { // bouton 1 au relachement
// changement de modebras
// au demarrage modebras 0
switch(modebras){
// si bt10 + 1 bras in rentré -> modebras 2 sortir bras
case( 1 ):
modebras = 2;
break;
//si bt10 + 3 bras sorti -> modebras 4 rentré le bras
case( 3 ):
modebras = 4;
break;
//si 6 mode auto scan + bt10 -> modebras 4 rentré le bras
case( 6 ):
modebras = 4;
break;
}
}
if (bt10 == 1) { // bouton 1 au relachement
// action selon le modebras
switch(modebras)
{
//0 demarrage rentrer bras -> degagement -> 1 bras in rentré
case( 0 ):
degage ();actionservo();delay(tempo);initin();actionservo();modebras++;
break;
//2 bras in sortir bras -> degagement -> 3 bras out sorti
case( 2 ):
degage ();actionservo();delay(tempo);initout();actionservo();delay (tempo);modebras++;
break;
//bras ? rentré le bras -> bras out -> degagement -> bras in rentré
case ( 4 ):
ledvoff();ledtoff();delay(100);initout();actionservo();delay (tempo); degage ();actionservo();delay(tempo);initin();actionservo();delay(tempo);modebras = 2;
break;
}
}
if (bt20 == 1){ // action bouton tire au relachement
// si 3 bras out sorti + bt20 -> 6 auto scan
switch(modebras)
{
case( 3 ):
modebras = 6;
ledvon;
break;
}
}
//boucle d'action quand modebras
switch(modebras)
{
case( 6 ): // action si 6 mode bras auto scan -> scan auto
ledvon();
hasardv ();actionservo();delay(tempo);
}
// tant que bt de tire enfoncé
if(newValtire == HIGH)
{
switch(modebras)
{
// si mode bras auto scan + bt tire reste appuyé -> tire !
case ( 6 ):
ledton();
ledtoff();
// manque le son
}
}
/* detection de fin de tempo pour les actions continues
if (millis() >= debuttempo + tempo )
{
tempofini = 1;
}
*/
}
void actionservo ()
{
myservo1.write(pos1); // tell servo to go to position in variable 'pos'
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
myservo3.write(pos3); // tell servo to go to position in variable 'pos'
myservo4.write(pos4); // tell servo to go to position in variable 'pos'
myservo5.write(pos5); // tell servo to go to position in variable 'pos'
}
void initin()
{
// coordonné de dégagement
pos1 = pos1initin;
pos2 = pos2initin;
pos3 = pos3initin;
pos4 = pos4initin;
pos5 = pos5initin;
}
void initout()
{
// coordonné de dégagement
pos1 = pos1initout;
pos2 = pos2initout;
pos3 = pos3initout;
pos4 = pos4initout;
pos5 = pos5initout;
}
void degage()
{
// coordonné de dégagement
pos1 = pos1degag;
pos2 = pos2degag;
pos3 = pos3degag;
pos4 = pos4degag;
pos5 = pos5degag;
}
void ledvon()
{
// fonction allumer led 1
digitalWrite(pinledviseur, HIGH);delay(100);
}
void ledvoff()
{
// fonction eteindre led 1
digitalWrite(pinledviseur, LOW);delay(100);
}
void ledton()
{
// fonction allumer led 2
digitalWrite(pinledshot, HIGH);delay(100);
}
void ledtoff()
{
// fonction eteindre led 2
digitalWrite(pinledshot, LOW);delay(100);
}
void hasardv ()
{
// fonction valeur hasard de visé
pos4rand = random(pos4initout - 45,pos4initout + 45);
delay(10);
pos5rand = random(pos5initout - 90,pos5initout +20);
delay(10);
pos4 = pos4rand;
pos5 = pos5rand;
}
void debutempo() // lancement tempo
{
debuttempo = millis();
tempofini = 0;
}