The cart is empty

Valutazione attuale: 0 / 5

Stella inattivaStella inattivaStella inattivaStella inattivaStella inattiva

Under construction
see you soon

Articolo in fase di montaggio....

Immagini...


preparazione...







distribuzione alimentazione



ponte motori...



piani...

{ddtoc_break title=Code pde}

#include <Servo.h> //include la libreria "servo"
 
 //TRAZIONE
#define PwmPinMotorA 5
#define PwmPinMotorB 6
#define DirectionPinMotorA 7
#define DirectionPinMotorB 8
//avanti 8=1 7=0 indietro 8=0 7=1
//STERZO
#define STR3 11
#define Ain1 12
#define Ain2 13
//luci
#define sensore_luce 0
#define rele 10
//contatto anteriore
#define contatto_ant 1

//sensore ultrasuoni
const int numOfReadings = 10;            // number of readings to take/ items in the array
int readings[numOfReadings];            // stores the distance readings in an array
int arrayIndex = 0;                     // arrayIndex of the current item in the array
int total = 0;                          // stores the cumlative total
int averageDistance = 0;                      // stores the average value
Servo myservo;                                  // create servo object to control a servo
                                                // a maximum of eight servo objects can be created
//int pos = 0;                                    // variable to store the servo position
 
// setup pins and variables for SRF05 sonar device
 
int echoPin = 2;                         // SRF05 echo pin (digital 2)
int initPin = 3;                         // SRF05 trigger pin (digital 3)
unsigned long pulseTime = 0;              // stores the pulse in Micro Seconds
unsigned long distance = 0;              // variable for storing the distance (cm)
int comando = 0;                                //variabile per non ripetere il comando
                        
int val_luce = 0;                                 //variabile valore luce
int val_contatto = 2;                                 //variabile valore contatto

//setup
 
void setup()
{
// I MOTORI DEVONO ESSERE OUTPUT
  //trazione
  pinMode(PwmPinMotorA, OUTPUT);
  pinMode(PwmPinMotorB, OUTPUT);
  pinMode(DirectionPinMotorA, OUTPUT);
  pinMode(DirectionPinMotorB, OUTPUT);
  //STERZO
  pinMode(Ain1, OUTPUT);
  pinMode(Ain2, OUTPUT);
  pinMode(STR3, OUTPUT); 
  //contatti anteriori e posteriori
  pinMode(contatto_ant, INPUT);
 
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object
 
 
  pinMode(initPin, OUTPUT);            // set init pin 3 as output
  pinMode(echoPin, INPUT);            // set echo pin 2 as input
 
  pinMode(rele,OUTPUT);
 
 
  // create array loop to iterate over every item in the array
 
  for (int thisReading = 0; thisReading < numOfReadings; thisReading++) {     
readings[thisReading] = 0;     
 }        
    Serial.begin(9600);
 }
 
// esecuzione

void loop() {  
  //contatto anteriore
  val_contatto=analogRead(contatto_ant);
  switch (val_contatto) {
    case 0:
      analogWrite(PwmPinMotorA, 150);
      digitalWrite(DirectionPinMotorA, HIGH);
      analogWrite(PwmPinMotorB, 100);
      digitalWrite(DirectionPinMotorB, LOW);
      analogWrite(STR3, 255);//stabilire la volocità in assemblaggio LA VELOCITà DETERMINA L'ANGOLO
      digitalWrite(Ain2, LOW);
      digitalWrite(Ain1, HIGH);
      myservo.write(0);
      delay(1000);
      digitalWrite(Ain2, LOW);
      digitalWrite(Ain1, LOW);
      comando = 11;
      break;
    case 128:
      analogWrite(PwmPinMotorA, 100);
      digitalWrite(DirectionPinMotorA, HIGH);
      analogWrite(PwmPinMotorB, 150);
      digitalWrite(DirectionPinMotorB, LOW);
      analogWrite(STR3, 255);//stabilire la volocità in assemblaggio
      digitalWrite(Ain2, HIGH);
      digitalWrite(Ain1, LOW);
      myservo.write(180);
      delay(1000);
      digitalWrite(Ain2, LOW);
      digitalWrite(Ain1, LOW);
      comando = 10;
      break;
   
  }

   val_luce=analogRead(sensore_luce);
   {
  //accende o spegne il relè
      if (val_luce >= 120)

        {
      digitalWrite(rele, HIGH);
        }
        else
       {
      analogWrite(rele, LOW);
     }
  
}
  
 
{                                 
myservo.write(90);              // posiziona il servo al centro

}

//lettura distanza
 
digitalWrite(initPin, HIGH);              // send 10 microsecond pulse  
delayMicroseconds(10);              // wait 10 microseconds before turning off  
digitalWrite(initPin, LOW);            // stop sending the pulse  
pulseTime = pulseIn(echoPin, HIGH);        // Look for a return pulse, it should be high as the pulse goes low-high-low  
distance = pulseTime/58;              // Distance = pulse time / 58 to convert to cm. 
 total= total - readings[arrayIndex];         // subtract the last distance  
readings[arrayIndex] = distance;         // add distance reading to array  
total= total + readings[arrayIndex];         // add the reading to the total  
arrayIndex = arrayIndex + 1;                 // go to the next item in the array                                
// At the end of the array (10 items) then start again  
if (arrayIndex >= numOfReadings)  {
    arrayIndex = 0;
  }
 
  averageDistance = total / numOfReadings;      // calculate the average distance
 
  // sceglie l'azione in base alla distanza dell'ostacolo
 if (averageDistance >=75)

{
  // dritto o dritto accellera
      if (comando==0)

        {
      analogWrite(STR3, 0); //stabilire la volocità in assemblaggio
      digitalWrite(Ain2, LOW);
      digitalWrite(Ain1, LOW);
      analogWrite(PwmPinMotorA, 60);
      digitalWrite(DirectionPinMotorA, LOW);
      analogWrite(PwmPinMotorB, 60);
      digitalWrite(DirectionPinMotorB, HIGH);
      comando=1;
      myservo.write(90);
        }
        else
       {
      analogWrite(STR3, 0); //accellera
      digitalWrite(Ain2, LOW);
      digitalWrite(Ain1, LOW);
      analogWrite(PwmPinMotorA, 65);
      digitalWrite(DirectionPinMotorA, LOW);
      analogWrite(PwmPinMotorB, 65);
      digitalWrite(DirectionPinMotorB, HIGH);
      comando=0;
      myservo.write(90);
     }
  
}

else if (averageDistance >= 50)
{
      // gira a dx 25° o gira sx 25°
      if (comando==2)
        {
      analogWrite(PwmPinMotorA, 60);
      digitalWrite(DirectionPinMotorA, LOW);
      analogWrite(PwmPinMotorB, 60);
      digitalWrite(DirectionPinMotorB, HIGH);
      analogWrite(STR3, 190);//stabilire la volocità in assemblaggio LA VELOCITà DETERMINA L'ANGOLO
      digitalWrite(Ain2, LOW);
      digitalWrite(Ain1, HIGH);
      myservo.write(140);
      delay(1000);
      digitalWrite(Ain2, LOW);
      digitalWrite(Ain1, LOW);
      comando = 3;
        }
        else
       {
      analogWrite(PwmPinMotorA, 60);
      digitalWrite(DirectionPinMotorA, LOW);
      analogWrite(PwmPinMotorB, 60);
      digitalWrite(DirectionPinMotorB, HIGH);
      analogWrite(STR3, 190);//stabilire la volocità in assemblaggio
      digitalWrite(Ain2, HIGH);
      digitalWrite(Ain1, LOW);
      myservo.write(50);
      delay(1000);
      digitalWrite(Ain2, LOW);
      digitalWrite(Ain1, LOW);
      comando = 2;
      myservo.write(135);
     }
}
else if (averageDistance >= 35)
{
  // gira a dx max o gira a sx max
      if (comando==4)
        {
      analogWrite(STR3, 255);//stabilire la volocità in assemblaggio LA VELOCITà DETERMINA L'ANGOLO
      digitalWrite(Ain2, LOW);
      digitalWrite(Ain1, HIGH);
      myservo.write(170);
      delay(1500);
      digitalWrite(Ain2, LOW);
      digitalWrite(Ain1, LOW);
      comando = 5;
        }
        else
       {
      analogWrite(STR3, 255);//stabilire la volocità in assemblaggio
      digitalWrite(Ain2, HIGH);
      digitalWrite(Ain1, LOW);
      myservo.write(10);
      delay(1500);
      digitalWrite(Ain2, LOW);
      digitalWrite(Ain1, LOW);
      comando = 4;
     }
}
else if (averageDistance >= 25)
{
  // indietro sinistra 25° o indietro destra 25°
      if (comando==6)
        {
      analogWrite(PwmPinMotorA, 60);
      digitalWrite(DirectionPinMotorA, HIGH);
      analogWrite(PwmPinMotorB, 60);
      digitalWrite(DirectionPinMotorB, LOW);
      analogWrite(STR3, 150);//stabilire la volocità in assemblaggio LA VELOCITà DETERMINA L'ANGOLO
      digitalWrite(Ain2, LOW);
      digitalWrite(Ain1, HIGH);
      myservo.write(0);
      delay(1500);
      digitalWrite(Ain2, LOW);
      digitalWrite(Ain1, LOW);
      comando = 7;
      myservo.write(90);
        }
        else
       {
      analogWrite(PwmPinMotorA, 60);
      digitalWrite(DirectionPinMotorA, HIGH);
      analogWrite(PwmPinMotorB, 60);
      digitalWrite(DirectionPinMotorB, LOW);
      analogWrite(STR3, 150);//stabilire la volocità in assemblaggio LA VELOCITà DETERMINA L'ANGOLO
      digitalWrite(Ain2, HIGH);
      digitalWrite(Ain1, LOW);
      myservo.write(180);
      delay(1500);
      digitalWrite(Ain2, LOW);
      digitalWrite(Ain1, LOW);
      comando = 6;
      myservo.write(90);
     }
}
else  //(averageDistance >= 20)
{
   // ruota 180° destra o 180° sinistra
  if (comando==10)
        {
      analogWrite(PwmPinMotorA, 80);
      digitalWrite(DirectionPinMotorA, HIGH);
      analogWrite(PwmPinMotorB, 65);
      digitalWrite(DirectionPinMotorB, LOW);
      analogWrite(STR3, 255);//stabilire la volocità in assemblaggio LA VELOCITà DETERMINA L'ANGOLO
      digitalWrite(Ain2, LOW);
      digitalWrite(Ain1, HIGH);
      myservo.write(0);
      delay(1000);
      digitalWrite(Ain2, LOW);
      digitalWrite(Ain1, LOW);
      comando = 11;
        }
        else
       {
      analogWrite(PwmPinMotorA, 65);
      digitalWrite(DirectionPinMotorA, HIGH);
      analogWrite(PwmPinMotorB, 80);
      digitalWrite(DirectionPinMotorB, LOW);
      analogWrite(STR3, 255);//stabilire la volocità in assemblaggio
      digitalWrite(Ain2, HIGH);
      digitalWrite(Ain1, LOW);
      myservo.write(180);
      delay(1000);
      digitalWrite(Ain2, LOW);
      digitalWrite(Ain1, LOW);
      comando = 10;
     }

}


Serial.println(val_contatto);  //debug
  delay(15);                       // wait 15 milli seconds before looping again
 
}

MINIMAMENTE

Sassifraghe ombrose le nostre anime, avvolte in un involucro alimentare

 

 

Latest News

18 Giugno 2016
18 Giugno 2016