So this gets it moving
So this gets it moving around
#include <NewPing.h>
int ENA=5;//connected to Arduino’s port 5(output pwm)
int IN1=2;//connected to Arduino’s port 2
int IN2=3;//connected to Arduino’s port 3
int ENB=6;//connected to Arduino’s port 6(output pwm)
int IN3=4;//connected to Arduino’s port 4
int IN4=7;//connected to Arduino’s port 7
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-1000cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup(){
Serial.begin(9600);
pinMode(ENA,OUTPUT);//output
pinMode(ENB,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);}
void loop(){
delay(35);
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println(“cm”);
if ( uS / US_ROUNDTRIP_CM > 30 || uS / US_ROUNDTRIP_CM == 0) {
forward();
}
else if (uS / US_ROUNDTRIP_CM < 30){
halt;
delay(250);
reverse();
delay(800);
rightturn();
delay(550);
leftturn();
delay(650);
}
}
void rightturn(){
analogWrite(ENA,125);
analogWrite(ENB,125);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
}
void leftturn(){
analogWrite (ENA,125);
analogWrite (ENB,125);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
}
void forward(){
analogWrite (ENA,150);
analogWrite (ENA,150);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
}
void reverse(){
analogWrite (ENA,110);
analogWrite (ENB,110);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
}
void halt(){
analogWrite (ENA,0);
analogWrite (ENB,0);
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
}
Im starting to try and write a code to check left , right, forward
/
Arduino Servo Test sketch
/
#include <Servo.h>
Servo servoMain; // Define our Servo
void setup()
{
servoMain.attach(9); // servo on digital pin 10
}
void loop()
{
servoMain.write(9); // Turn Servo Left to 0 degrees
delay(200); // Wait 1 second
servoMain.write(75); // Turn Servo back to center position (90 degrees)
delay(200); // Wait 1 second
// Wait 1 second
servoMain.write(170); // Turn Servo Right to 180 degrees
delay(200); // Wait 1 second
servoMain.write(75); // Turn Servo back to center position (90 degrees)
delay(200); // Wait 1 second
}
Im using this to check my direction seeking …just turn on a led for which direction it choose’s and then get that to work with the Motor’s, head servo ,ping …
#include <NewPing.h>
#define LED_PIN 4
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters).
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
boolean trig = false;
void setup(){
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
pinMode(LED_PIN,OUTPUT);
analogWrite(LED_PIN,0);
delay(1000);
}
void Engaged(){
digitalWrite(LED_PIN,HIGH);
Serial.println(“This Hurt!!!”);}
void loop(){
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println(“cm”);
if(trig == true){
Engaged();}
{
delay(50);
unsigned int uS = sonar.ping();
unsigned int distance = uS / US_ROUNDTRIP_CM;
if(distance<10){
trig = true;}
delay(50);
if(distance>10){
trig = false;
digitalWrite(LED_PIN,LOW);}}}
Ohh and then its onto the flame sensors…if its gonna wander around the house , it may as well be doing something…