Arduino powered turret from Portal game (populating arrays)

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. 
}
if (room = 0) {

Probably want == here, not =.

   int alarmScan();

What is the int keyword for? You are not declaring a function or a variable.

Put the ping code in a function that returns a long. Call that function in the loop that moves the servo.

  // THIS IS WHERE THE ARRAY NEEDS TO BE POPUALATED
  // I don't know how, so I am just doing serial output.

Which array? You haven't declared an array to hold the values.

If you had declared an array, like this:

long dist[50];

you could use a for loop like this:

for(int pos=30, ind=0; pos<180; pos+=3, i++)
{
   dist[ind] = pingDist();
}

pingDist() is the function you are going to create.

  int room = 1; // sets room as scanned

Sets the local variable, room. to 1, not the global variable declared at the top of the code. The local variable goes out of scope immediately, so this statement is useless.

Wow Paul... Thank you very much.
The array is what was holding me up ... I haven't defined it because I didn't know how. I'll tinker with the code examples you gave me and let you know how it goes.

I knew I came to the right place :smiley:

I'm still a little stumped on how to create an array and populate it with data, them poll the array to compare the data.

I know I'll need to make a function int getPing(pos){ but still am learning how to toss data to a function, get data from it, then use the combination to make the array.

I'll figure it out... back to the forums and googling!

I am not able to add any input for the coding of your project but I stumbled on this thread while lurking with the intentions of learning and liked the idea of what you are working on. I think maybe a cheap lazer pointer wired up instead of the led may be a cool addition in retrospect to the game.

I'm still a little stumped on how to create an array

Creating an array is simple:
arrayType variableName[arraySize];
arrayType defines the kind of data that the array will hold (int, byte, float, Servo, etc.)
variableName is the name of the array you are creating.
arraySize defines how many elements there are to be in the array.

and populate it with data

One element at a time, using the assignment operator (=).

them poll the array to compare the data.

This needs a little clarification. You can reference the nth element in an array using the [] operator:
int a = array[7];
This gets the 8th element from the array named array, and stores it in the variable a.

if(array[3] == 14)
{
}

This does something if the 4th element in the array contains the value 14.

I know I'll need to make a function int getPing(pos){

Actually, you need to make a function called pingDist that takes no arguments, and returns a long:

long pingDist()
{
   long dist = 0;
   // Do some stuff that might change the value in dist
   return dist;
}

but still am learning how to toss data to a function

Proper terminology helps. You pass data to a function, not toss. You pass data through arguments to the function.
int myFunc(int a, int b, float c)
{
return 9;
}
This defines a function that takes 3 arguments. The first 2 are ints. The 3rd is a float. The function returns an int.

You call this function like so:

int x = 2;
int y = 3
float pi = 3.14159; 
int myVal = myFunc(x, y, pi);

then use the combination to make the array.

Terminology again. You populate or initialize or value an array or variable with the data returned from a function, not make. The array or variable must already exist, except when the creation and valuation of a variable is done in one step, as in the x, y, pi, and myVal examples above.

Thank you again, Paul. I'll have new code to post tonight and show you progress.