I need a help with this code

I’m creating a PIR Sensor Alarm with Arduino. I’m using Arduino Uno, PIR Sensor, Breadboard, LED, Buzzer, TTL JPEG Camera, Arduino shield and SD Card.

I already have the code to make my project work, but I still can’t figure out the right code for the Camera… so I need some help with that… :blush:

here’s my code:

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int pinSpeaker = 10;           //Set up a speaker on a PWM pin (digital 9, 10, or 11)

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(pinSpeaker, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    playTone(300, 160);
    delay(150);

    
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
      digitalWrite(ledPin, LOW); // turn LED OFF
      playTone(0, 0);
      delay(300);    
      if (pirState == HIGH){
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
    duration *= 1000;
    int period = (1.0 / freq) * 1000000;
    long elapsed_time = 0;
    while (elapsed_time < duration) {
        digitalWrite(pinSpeaker,HIGH);
        delayMicroseconds(period / 2);
        digitalWrite(pinSpeaker, LOW);
        delayMicroseconds(period / 2);
        elapsed_time += (period);
    }
}

Thanks!

I've seen other projects where people use a TTL camera, so I suggest you search the playground and forum for related projects. "Arduino ttl serial camera" would probably be a good search term.

@DesertRose
what are your expectations of the jpeg camera?

robtillaart:
@DesertRose
what are your expectations of the jpeg camera?

My idea is that the JPEG Camera should work with the PIR Sensor and a capture a photo that must be stored in the SD Card.

Arduino still image to SD card - 10 to 15 seconds
http://www.ladyada.net/make/IoTcamera/

Look for the long thread here about the OV7670 camera.

michinyon:
Look for the long thread here about the OV7670 camera.

Do you mean this:-

Not very long nor successful is it?

OK, I added a code for the camera, I tested it and it didn’t give me any errors.

However, the LED and the Buzzer are on all the time, my idea is to have them on once the PIR Sensor detects motions. I know that I’m having problem with the code. Therefore, I’d appreciate it so much if someone could find out which parts of my code should I change :cold_sweat:

Thanks a lot!

#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>

int pirPin = 7;
int ledPin = 8;                // choose the pin for the LED
int pinSpeaker = 10;           //Set up a speaker on a PWM pin (digital 9, 10, or 11)

int minSecsBetweenEmails = 60; // 1 min

long lastSend = -minSecsBetweenEmails * 1000l;
 
byte ZERO = 0x00;
 
byte incomingbyte;
SoftwareSerial mySerial(2,3);          // Set Arduino pin 2 and 3 as softserial
 
long int a=0x0000,j=0,k=0,count=0,i=0;
uint8_t MH,ML;
boolean EndFlag=0;
 
File  myFile;
 
void SendResetCmd();
void SetBaudRateCmd();
void SetImageSizeCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();
 
void setup()
{
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(pinSpeaker, OUTPUT);
  Serial.begin(9600);
  
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
 
mySerial.begin(38400);
 
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
 
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
 
}
 
void loop()
{
  long now = millis();
  if (digitalRead(pirPin) == HIGH)
  {
    digitalWrite(ledPin, HIGH);  // turn LED ON
    playTone(300, 160);
    delay(150);

    if (now > (lastSend + minSecsBetweenEmails * 1000l))
    {
      Serial.println("MOVEMENT");
      lastSend = now;
    }
    else
    {
      digitalWrite(ledPin, LOW); // turn LED OFF
      playTone(0, 0);
      delay(300);   
      Serial.println("Too soon");
    }
  }
  delay(300);
}

// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
    duration *= 1000;
    int period = (1.0 / freq) * 1000000;
    long elapsed_time = 0;
    while (elapsed_time < duration) {
        digitalWrite(pinSpeaker,HIGH);
        delayMicroseconds(period / 2);
        digitalWrite(pinSpeaker, LOW);
        delayMicroseconds(period / 2);
        elapsed_time += (period);
    }
 
byte a[32];
int ii;
 
SendResetCmd();
delay(4000);                            //Wait 2-3 second to send take picture command
 
SendTakePhotoCmd();
 
while(mySerial.available()>0)
{
  incomingbyte=mySerial.read();
}
 
myFile = SD.open("pic.jpg", FILE_WRITE); //<strong><span style="color: #ff0000;">The file name should not be too long</span></strong>
 
while(!EndFlag)
{
  j=0;
  k=0;
  count=0;
 
  SendReadDataCmd(); 
  delay(200); //250 for regular
  while(mySerial.available()>0)
  {
     incomingbyte=mySerial.read();
    k++;
    if((k>5)&&(j<32)&&(!EndFlag))
    {
     a[j]=incomingbyte;
     if((a[j-1]==0xFF)&&(a[j]==0xD9))     //tell if the picture is finished
     EndFlag=1;
     j++;
    count++;
    }
  }
 
for(j=0;j<count;j++)
{
if(a[j]<0x10)
Serial.print("0");
Serial.print(a[j],HEX);           // observe the image through serial port
Serial.print(" ");
}
 
for(ii=0; ii<count; ii++)
myFile.write(a[ii]);
 
Serial.println();
 
i++;
 
}
 
myFile.close();
 
Serial.print("Finished writing data to file");
 
while(1);
 
}
 
void SendResetCmd()
{
mySerial.write(0x56);
mySerial.write(ZERO);
mySerial.write(0x26);
mySerial.write(ZERO);
}
 
void SetImageSizeCmd()
{
mySerial.write(0x56);
mySerial.write(ZERO);
mySerial.write(0x31);
mySerial.write(0x05);
mySerial.write(0x04);
mySerial.write(0x01);
mySerial.write(ZERO);
mySerial.write(0x19);
mySerial.write(0x11);
}
 
void SetBaudRateCmd()
{
mySerial.write(0x56);
mySerial.write(ZERO);
mySerial.write(0x24);
mySerial.write(0x03);
mySerial.write(0x01);
mySerial.write(0x2A);
mySerial.write(0xC8);
 
}
 
void SendTakePhotoCmd()
{
mySerial.write(0x56);
mySerial.write(ZERO);
mySerial.write(0x36);
mySerial.write(0x01);
mySerial.write(ZERO);
}
 
void SendReadDataCmd()
{
MH=a/0x100;
ML=a%0x100;
mySerial.write(0x56);
mySerial.write(ZERO);
mySerial.write(0x32);
mySerial.write(0x0c);
mySerial.write(ZERO);
mySerial.write(0x0a);
mySerial.write(ZERO);
mySerial.write(ZERO);
mySerial.write(MH);
mySerial.write(ML);
mySerial.write(ZERO);
mySerial.write(ZERO);
mySerial.write(ZERO);
mySerial.write(0x20);
mySerial.write(ZERO);
mySerial.write(0x0a);
a+=0x20;
}
 
void StopTakePhotoCmd()
{
mySerial.write(0x56);
mySerial.write(ZERO);
mySerial.write(0x36);
mySerial.write(0x01);
mySerial.write(0x03);
}

If there is motion, you turn the LED on. If there isn't, you don't turn it off. Why not?

long int a=0x0000

That's different from 0 somehow?