// Ardublockly generated sketch
#include <Ultrasonic.h>


boolean meio;

void forward(int MotA, int MotB)
{
  analogWrite(10,MotA);//Motor A vel
  analogWrite(11,MotB);//Motor B vel
  digitalWrite(13,LOW);// Motor B (direito) move no sentido do relogio
  digitalWrite(12,LOW);//Motor A (esquerda) move no sentido do relogio
}

Ultrasonic ultrasonic_15(15,16);

void stop()
{
  analogWrite(10,0);//Motor A vel
  analogWrite(11,0);//Motor B vel
}


void backward(int MotA, int MotB)
{
  analogWrite(10,MotA);//Motor A vel
  analogWrite(11,MotB);//Motor B vel
  digitalWrite(13,HIGH);// Motor B (direito) move no sentido contrario do relogio
  digitalWrite(12,HIGH);//Motor A (esquerda) move no sentido contrario do relogio
}


void right(int MotA, int MotB)
{
  analogWrite(10,MotA);//Motor A vel
  analogWrite(11,MotB);//Motor B vel
  digitalWrite(13,HIGH);//Motor B (direito) move no sentido do relogio
  digitalWrite(12,LOW);//Motor A (esquerda) move no sentido contrario do relogio
}



void setup() {
  pinMode(17, INPUT);
  pinMode(12,OUTPUT);//direcaoPinoA
  pinMode(13,OUTPUT);//direcaoPinoB
  pinMode(10,OUTPUT);//velPinoA
  pinMode(11,OUTPUT);//velPinoB

  pinMode(4, OUTPUT);

}

void loop() {
  meio = (boolean)(digitalRead(17));
  forward(250, 250);
  if (ultrasonic_15.convert(ultrasonic_15.timing(),Ultrasonic::CM) < 20) {
    stop();
    delay(1000);
    backward(200, 200);
    delay(1000);
    right(200, 127);
    delay(500);
  }
  if (!meio) {
    stop();
    //delay(2000);
    tone(4,220);
    delay(200);
    tone(4,210);
    delay(200);
    tone(4,220);
    delay(200);
    tone(4,210);
    delay(200);
    noTone(4);
    forward(250,250);
    delay(1000);
  }

}
