КПІ-Arduino Клуб

До клубу запрошуються школярі 10, 11 класів і студенти спеціальності ОМ.

Контакна інформація в а.520 22 корпусу НТУУ КПІ.

тел. 204-82-27

 

Скетч для робота Arduino  з сервоприводом датчиком відстані:

// Arduino code for the H_BRIDGE
int LPWM = 5; // H-bridge leg 1 ->LPWM
int RPWM = 6; // H-bridge leg 2 ->RPWM
int LPWM1 = 11;
int RPWM1 = 3;
#include “Ultrasonic.h”
Ultrasonic ultrasonic(2, 4);
#include <Servo.h>
Servo myservo;
void setup()
{
pinMode(LPWM, OUTPUT);
pinMode(RPWM, OUTPUT);
pinMode(LPWM1, OUTPUT);
pinMode(RPWM1, OUTPUT);
Serial.begin(9600);
myservo.attach(12);
}
void loop()
{
myservo.write(10);
float dist_cm1 = ultrasonic.Ranging(CM);
delay(300);
if (dist_cm1 > 30 )
{
analogWrite(LPWM, 110);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, 10);
analogWrite(RPWM1, LOW);
delay(100);
}
else if (dist_cm1 < 3)
{
analogWrite(LPWM, 110);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, 110);
analogWrite(RPWM1, LOW);
delay(100);
}
else
{
analogWrite(LPWM, LOW);
analogWrite(RPWM, 254);
analogWrite(LPWM1, LOW);
analogWrite(RPWM1, 254);
delay(2000);
analogWrite(LPWM, 254);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, LOW);
analogWrite(RPWM1, 254);
delay(4000);
}

myservo.write(50);

float dist_cm2 = ultrasonic.Ranging(CM);

delay(200);
if (dist_cm2 > 30 )
{
analogWrite(LPWM, 110);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, 110);
analogWrite(RPWM1, LOW);
delay(100);
}
else if (dist_cm2 < 3)
{
analogWrite(LPWM, 110);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, 110);
analogWrite(RPWM1, LOW);
delay(100);
}
else
{
analogWrite(LPWM, LOW);
analogWrite(RPWM, 254);
analogWrite(LPWM1, LOW);
analogWrite(RPWM1, 254);
delay(2000);
analogWrite(LPWM, LOW);
analogWrite(RPWM, 254);
analogWrite(LPWM1,254);
analogWrite(RPWM1, LOW);
delay(4000);
}
myservo.write(90);
float dist_cm3 = ultrasonic.Ranging(CM);
delay(200);
if (dist_cm3 > 30 )
{
analogWrite(LPWM, 110);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, 110);
analogWrite(RPWM1, LOW);
delay(100);
}
else if (dist_cm3 < 3)
{
analogWrite(LPWM, 110);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, 110);
analogWrite(RPWM1, LOW);
delay(100);
}
else
{
analogWrite(LPWM, LOW);
analogWrite(RPWM, 254);
analogWrite(LPWM1, LOW);
analogWrite(RPWM1, 254);
delay(2000);
analogWrite(LPWM, 254);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, LOW);
analogWrite(RPWM1, 254);
delay(4000);
}
myservo.write(110);

float dist_cm4 = ultrasonic.Ranging(CM);
delay(200);
if (dist_cm4 > 30 )
{
analogWrite(LPWM, 110);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, 110);
analogWrite(RPWM1, LOW);
delay(100);
}
else if (dist_cm4 < 3)
{
analogWrite(LPWM, 110);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, 110);
analogWrite(RPWM1, LOW);
delay(100);
}
else
{
analogWrite(LPWM, LOW);
analogWrite(RPWM, 254);
analogWrite(LPWM1, LOW);
analogWrite(RPWM1, 254);
delay(2000);
analogWrite(LPWM, LOW);
analogWrite(RPWM, 254);
analogWrite(LPWM1,254);
analogWrite(RPWM1, LOW);
delay(4000);
}
myservo.write(150);

float dist_cm5 = ultrasonic.Ranging(CM);
delay(200);
if (dist_cm5 > 30 )
{
analogWrite(LPWM, 110);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, 110);
analogWrite(RPWM1, LOW);
delay(100);
}
else if (dist_cm5 < 3)
{
analogWrite(LPWM, 110);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, 110);
analogWrite(RPWM1, LOW);
delay(100);
}
else
{
analogWrite(LPWM, LOW);
analogWrite(RPWM, 254);
analogWrite(LPWM1, LOW);
analogWrite(RPWM1, 254);
delay(2000);
analogWrite(LPWM, LOW);
analogWrite(RPWM, 254);
analogWrite(LPWM1,254);
analogWrite(RPWM1, LOW);
delay(4000);
}
analogWrite(LPWM, 254);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, 254);
analogWrite(RPWM1, LOW);
delay(30);

myservo.write(130);
float dist_cm6 = ultrasonic.Ranging(CM);
delay(300);
if (dist_cm6 > 30 )
{
analogWrite(LPWM, 110);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, 10);
analogWrite(RPWM1, LOW);
delay(100);
}
else if (dist_cm6 < 3)
{
analogWrite(LPWM, 110);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, 110);
analogWrite(RPWM1, LOW);
delay(100);
}
else
{
analogWrite(LPWM, LOW);
analogWrite(RPWM, 254);
analogWrite(LPWM1, LOW);
analogWrite(RPWM1, 254);
delay(2000);
analogWrite(LPWM, 254);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, LOW);
analogWrite(RPWM1, 254);
delay(4000);
}

myservo.write(90);

float dist_cm7 = ultrasonic.Ranging(CM);

delay(200);
if (dist_cm7 > 30 )
{
analogWrite(LPWM, 110);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, 110);
analogWrite(RPWM1, LOW);
delay(100);
}
else if (dist_cm7 < 3)
{
analogWrite(LPWM, 110);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, 110);
analogWrite(RPWM1, LOW);
delay(100);
}
else
{
analogWrite(LPWM, LOW);
analogWrite(RPWM, 254);
analogWrite(LPWM1, LOW);
analogWrite(RPWM1, 254);
delay(2000);
analogWrite(LPWM, LOW);
analogWrite(RPWM, 254);
analogWrite(LPWM1,254);
analogWrite(RPWM1, LOW);
delay(4000);
}
myservo.write(50);
float dist_cm8 = ultrasonic.Ranging(CM);
delay(200);
if (dist_cm8 > 30 )
{
analogWrite(LPWM, 110);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, 110);
analogWrite(RPWM1, LOW);
delay(100);
}
else if (dist_cm8 < 3)
{
analogWrite(LPWM, 110);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, 110);
analogWrite(RPWM1, LOW);
delay(100);
}
else
{
analogWrite(LPWM, LOW);
analogWrite(RPWM, 254);
analogWrite(LPWM1, LOW);
analogWrite(RPWM1, 254);
delay(2000);
analogWrite(LPWM, 254);
analogWrite(RPWM, LOW);
analogWrite(LPWM1, LOW);
analogWrite(RPWM1, 254);
delay(4000);
}

}

sketch_9-26-12-1______