Advertisement
Maderdash

Untitled

Apr 25th, 2021
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.58 KB | None | 0 0
  1. /*
  2.   Arduino Starter Kit example
  3.   Project 5 - Servo Mood Indicator
  4.  
  5.   This sketch is written to accompany Project 5 in the Arduino Starter Kit
  6.  
  7.   Parts required:
  8.   - servo motor
  9.   - 10 kilohm potentiometer
  10.   - two 100 uF electrolytic capacitors
  11.  
  12.   created 13 Sep 2012
  13.   by Scott Fitzgerald
  14.  
  15.   http://www.arduino.cc/starterKit
  16.  
  17.   This example code is part of the public domain.
  18. */
  19.  
  20. // include the Servo library
  21. // ArduCAM Mini demo (C)2018 Lee
  22. // Web: http://www.ArduCAM.com
  23. // This program is a demo of how to use the enhanced functions
  24. // This demo was made for ArduCAM_Mini_2MP_Plus.
  25. // It can  continue shooting and store it into the SD card  in JPEG format
  26. // The demo sketch will do the following tasks
  27. // 1. Set the camera to JPEG output mode.
  28. // 2. Capture a JPEG photo and buffer the image to FIFO
  29. // 3.Write the picture data to the SD card
  30. // 5.close the file
  31. //You can change the FRAMES_NUM count to change the number of the picture.
  32. //IF the FRAMES_NUM is 0X00, take one photos
  33. //IF the FRAMES_NUM is 0X01, take two photos
  34. //IF the FRAMES_NUM is 0X02, take three photos
  35. //IF the FRAMES_NUM is 0X03, take four photos
  36. //IF the FRAMES_NUM is 0X04, take five photos
  37. //IF the FRAMES_NUM is 0X05, take six photos
  38. //IF the FRAMES_NUM is 0X06, take seven photos
  39. //IF the FRAMES_NUM is 0XFF, continue shooting until the FIFO is full
  40. //You can see the picture in the SD card.
  41. // This program requires the ArduCAM V4.0.0 (or later) library and ArduCAM_Mini_2MP_Plus
  42. // and use Arduino IDE 1.6.8 compiler or above
  43.  
  44. #include <Wire.h>
  45. #include <ArduCAM.h>
  46. #include <SPI.h>
  47. #include <SD.h>
  48. #include "memorysaver.h"
  49. //This demo can only work on OV5640_MINI_5MP_PLUS or OV5642_MINI_5MP_PLUS platform.
  50. #if !(defined (OV2640_MINI_2MP_PLUS))
  51. #error Please select the hardware platform and camera module in the ../libraries/ArduCAM/memorysaver.h file
  52. #endif
  53. #define   FRAMES_NUM    0x06
  54. // set pin 7 as the slave select for the digital pot:
  55. const int CS = 7;
  56. // sets up pins for ultrasonic sensor
  57. const int trigPin = 3;
  58. const int echoPin = 2;
  59. int toggle = 1;
  60. //declare vars to calc distance with ultra sensor
  61. float duration, distance;
  62. #define SD_CS 10
  63. bool is_header = false;
  64. int total_time = 0;
  65. #if defined (OV2640_MINI_2MP_PLUS)
  66. ArduCAM myCAM( OV2640, CS );
  67. #endif
  68. uint8_t read_fifo_burst(ArduCAM myCAM);
  69. void setup() {
  70.   // put your setup code here, to run once:
  71.   pinMode(trigPin, OUTPUT);
  72.   pinMode(echoPin, INPUT);
  73.   Serial.begin(9600);
  74.   // above 3 lines are setting up ultrasonic sensor
  75.   uint8_t vid, pid;
  76.   uint8_t temp;
  77. #if defined(__SAM3X8E__)
  78.   Wire1.begin();
  79. #else
  80.   Wire.begin();
  81. #endif
  82.   Serial.begin(115200);
  83.   Serial.println(F("ArduCAM Start!"));
  84.   // set the CS as an output:
  85.   pinMode(CS, OUTPUT);
  86.   digitalWrite(CS, HIGH);
  87.   // initialize SPI:
  88.   SPI.begin();
  89.   //Reset the CPLD
  90.   myCAM.write_reg(0x07, 0x80);
  91.   delay(100);
  92.   myCAM.write_reg(0x07, 0x00);
  93.   delay(100);
  94.   while (1) {
  95.     //Check if the ArduCAM SPI bus is OK
  96.     myCAM.write_reg(ARDUCHIP_TEST1, 0x55);
  97.     temp = myCAM.read_reg(ARDUCHIP_TEST1);
  98.     if (temp != 0x55)
  99.     {
  100.       Serial.println(F("SPI interface Error!"));
  101.       delay(1000); continue;
  102.     } else {
  103.       Serial.println(F("SPI interface OK.")); break;
  104.     }
  105.   }
  106. #if defined (OV2640_MINI_2MP_PLUS)
  107.   while (1) {
  108.     //Check if the camera module type is OV2640
  109.     myCAM.wrSensorReg8_8(0xff, 0x01);
  110.     myCAM.rdSensorReg8_8(OV2640_CHIPID_HIGH, &vid);
  111.     myCAM.rdSensorReg8_8(OV2640_CHIPID_LOW, &pid);
  112.     if ((vid != 0x26 ) && (( pid != 0x41 ) || ( pid != 0x42 ))) {
  113.       Serial.println(F("ACK CMD Can't find OV2640 module!"));
  114.       delay(1000); continue;
  115.     }
  116.     else {
  117.       Serial.println(F("ACK CMD OV2640 detected.")); break;
  118.     }
  119.   }
  120. #endif
  121.   //Initialize SD Card
  122.   while (!SD.begin(SD_CS))
  123.   {
  124.     Serial.println(F("SD Card Error!")); delay(1000);
  125.   }
  126.   Serial.println(F("SD Card detected."));
  127.   //Change to JPEG capture mode and initialize the OV5640 module
  128.   myCAM.set_format(JPEG);
  129.   myCAM.InitCAM();
  130.   myCAM.clear_fifo_flag();
  131.   myCAM.write_reg(ARDUCHIP_FRAMES, FRAMES_NUM);
  132. }
  133.  
  134. void loop() {
  135.   // put your main code here, to run repeatedly:
  136.   digitalWrite(trigPin, LOW);
  137.   delayMicroseconds(2);
  138.   digitalWrite(trigPin, HIGH);
  139.   delayMicroseconds(10);
  140.   digitalWrite(trigPin, LOW);
  141.   duration = pulseIn(echoPin, HIGH);
  142.   distance = (duration * .0343) / 2;
  143.   Serial.println(distance);
  144.   // above is for ultra sensor
  145.   myCAM.flush_fifo();
  146.   myCAM.clear_fifo_flag();
  147. #if defined (OV2640_MINI_2MP_PLUS)
  148.   myCAM.OV2640_set_JPEG_size(OV2640_1600x1200);
  149. #endif
  150.   //Start capture
  151.   while (distance <= 15 || toggle == 1){
  152.     myCAM.start_capture();
  153.     Serial.println(F("start capture."));
  154.     total_time = millis();
  155.     while ( !myCAM.get_bit(ARDUCHIP_TRIG, CAP_DONE_MASK));
  156.     Serial.println(F("CAM Capture Done."));
  157.     total_time = millis() - total_time;
  158.     Serial.print(F("capture total_time used (in miliseconds):"));
  159.     Serial.println(total_time, DEC);
  160.     total_time = millis();
  161.     read_fifo_burst(myCAM);
  162.     total_time = millis() - total_time;
  163.     Serial.print(F("save capture total_time used (in miliseconds):"));
  164.     Serial.println(total_time, DEC);
  165.     //Clear the capture done flag
  166.     myCAM.clear_fifo_flag();
  167.     delay(5000);
  168.  
  169.     uint8_t read_fifo_burst(ArduCAM myCAM)
  170.     {
  171.       uint8_t temp = 0, temp_last = 0;
  172.       uint32_t length = 0;
  173.       static int i = 0;
  174.       static int k = 0;
  175.       char str[16];
  176.       File outFile;
  177.       byte buf[256];
  178.       length = myCAM.read_fifo_length();
  179.       Serial.print(F("The fifo length is :"));
  180.       Serial.println(length, DEC);
  181.       if (length >= MAX_FIFO_SIZE) //8M
  182.       {
  183.         Serial.println("Over size.");
  184.         return 0;
  185.       }
  186.       if (length == 0 ) //0 kb
  187.       {
  188.         Serial.println(F("Size is 0."));
  189.         return 0;
  190.       }
  191.       myCAM.CS_LOW();
  192.       myCAM.set_fifo_burst();//Set fifo burst mode
  193.       i = 0;
  194.       while ( length-- )
  195.       {
  196.         temp_last = temp;
  197.         temp =  SPI.transfer(0x00);
  198.         //Read JPEG data from FIFO
  199.         if ( (temp == 0xD9) && (temp_last == 0xFF) ) //If find the end ,break while,
  200.         {
  201.           buf[i++] = temp;  //save the last  0XD9
  202.           //Write the remain bytes in the buffer
  203.           myCAM.CS_HIGH();
  204.           outFile.write(buf, i);
  205.           //Close the file
  206.           outFile.close();
  207.           Serial.println(F("OK"));
  208.           is_header = false;
  209.           myCAM.CS_LOW();
  210.           myCAM.set_fifo_burst();
  211.           i = 0;
  212.         }
  213.         if (is_header == true)
  214.         {
  215.           //Write image data to buffer if not full
  216.           if (i < 256)
  217.             buf[i++] = temp;
  218.           else
  219.           {
  220.             //Write 256 bytes image data to file
  221.             myCAM.CS_HIGH();
  222.             outFile.write(buf, 256);
  223.             i = 0;
  224.             buf[i++] = temp;
  225.             myCAM.CS_LOW();
  226.             myCAM.set_fifo_burst();
  227.           }
  228.         }
  229.         else if ((temp == 0xD8) & (temp_last == 0xFF))
  230.         {
  231.           is_header = true;
  232.           myCAM.CS_HIGH();
  233.           //Create a avi file
  234.           k = k + 1;
  235.           itoa(k, str, 10);
  236.           strcat(str, ".jpg");
  237.           //Open the new file
  238.           outFile = SD.open(str, O_WRITE | O_CREAT | O_TRUNC);
  239.           if (! outFile)
  240.           {
  241.             Serial.println(F("File open failed"));
  242.             while (1);
  243.           }
  244.           myCAM.CS_LOW();
  245.           myCAM.set_fifo_burst();
  246.           buf[i++] = temp_last;
  247.           buf[i++] = temp;
  248.         }
  249.       }
  250.       myCAM.CS_HIGH();
  251.       return 1;
  252.     }
  253.     toggle = 0;
  254.   }
  255.  
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement