Close

code change, PIR and microphone

A project log for ScrapTrack

Robots scrounged from various toys, with hobby controllers.

flozFloz 12/25/2014 at 11:060 Comments

Got some basic stuff worked out for the PIR and microphone. Right now, he (not sure why I picked male gender, maybe the cool camo look?) makes two different beeps for the sensors, and reports a noteworthy sensor value over serial at 57600 baud.

//. Motor driver shield- 2012 Copyright (c) Seeed Technology Inc.
// 
//  Original Author: Jimbo.we
//  Contribution: LG
//  
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
//
//  Basic motion based on A0 A4 sensor input by Aaron "Floz" Casper
//  for TankBot
//
// set up variables 
// name pins
// 
int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface 
int speedpinA=9;//enable motor A
int pinI3=12;//define I3 interface 
int pinI4=13;//define I4 interface 
int speedpinB=10;//enable motor B
int SpeakerPin = A5;
int speed_A =0;//define the speed of motor_A
int speed_B =0;//define the speed of motor_B
const int photoCell_A = A4; // light sensor_A pin
const int photoCell_B = A0; // light sensor_B pin
const int PIR_signal_pin = A3;
const int MIC_signal_pin = A2;
int sensor_value_A = 0;// zero all our math
int sensor_value_B = 0;// variables and sensors
int sensor_value_PIR = 0;
int sensor_value_MIC = 0;
int output_value_A = 0;
int output_value_B = 0;

void setup()
{
   // tell the arduino which pins are input and
   // which ones are output
  pinMode(MIC_signal_pin, INPUT);
  pinMode(PIR_signal_pin, INPUT);
  pinMode(photoCell_A, INPUT);
  pinMode(photoCell_B, INPUT);
  pinMode(pinI1,OUTPUT);
  pinMode(pinI2,OUTPUT);
  pinMode(speedpinA,OUTPUT);
  pinMode(pinI3,OUTPUT);
  pinMode(pinI4,OUTPUT);
  pinMode(speedpinB,OUTPUT);
  Serial.begin(57600);
}
 
void forward()
{
  
  //This is really the only movement function used to begin with
  //it enables the motor to a speed determined by the photocell
  //this configuration should chase bright light.
  //
  
     analogWrite(speedpinA,speed_A);//input a simulation value to set the speed
     analogWrite(speedpinB,speed_B);
     digitalWrite(pinI4,LOW);//turn DC Motor B move clockwise
     digitalWrite(pinI3,HIGH);
     digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
     digitalWrite(pinI1,HIGH);
}
void stop()
{
     // shuts the motors down by setting both speed-pins
     // to LOW
     digitalWrite(speedpinA,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor. 
     digitalWrite(speedpinB,LOW);
     delay(1000);
 
}

void loop()
{
  // read sensor values
  sensor_value_A= analogRead(photoCell_A);
  sensor_value_B = analogRead(photoCell_B);
  sensor_value_PIR = analogRead(PIR_signal_pin);
  sensor_value_MIC = analogRead(MIC_signal_pin);
  // map sensor readings to 0-255 range
  sensor_value_MIC = map(sensor_value_MIC, 924, 1024, 0, 255);
  output_value_A = map(sensor_value_A, 0, 30, 0, 255);
  output_value_B = map(sensor_value_B, 0, 30, 0, 255);
 
   if (sensor_value_PIR <= 5){
    tone(SpeakerPin, 200,50);
   Serial.println("motion detected!");
  }
   if (sensor_value_MIC >= 75){
    tone(SpeakerPin, 500,25);
   Serial.println("loud noise detected!");
  
 }  
 
  // normalize Mic input to 0-255
  if (sensor_value_MIC >= 255){
    sensor_value_MIC = 255;
  }
  if (sensor_value_MIC <= 0){
    sensor_value_MIC = 0;
  }
 
  // don't pass values beyond what the motor controller
  // can handle for speed.  
  if (output_value_A >= 255){
    output_value_A = 255;
  }
    if (output_value_B >= 255){
    output_value_B = 255;
  }
  // don't overwork the controllers trying to make tiny movements
  // chances are they won't turn on the motor enough to move, and
  // just get hot.
  
  if (output_value_A <= 128){
    output_value_A = 0;
  }
    if (output_value_B <= 128){
    output_value_B = 0;
  }
  
  // set the speed pin values for each motor controller
  speed_A= (output_value_A);
  speed_B= (output_value_B);
  
  //serial debugging output
  //Serial.print(analogRead(photoCell_A));
  //Serial.print(" ");
  //Serial.print(output_value_A);
  //Serial.print(" ");
  //Serial.print(analogRead(photoCell_B));
  //Serial.print(" ");
  //Serial.print(output_value_B);
  //Serial.print(speed_A); 

  //Serial.print(sensor_value_PIR);
  //Serial.print(sensor_value_MIC);
  //Serial.println(" ");
  
  // move forward with speed_A and speed_B
  
  
  //UNCOMMENT THE FOLLOWING LINE
  //OR ELSE THE ROBOT WILL NOT GO
  forward();
  
   // delay to help jitter/freakouts
    delay(100);
    
}
Some minor changes, adding the PIR and Microphone. Apologies for the lack of documentation on the build. A tip for those geting into it, the seedstudio motor shield seems to pass appropriate voltage to the 'duino, and power the motors from one battery. My fancy-pants wiring harness was at least halfway for naught.

Discussions