I've been working on hardware, construction, and testing, but now I'm to the point where I have to figure out code. I have the general construct of what I want in my mind so now I am googling syntax and testing what I find.
I'm not asking for any answers - just feedback.
Here's the test electronics on my workspace. Facebook
/*
PortalBot
Parts:
-PIR motion sensor
-PING ultrasonic sensor
-Servo
-Red LED
Wiring
-PING to digital pin 7
-PIR to digital pin 8
-Servo to digital pin 9
-LED to digital pin 13
Code borrowed from example sketches:
-Sweep
-Button
Original code created May 2011
by Daniel Gentleman
thoughtfix@gmail.com
http://thoughtfix.com
assistance from #arduino on irc.freenode.net:
- irseeyou
- sabesto
- xkr47
- aki
- Milez-
Goal of program structure:
Set flag room == unscanned
Run roomScan subroutine. Populate array baseScan[pos] = duration
Set flag room == scanned
Sit and wait for infrared trigger irState = HIGH
When IR triggers, run alarmScan routine. Compare baseScan to new durations
Set servo to aim at target. Turn on laser.
*/
#include <Servo.h>
const int pingPin = 7; // Ping sensor
const int irsensePin = 8; // the number of the pirsensor pin
const int ledPin = 13 ; // the number of the LED pin
int room = 0 ; // Initial scan not done
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int irsenseState = 0; // variable for reading the pirsensor status
int motionDetected = 0; // Motion variable
int laserOn = 0; // Laser variable
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pirsensor pin as an input:
pinMode(irsensePin, INPUT);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop(){
// read the state of the pirsensor value:
irsenseState = digitalRead(irsensePin);
if (room = 0) {
int roomScan; // perform the initial room scan
}
// check if the pirsensor detects movement.
if (irsenseState == HIGH || motionDetected) {
// Activate scan
digitalWrite(ledPin, HIGH);
int alarmScan();
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
void roomScan (){
long duration;
for(pos = 30; pos < 150; pos += 3) // goes from 0 degrees to 180 degrees
{ // in steps of 3 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// THIS IS WHERE THE ARRAY NEEDS TO BE POPUALATED
// I don't know how, so I am just doing serial output.
Serial.print(duration);
Serial.print("msec, ");
Serial.println();
delay(15); // waits 15ms for the servo to reach the position
}
int room = 1; // sets room as scanned
}
void alarmScan(){
// THIS IS WHERE IT CYCLES THROUGH THE ARRAY
// for each pos it'll gather a new distance
// and compare that to the array's distance
// If the distance is shorter, trigger targetLock
}
void targetLock() {
// THIS IS WHERE THE LASER IS POINTED
// A mirror will be on the sonar servo
// simply point the servo to the target
// and turn on the laser for 20 sec, then
// set room = 0 to restart.
}