<commit_before>/* Main code for mouse behavoior tracking for two pellet dispensors */
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
#include <SdFat.h> //SD and RTC libraries
#include "RTClib.h"
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SharpMem.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <SoftwareSerial.h>
//#include <Adafruit_MotorShield.h>
//#include "utility/Adafruit_PWMServoDriver.h"

#define FILENAME "Feeder_Data.csv"

#define DISPLAY_SERIAL_RX_PIN 255 // we don't need a receive pin, 255 indicates this
// Connect the Arduino pin 3 to the rx pin on the 7 segment display
#define DISPLAY_SERIAL_TX_PIN 9

// Photo interrupter is also the wakeup for the system
#define PHOTO_INTERRUPTER_PIN_1 2
//#define PHOTO_INTERRUPTER_PIN_2 3

const int PHOTO_INTERRUPTER_PINS[] = { PHOTO_INTERRUPTER_PIN_1 };

SoftwareSerial LEDserial = SoftwareSerial(DISPLAY_SERIAL_RX_PIN, DISPLAY_SERIAL_TX_PIN);
SdFat SD;

long previousMillis = 0;
long startTime = 0;
long timeElapsed = 0;
const long day2 = 86400000; // 86400000 milliseconds in a day
const long hour2 = 3600000; // 3600000 milliseconds in an hour
const long minute2 = 60000; // 60000 milliseconds in a minute
const long second2 =  1000; // 1000 milliseconds in a second

const int CS_pin = 10;
RTC_DS1307 RTC;    // refer to the real-time clock on the SD shield
String time;
File dataFile;

const int TTL_DEBUG_PIN = 3;

const int STEPS_TO_INCREMENT = 64;
const int MOTOR_STEPS_PER_REVOLUTION = 513;

int PIStates[] = { 1 };
int lastStates[] = { 1 };

int pelletCount[] = { 0 };

// Stepper motor with shield test
//Adafruit_MotorShield gMotorShield = Adafruit_MotorShield();
//Adafruit_StepperMotor *gPtrToStepper = gMotorShield.getStepper(MOTOR_STEPS_PER_REVOLUTION,2);

int logData()
{
  String year, month, day, hour, minute, second;
  power_twi_enable();
  power_spi_enable();

  DateTime datetime = RTC.now();
  year = String(datetime.year(), DEC);
  month = String(datetime.month(), DEC);
  day  = String(datetime.day(),  DEC);
  hour  = String(datetime.hour(),  DEC);
  minute = String(datetime.minute(), DEC);
  second = String(datetime.second(), DEC);

  // concatenates the strings defined above into date and time
  time = month + "/" + day + " " + hour + ":" + minute + ":" + second;

  // opens a file on the SD card and prints a new line with the
  // current reinforcement schedule, the time stamp,
  // the number of sucrose deliveries, the number of active and
  // inactive pokes, and the number of drinking well entries.

  dataFile = SD.open(FILENAME, FILE_WRITE);
  if (dataFile)
  {
    Serial.println(F("File successfully written..."));
    Serial.println(time);
    dataFile.print(time);
    dataFile.print(",");
    for (int i = 0; i < sizeof(pelletCount); i++)
    {
      dataFile.print(pelletCount[i]);
      dataFile.print(",");
    }
    dataFile.println(timeElapsed);
    dataFile.close();
  }
  power_twi_disable();
  power_spi_disable();
}

//Clears the display and resets the cursor
void clearDisplay()
{ LEDserial.write(0x76); } // Clear display command

void setup()
{
  // make all unused pins inputs with pullups enabled by default, lowest power drain
  // leave pins 0 & 1 (Arduino RX and TX) as they are
  for (byte i=2; i <= 20; i++)
  { pinMode(i, INPUT_PULLUP); }
  ADCSRA = 0;  // disable ADC as we won't be using it
  power_adc_disable(); // ADC converter
  power_timer1_disable();// Timer 1
  power_timer2_disable();// Timer 2

  Serial.begin(9600);
  Serial.println(F("Starting up..."));

  // Display setup
  LEDserial.begin(9600); 
  pinMode(DISPLAY_SERIAL_TX_PIN, OUTPUT);
  setDisplayBrightness(64);
  clearDisplay();
  delay(1); // allow a very short delay for display response
  setDisplayValues(pelletCount[0]);
  for (int i = 0; i < sizeof(PHOTO_INTERRUPTER_PINS); i++)
  { pinMode(PHOTO_INTERRUPTER_PINS[i], INPUT); }
  pinMode(TTL_DEBUG_PIN, OUTPUT);
  pinMode(CS_pin, OUTPUT);
  pinMode(SS, OUTPUT);

  Wire.begin();
  
  // both the stepper control and RTC use I2C (the Wire library)
  RTC.begin(); // RTC library needs Wire

  // set up stepper
  //gMotorShield.begin(); // use default I2C address of 0x40
  //gPtrToStepper->setSpeed(30); // rpm, max suggested by Adafruit for this 5V stepper

  if (! RTC.isrunning()) {
    Serial.println(F("RTC is NOT running!"));
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

  if (!SD.begin(CS_pin)) {
    Serial.println(F("Card failed, or not present"));
    while (1); //end
  }

  Serial.println(F("Card initialized."));

  dataFile = SD.open(FILENAME, FILE_WRITE);
  if (!dataFile) {
    Serial.println(F("Error opening datalog.txt"));
    while (1); //end
  }

  else {
    dataFile.print(time);
    String temp = "";
    for (int i = 1; i <= sizeof(pelletCount); i++)
    { temp += ",Pellet Count #" + String(i); }
    dataFile.println(temp);
    dataFile.close();
  }
  
  delay (500);
  
  // get ready for what the system should be doing
  for (int i = 0; i < sizeof(PHOTO_INTERRUPTER_PINS); i++)
  {
    PIStates[i] = digitalRead(PHOTO_INTERRUPTER_PINS[i]);
    lastStates[i] = 0;
  }
}

void loop()
{
  digitalWrite(TTL_DEBUG_PIN, LOW);
  if (!updateState(0))
  { enterSleep(); }
  delay(100);
}

bool updateState(int inputNum)
{
  PIStates[inputNum] = digitalRead(PHOTO_INTERRUPTER_PINS[inputNum]);
  Serial.print("Photointerrupter State " + String(inputNum + 1) + ": ");<commit_msg>Fixed data file header message<commit_after>/* Main code for mouse behavoior tracking for two pellet dispensors */
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
#include <SdFat.h> //SD and RTC libraries
#include "RTClib.h"
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SharpMem.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <SoftwareSerial.h>
//#include <Adafruit_MotorShield.h>
//#include "utility/Adafruit_PWMServoDriver.h"

#define FILENAME "Feeder_Data.csv"

#define DISPLAY_SERIAL_RX_PIN 255 // we don't need a receive pin, 255 indicates this
// Connect the Arduino pin 3 to the rx pin on the 7 segment display
#define DISPLAY_SERIAL_TX_PIN 9

// Photo interrupter is also the wakeup for the system
#define PHOTO_INTERRUPTER_PIN_1 2
//#define PHOTO_INTERRUPTER_PIN_2 3

const int PHOTO_INTERRUPTER_PINS[] = { PHOTO_INTERRUPTER_PIN_1 };

SoftwareSerial LEDserial = SoftwareSerial(DISPLAY_SERIAL_RX_PIN, DISPLAY_SERIAL_TX_PIN);
SdFat SD;

long previousMillis = 0;
long startTime = 0;
long timeElapsed = 0;
const long day2 = 86400000; // 86400000 milliseconds in a day
const long hour2 = 3600000; // 3600000 milliseconds in an hour
const long minute2 = 60000; // 60000 milliseconds in a minute
const long second2 =  1000; // 1000 milliseconds in a second

const int CS_pin = 10;
RTC_DS1307 RTC;    // refer to the real-time clock on the SD shield
String time;
File dataFile;

const int TTL_DEBUG_PIN = 3;

const int STEPS_TO_INCREMENT = 64;
const int MOTOR_STEPS_PER_REVOLUTION = 513;

int PIStates[] = { 1 };
int lastStates[] = { 1 };

int pelletCount[] = { 0 };

// Stepper motor with shield test
//Adafruit_MotorShield gMotorShield = Adafruit_MotorShield();
//Adafruit_StepperMotor *gPtrToStepper = gMotorShield.getStepper(MOTOR_STEPS_PER_REVOLUTION,2);

int logData()
{
  String year, month, day, hour, minute, second;
  power_twi_enable();
  power_spi_enable();

  DateTime datetime = RTC.now();
  year = String(datetime.year(), DEC);
  month = String(datetime.month(), DEC);
  day  = String(datetime.day(),  DEC);
  hour  = String(datetime.hour(),  DEC);
  minute = String(datetime.minute(), DEC);
  second = String(datetime.second(), DEC);

  // concatenates the strings defined above into date and time
  time = month + "/" + day + " " + hour + ":" + minute + ":" + second;

  // opens a file on the SD card and prints a new line with the
  // current reinforcement schedule, the time stamp,
  // the number of sucrose deliveries, the number of active and
  // inactive pokes, and the number of drinking well entries.

  dataFile = SD.open(FILENAME, FILE_WRITE);
  if (dataFile)
  {
    Serial.println(F("File successfully written..."));
    Serial.println(time);
    dataFile.print(time);
    dataFile.print(",");
    for (int i = 0; i < sizeof(pelletCount); i++)
    {
      dataFile.print(pelletCount[i]);
      dataFile.print(",");
    }
    dataFile.println(timeElapsed);
    dataFile.close();
  }
  power_twi_disable();
  power_spi_disable();
}

//Clears the display and resets the cursor
void clearDisplay()
{ LEDserial.write(0x76); } // Clear display command

void setup()
{
  // make all unused pins inputs with pullups enabled by default, lowest power drain
  // leave pins 0 & 1 (Arduino RX and TX) as they are
  for (byte i=2; i <= 20; i++)
  { pinMode(i, INPUT_PULLUP); }
  ADCSRA = 0;  // disable ADC as we won't be using it
  power_adc_disable(); // ADC converter
  power_timer1_disable();// Timer 1
  power_timer2_disable();// Timer 2

  Serial.begin(9600);
  Serial.println(F("Starting up..."));

  // Display setup
  LEDserial.begin(9600); 
  pinMode(DISPLAY_SERIAL_TX_PIN, OUTPUT);
  setDisplayBrightness(64);
  clearDisplay();
  delay(1); // allow a very short delay for display response
  setDisplayValues(pelletCount[0]);
  for (int i = 0; i < sizeof(PHOTO_INTERRUPTER_PINS); i++)
  { pinMode(PHOTO_INTERRUPTER_PINS[i], INPUT); }
  pinMode(TTL_DEBUG_PIN, OUTPUT);
  pinMode(CS_pin, OUTPUT);
  pinMode(SS, OUTPUT);

  Wire.begin();
  
  // both the stepper control and RTC use I2C (the Wire library)
  RTC.begin(); // RTC library needs Wire

  // set up stepper
  //gMotorShield.begin(); // use default I2C address of 0x40
  //gPtrToStepper->setSpeed(30); // rpm, max suggested by Adafruit for this 5V stepper

  if (! RTC.isrunning()) {
    Serial.println(F("RTC is NOT running!"));
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

  if (!SD.begin(CS_pin)) {
    Serial.println(F("Card failed, or not present"));
    while (1); //end
  }

  Serial.println(F("Card initialized."));

  dataFile = SD.open(FILENAME, FILE_WRITE);
  if (!dataFile) {
    Serial.println(F("Error opening datalog.txt"));
    while (1); //end
  }

  else {
    dataFile.print(time);
    String temp = "";
    for (int i = 1; i <= sizeof(pelletCount); i++)
    { temp += ",Pellet Count #" + String(i); }
    temp = "Time" + temp + ",Pellet Drop Delay";
    dataFile.println(temp);
    dataFile.close();
  }
  
  delay (500);
  
  // get ready for what the system should be doing
  for (int i = 0; i < sizeof(PHOTO_INTERRUPTER_PINS); i++)
  {
    PIStates[i] = digitalRead(PHOTO_INTERRUPTER_PINS[i]);
    lastStates[i] = 0;
  }
}

void loop()
{
  digitalWrite(TTL_DEBUG_PIN, LOW);
  if (!updateState(0))
  { enterSleep(); }
  delay(100);
}

bool updateState(int inputNum)
{
  PIStates[inputNum] = digitalRead(PHOTO_INTERRUPTER_PINS[inputNum]);
  Serial.print("Photointerrupter State " + String(inputNum + 1) + ": ");
{'fraction_non_alphanumeric': 0.13085664335664335, 'fraction_numerical': 0.026223776223776224, 'mean_word_length': 3.8031066330814443, 'pattern_counts': {'":': 6, '<': 37, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5648740', 'n_tokens_mistral': 4339, 'n_tokens_neox': 3914, 'n_words': 1154}