Skip to content

Commit

Permalink
Merge pull request KravitzLabDevices#53 from mcci-catena/issue1
Browse files Browse the repository at this point in the history
add feature to transmit data over UART interface
  • Loading branch information
KravitzLab authored Dec 15, 2022
2 parents 59d1883 + 5647e68 commit 9dd00c1
Show file tree
Hide file tree
Showing 12 changed files with 471 additions and 30 deletions.
1 change: 1 addition & 0 deletions examples/1_Programs/ClassicFED3/ClassicFED3.ino
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ int pokes_required = 1; // increase the number of

void setup() {
fed3.ClassicFED3 = true;
fed3.LoRaTransmit = true; //set 'true' to send data to LoRa, if using BNC set 'false'
fed3.begin(); //Setup the FED3 hardware
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ String sketch = "ClosedEcon_PR1"; //Unique identifier text
FED3 fed3 (sketch); //Start the FED3 object

void setup() {
fed3.LoRaTransmit = false; //set 'true' to send data to LoRa, if using BNC set 'false'
fed3.begin(); //Setup the FED3 hardware
fed3.FEDmode = 1; //Customize the display options to FEDmode 1 for an operant session
fed3.EnableSleep = true; //Set to false to inhibit sleeping to use the Serial port; Set to true to reduce battery power
Expand Down
1 change: 1 addition & 0 deletions examples/1_Programs/Dispenser/Dispenser.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ String sketch = "Dispenser"; //Unique identifier text
FED3 fed3 (sketch); //Start the FED3 object

void setup() {
fed3.LoRaTransmit = false; //set 'true' to send data to LoRa, if using BNC set 'false'(default value)
fed3.DisplayPokes = false; //turn off poke indicators on screen
fed3.disableSleep(); //disable sleep for this program as the BNC port is not currently set up as an interrupt
fed3.begin(); //Setup the FED3 hardware
Expand Down
1 change: 1 addition & 0 deletions examples/1_Programs/Extinction/Extinction.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ String sketch = "Ext"; //Unique identifier text
FED3 fed3 (sketch); //Start the FED3 object

void setup() {
fed3.LoRaTransmit = false; //set 'true' to send data to LoRa, if using BNC set 'false'
fed3.begin(); //Setup the FED3 hardware
}

Expand Down
1 change: 1 addition & 0 deletions examples/1_Programs/FR_Customizable/FR_Customizable.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ String sketch = "FRCustom"; //Unique identifier text f
FED3 fed3 (sketch); //Start the FED3 object

void setup() {
fed3.LoRaTransmit = true; //set 'true' to send data to LoRa, if using BNC set 'false'
fed3.begin(); //Setup the FED3 hardware
fed3.FR = FR; //Share the FR ratio with the fed3 library so it is logged on the SD card and displayed on the screen
if (LeftActive == false) {
Expand Down
1 change: 1 addition & 0 deletions examples/1_Programs/FixedRatio1/FixedRatio1.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ String sketch = "FR1"; //Unique identifier text
FED3 fed3 (sketch); //Start the FED3 object

void setup() {
fed3.LoRaTransmit = true; //set 'true' to send data to LoRa, if using BNC set 'false'
fed3.begin(); //Setup the FED3 hardware
}

Expand Down
1 change: 1 addition & 0 deletions examples/1_Programs/FreeFeeding/FreeFeeding.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ String sketch = "FreeFeed"; //Unique identifier text
FED3 fed3 (sketch); //Start the FED3 object

void setup() {
fed3.LoRaTransmit = true; //set 'true' to send data to LoRa, if using BNC set 'false'
fed3.begin(); //Setup the FED3 hardware
fed3.DisplayPokes = false; //Customize the DisplayPokes option to 'false' to not display the poke indicators
}
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=FED3
version=1.16.3
version=1.17.0
author=Lex Kravitz
maintainer=Lex Kravitz <alexxai@wustl.edu>
depends=Adafruit BusIO, Adafruit GFX Library, Adafruit NeoPixel, Adafruit SHARP Memory Display, Adafruit Unified Sensor, Arduino Low Power, RTClib, RTCZero, SdFat - Adafruit Fork, Adafruit AHTX0, Adafruit SH110X
Expand Down
15 changes: 13 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## This library controls [FED3](https://github.com/KravitzLabDevices/FED3), a device for training mice.
<p>
The files on this Github repository should be considered "beta", for the most recent stable release please install the FED3 library from the Arduino IDE as decribed below. See the [Wiki](https://github.com/KravitzLabDevices/FED3_library/wiki) for documentation on how to use the library. This library is in development, please report bugs using Issues.
The files on this Github repository should be considered "beta", for the most recent stable release please install the FED3 library from the Arduino IDE as decribed below. See the [Wiki](https://github.com/KravitzLabDevices/FED3_library/wiki) for documentation on how to use the library. This library is in development, please report bugs using Issues.

This repository in addition includes the feature of transmitting the data from FED3 to other devices via UART interface. BNC support is removed in this version, as Pin A0 is configured as UART_Tx instead of BNC_OUT.

## Do you have custom FED3 code you want to add to the libary? <br>[Submit it here!](https://kravitzlab.com/submit-custom-fed3-code)

Expand Down Expand Up @@ -37,5 +39,14 @@ Open the "SetClock" example sketch in the Arduino IDE and flash this code to the
Check out the example FED3 programs by clicking File > Examples > FED3 <br>
![FED3 examples](https://raw.githubusercontent.com/KravitzLabDevices/FED3/main/photos/FED3example2.png)

### 7. Start coding!
### 7. Send data to LoRaWAN network
Set flag `LoRaTransmit` in the sketch to transmit FED3 data over the network. Refer below from the example `ClassicFED3`:

```
fed3.LoRaTransmit = true; //set 'true' to send data to network, if using BNC set 'false'
```

**NOTE:** The application can either use BNC (or) LoRa feature at a time.

### 8. Start coding!
Check out the [functions](https://github.com/KravitzLabDevices/FED3_library/wiki/3.-Functions) and [variables](https://github.com/KravitzLabDevices/FED3_library/wiki/2.-Variables) of the FED3 library to start making custom programs.
69 changes: 46 additions & 23 deletions src/FED3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@
#include "Arduino.h"
#include "FED3.h"

#include "FED3WAN.h"

// Start FED3 and RTC objects
FED3 *pointerToFED3;
RTC_PCF8523 rtc;
RTC_PCF8523 rtc;

// Start the FED3WAN object
FED3WAN fed3wan;

// Interrupt handlers
static void outsidePelletTriggerHandler(void) {
Expand Down Expand Up @@ -84,6 +89,9 @@ void FED3::logLeftPoke(){
else{
Event = "Left";
}
if (LoRaTransmit)
fed3wan.run(pointerToFED3); //Tx data via uart

logdata();
Left = false;
}
Expand All @@ -105,6 +113,9 @@ void FED3::logRightPoke(){
else{
Event = "Right";
}
if (LoRaTransmit)
fed3wan.run(pointerToFED3); //Tx data via uart

logdata();
Right = false;
}
Expand Down Expand Up @@ -232,7 +243,10 @@ void FED3::Feed(int pulse, bool pixelsoff) {
DateTime now = rtc.now();
interPelletInterval = now.unixtime() - lastPellet; //calculate time in seconds since last pellet logged
lastPellet = now.unixtime();


if (LoRaTransmit)
fed3wan.run(pointerToFED3); //Tx data via uart

logdata();
numMotorTurns = 0; //reset numMotorTurns
PelletAvailable = true;
Expand Down Expand Up @@ -541,10 +555,10 @@ void FED3::Blink(byte PIN, byte DELAY_MS, byte loops) {
//Simple function for sending square wave pulses to the BNC port
void FED3::BNC(int DELAY_MS, int loops) {
for (int i = 0; i < loops; i++) {
digitalWrite(BNC_OUT, HIGH);
digitalWrite(BNC_Out, HIGH);
digitalWrite(GREEN_LED, HIGH);
delay(DELAY_MS);
digitalWrite(BNC_OUT, LOW);
digitalWrite(BNC_Out, LOW);
digitalWrite(GREEN_LED, LOW);
delay(DELAY_MS);
}
Expand All @@ -553,36 +567,35 @@ void FED3::BNC(int DELAY_MS, int loops) {
//More advanced function for controlling pulse width and frequency for the BNC port
void FED3::pulseGenerator(int pulse_width, int frequency, int repetitions){ // freq in Hz, width in ms, loops in number of times
for (byte j = 0; j < repetitions; j++) {
digitalWrite(BNC_OUT, HIGH);
digitalWrite(BNC_Out, HIGH);
digitalWrite(GREEN_LED, HIGH);
delay(pulse_width); //pulse high for width
digitalWrite(BNC_OUT, LOW);
digitalWrite(BNC_Out, LOW);
digitalWrite(GREEN_LED, LOW);
long temp_delay = (1000 / frequency) - pulse_width;
if (temp_delay < 0) temp_delay = 0; //if temp delay <0 because parameters are set wrong, set it to 0 so FED3 doesn't crash O_o
delay(temp_delay); //pin low
delay(temp_delay); //pin low
}
}

void FED3::ReadBNC(bool blinkGreen){
pinMode(BNC_OUT, INPUT_PULLDOWN);
pinMode(BNC_Out, INPUT_PULLDOWN);
BNCinput=false;
if (digitalRead(BNC_OUT) == HIGH)
{
delay (1);
if (digitalRead(BNC_OUT) == HIGH)
{
if (blinkGreen == true)
{
digitalWrite(GREEN_LED, HIGH);
delay (25);
digitalWrite(GREEN_LED, LOW);
}
BNCinput=true;
}
if (digitalRead(BNC_Out) == HIGH)
{
delay (1);
if (digitalRead(BNC_Out) == HIGH)
{
if (blinkGreen == true)
{
digitalWrite(GREEN_LED, HIGH);
delay (25);
digitalWrite(GREEN_LED, LOW);
}
BNCinput=true;
}
}
}


/**************************************************************************************************************************************************
Display functions
Expand Down Expand Up @@ -1466,6 +1479,9 @@ void FED3::ReleaseMotor () {
/**************************************************************************************************************************************************
Startup Functions
**************************************************************************************************************************************************/
//Constructor
FED3::FED3(void) {};

//Import Sketch variable from the Arduino script
FED3::FED3(String sketch) {
sessiontype = sketch;
Expand Down Expand Up @@ -1495,7 +1511,14 @@ void FED3::begin() {
pinMode(A3, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);
pinMode(BNC_OUT, OUTPUT);

if (LoRaTransmit)
fed3wan.begin();
else
{
BNC_Out = A0;
pinMode(BNC_Out, OUTPUT);
}

// Initialize RTC
rtc.begin();
Expand Down
10 changes: 6 additions & 4 deletions src/FED3.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This device includes hardware and code from:
Copyright (c) 2019, 2020 Lex Kravitz
*/

#define VER "1.16.3"
#define VER "1.17.0"

#ifndef FED3_H
#define FED3_H
Expand Down Expand Up @@ -56,7 +56,6 @@ This device includes hardware and code from:
#define BUZZER 0
#define VBATPIN A7
#define cardSelect 4
#define BNC_OUT A0
#define SHARP_SCK 12
#define SHARP_MOSI 11
#define SHARP_SS 10
Expand All @@ -70,6 +69,7 @@ extern bool Left;
class FED3 {
// Members
public:
FED3(void);
FED3(String sketch);
String sketch = "undef";
String sessiontype = "undef";
Expand Down Expand Up @@ -130,9 +130,10 @@ class FED3 {
void StartScreen();
void FED3MenuScreen();
void SetClock();

//BNC input/output
void ReadBNC(bool blinkGreen);
uint8_t BNC_Out;
void ReadBNC(bool blinkGreen);
bool BNCinput = false;

// Motor
Expand Down Expand Up @@ -223,6 +224,7 @@ class FED3 {
bool ClassicFED3 = false;
bool FED3Menu = false;
bool tempSensor = false;
bool LoRaTransmit = false;

int EndTime = 0;
int ratio = 1;
Expand Down
Loading

0 comments on commit 9dd00c1

Please sign in to comment.