<commit_before>/********************************************

 This is an oven control sketch for the Adafruit 1.8" TFT
 shield with joystick stacked on a PSAS Airframes Team 
 Oven Control Shield v3 stacked on an Arduino Uno.
 
 Our LCD shield is the BLACKTAB version.
 
 The display, SD card, and five MAX31855 thermocouple-
 to-digital chips are all on the SPI bus. The SD card 
 also uses the SD library. 
 
 We referenced code written by <NAME>/Ladyada
 for Adafruit Industries under the MIT license.
 
 This is oven control shield code v3 written by 
 <NAME> and <NAME>, 2014. 
 http://github.com/wicker/oven-controller/
 
 *******************************************/
  
#include <Adafruit_MAX31855.h>
#include <Adafruit_ST7735.h>
#include <Adafruit_GFX.h>
#include <SD.h>
#include <SPI.h>

#define OFF 0
#define ON  1

// assign joystick positions
#define BUTTON_NONE 0
#define BUTTON_DOWN 1
#define BUTTON_RIGHT 2
#define BUTTON_SELECT 3
#define BUTTON_UP 4
#define BUTTON_LEFT 5

// Pins connection
int swReset       = 0;
int thermoCS1     = 1;
int thermoCS2     = 2;
int thermoCS3     = 3;
int sdCS          = 4;
int heater1       = 5;
int heater2       = 6;
int thermoCS4     = 7;
int lcdDC         = 8;
int thermoCS5     = 9;
int lcdCS         = 10;
int spiMOSI       = 11;
int spiMISO       = 12;
int spiCLK        = 13;
int swStart       = A0;
int fan           = A1;
int vent          = A2;
int joyRead       = A3;

// initialize the LCD screen
Adafruit_ST7735 tft = Adafruit_ST7735(lcdCS, lcdDC, swReset);

// Indicate status variables: 0 off; 1: on
int start      =  OFF;
int stopnow    =  ON;

// shouldn't these be #defines or at least const double, const int, etc?
double tempSet         =  350;        // farenheit
double tempInc         =  3 ;         // Temperature increament per minutes
double tempErrRange    =  0;
double tempThreshold   =  3;          // Temperature limit to turn on both heaters
int ADCThreshold       =  900;
int timeDelay          =  5000;
float timeMax          =  12780;      // (sec) timeMax=time_ramping+time_at_350; (set at 2 hours), initial temp: 70*F

// calibration 
double sensor1  =  0;
double sensor2  =  -4;
double sensor3  =  0;
double sensor4  =  0;
double sensor5  =  0;

// variables
double tempErr;
double tempSensor1;
double tempSensor2;
double tempSensor3;
double tempSensor4;
double tempSensor5;
double tempAverage;
double tempInitial;         // room temp

double currTempSet;
double tempSetLow;
double tempSetHigh;

double startTime;
double currTime;

uint8_t buttonhistory  =  0;

// Thermocouples
Adafruit_MAX31855 thermocouple1(spiCLK, thermoCS1, spiMISO);
Adafruit_MAX31855 thermocouple2(spiCLK, thermoCS2, spiMISO);
Adafruit_MAX31855 thermocouple3(spiCLK, thermoCS3, spiMISO);
Adafruit_MAX31855 thermocouple4(spiCLK, thermoCS4, spiMISO);
Adafruit_MAX31855 thermocouple5(spiCLK, thermoCS5, spiMISO);

void all_heaters_off()
{
  digitalWrite(heater1, LOW);  // turn off the heaters
  delay(100);
  digitalWrite(heater2, LOW);  // turn off the heaters
  Serial.println("  OFF");  
}

void one_heater_off()
{
  digitalWrite(heater1, LOW);  // turn off the heaters
  delay(100);
  Serial.println("  1 OFF");  
}

void all_heaters_on()
{
  digitalWrite(heater1, HIGH);  // turn on the heaters
  delay(100);
  digitalWrite(heater2, HIGH);  // turn on the heaters
  delay(100);
  Serial.println("  ON");  
}
 
void one_heater_on()
{
  digitalWrite(heater1, HIGH);  // turn on the heaters
  delay(100);
  Serial.println("  1 ON");  
} 
 
uint8_t readButton(void) {
  float a = analogRead(3);
  
  a *= 5.0;
  a /= 1024.0;
  
  Serial.print("Button read analog = ");
  Serial.println(a);
  if (a < 0.2) return BUTTON_DOWN;
  if (a < 1.0) return BUTTON_RIGHT;
  if (a < 1.5) return BUTTON_SELECT;
  if (a < 2.0) return BUTTON_UP;
  if (a < 3.2) return BUTTON_LEFT;
  else return BUTTON_NONE;
}

int scan_button(int swButton)
{
  int ADCreading = analogRead(swButton);
//  Serial.println(ADCreading);
  if (ADCreading < ADCThreshold) 
         return (1);
  else   return (0);
}

void tempPrint()
{
  Serial.print("Current temperature F = ");
 // Serial.println(tempSensor1,"\t", tempSensor2,"\t",tempSensor3,"\t",tempSensor4,"\t",tempSensor5);
  Serial.print(tempSensor1);
  Serial.print(" ");
  Serial.print(tempSensor2);
  Serial.print(" ");
  Serial.print(tempSensor3);
  Serial.print(" ");
  Serial.print(tempSensor4);
  Serial.print(" ");
  Serial.print(tempSensor5); 
  Serial.print("Average temperature F = ");
  Serial.println(tempAverage);
}

// Poll each thermocouple value and get an average value.
// Adjusting by 32 for each tempSensor value
// eliminates the SPI disconnection.
void check_temp_sensors()
{
  float tempSum=0;
  int count=0;
  tempSensor1 = thermocouple1.readFarenheit()+sensor1;
  if (tempSensor1>32) {
    count=count+1;
    tempSum=tempSum+tempSensor1;
  }
  delay(10);
  tempSensor2 = thermocouple2.readFarenheit()+sensor2;
  if (tempSensor2>32) {
    count=count+1;
    tempSum=tempSum+tempSensor2;
  }
  delay(10);
  tempSensor3 = thermocouple3.readFarenheit()+sensor3;
  if (tempSensor3>32) {
    count=count+1;
    tempSum=tempSum+tempSensor3;
  }
  delay(10);
  tempSensor4 = thermocouple4.readFarenheit()+sensor4;
  if (tempSensor4>32) {
    count=count+1;
    tempSum=tempSum+tempSensor4;
  }
  delay(10);<commit_msg>Wrote handle_joystick() and handle_start_button() as separate functions where start button updates state.<commit_after>/********************************************

 This is oven control shield code v3 written by 
 <NAME> and <NAME>, 2014. 
 http://github.com/wicker/oven-controller/
 
 This is an oven control sketch for the Adafruit 1.8" TFT
 shield with joystick stacked on a PSAS Airframes Team 
 Oven Control Shield v3 stacked on an Arduino Uno.
 
 Our LCD shield is the BLACKTAB version.
 
 The display, SD card, and five MAX31855 thermocouple-
 to-digital chips are all on the SPI bus. The SD card 
 also uses the SD library. 
 
 We referenced code written by <NAME>/Ladyada
 for Adafruit Industries under the MIT license.
 
 *******************************************/
  
#include <Adafruit_MAX31855.h>
#include <Adafruit_ST7735.h>
#include <Adafruit_GFX.h>
#include <SD.h>
#include <SPI.h>

#define OFF 0
#define ON  1

// assign state values
#define START   1
#define RUNNING 2
#define STOP    3
#define STOPPED 4

// assign joystick positions
#define BUTTON_NONE 0
#define BUTTON_DOWN 1
#define BUTTON_RIGHT 2
#define BUTTON_SELECT 3
#define BUTTON_UP 4
#define BUTTON_LEFT 5

// Pins connection
int swReset       = 0;
int thermoCS1     = 1;
int thermoCS2     = 2;
int thermoCS3     = 3;
int sdCS          = 4;
int heater1       = 5;
int heater2       = 6;
int thermoCS4     = 7;
int lcdDC         = 8;
int thermoCS5     = 9;
int lcdCS         = 10;
int spiMOSI       = 11;
int spiMISO       = 12;
int spiCLK        = 13;
int swStart       = A0;
int fan           = A1;
int vent          = A2;
int joyRead       = A3;

// initialize the LCD screen
Adafruit_ST7735 tft = Adafruit_ST7735(lcdCS, lcdDC, swReset);

// Indicate status variables: 0 off; 1: on
int start      =  OFF;
int stopnow    =  ON;

// shouldn't these be #defines or at least const double, const int, etc?
double tempSet         =  350;        // farenheit
double tempInc         =  3 ;         // Temperature increament per minutes
double tempErrRange    =  0;
double tempThreshold   =  3;          // Temperature limit to turn on both heaters
int ADCThreshold       =  900;
int timeDelay          =  5000;
float timeMax          =  12780;      // (sec) timeMax=time_ramping+time_at_350; (set at 2 hours), initial temp: 70*F

// calibration 
double sensor1  =  0;
double sensor2  =  -4;
double sensor3  =  0;
double sensor4  =  0;
double sensor5  =  0;

// variables
double tempErr;
double tempSensor1;
double tempSensor2;
double tempSensor3;
double tempSensor4;
double tempSensor5;
double tempAverage;
double tempInitial;         // room temp

double currTempSet;
double tempSetLow;
double tempSetHigh;

double startTime;
double currTime;

uint8_t buttonhistory  =  0;
uint8_t joystickVal = 0;

// Thermocouples
Adafruit_MAX31855 thermocouple1(spiCLK, thermoCS1, spiMISO);
Adafruit_MAX31855 thermocouple2(spiCLK, thermoCS2, spiMISO);
Adafruit_MAX31855 thermocouple3(spiCLK, thermoCS3, spiMISO);
Adafruit_MAX31855 thermocouple4(spiCLK, thermoCS4, spiMISO);
Adafruit_MAX31855 thermocouple5(spiCLK, thermoCS5, spiMISO);

void all_heaters_off()
{
  digitalWrite(heater1, LOW);  // turn off the heaters
  delay(100);
  digitalWrite(heater2, LOW);  // turn off the heaters
  Serial.println("  OFF");  
}

void one_heater_off()
{
  digitalWrite(heater1, LOW);  // turn off the heaters
  delay(100);
  Serial.println("  1 OFF");  
}

void all_heaters_on()
{
  digitalWrite(heater1, HIGH);  // turn on the heaters
  delay(100);
  digitalWrite(heater2, HIGH);  // turn on the heaters
  delay(100);
  Serial.println("  ON");  
}
 
void one_heater_on()
{
  digitalWrite(heater1, HIGH);  // turn on the heaters
  delay(100);
  Serial.println("  1 ON");  
} 
 
void tempPrint()
{
  Serial.print("Current temperature F = ");
 // Serial.println(tempSensor1,"\t", tempSensor2,"\t",tempSensor3,"\t",tempSensor4,"\t",tempSensor5);
  Serial.print(tempSensor1);
  Serial.print(" ");
  Serial.print(tempSensor2);
  Serial.print(" ");
  Serial.print(tempSensor3);
  Serial.print(" ");
  Serial.print(tempSensor4);
  Serial.print(" ");
  Serial.print(tempSensor5); 
  Serial.print("Average temperature F = ");
  Serial.println(tempAverage);
}

// read the joystick and save the value
void handle_joystick() {

  float a = analogRead(3);

  a *= 5.0;
  a /= 1024.0;
  
  Serial.print("Button read analog = ");
  Serial.println(a);
  if (a < 0.2) joystickVal = BUTTON_DOWN;
  if (a < 1.0) joystickVal = BUTTON_RIGHT;
  if (a < 1.5) joystickVal = BUTTON_SELECT;
  if (a < 2.0) joystickVal = BUTTON_UP;
  if (a < 3.2) joystickVal = BUTTON_LEFT;
  else         joystickVal = BUTTON_NONE;
}

// Read the start button and update the state machine.
// Possible states are START, RUNNING, STOP, and STOPPED.
// The state is updated and the timer is incremented or
// reset to zero, depending on state.
void handle_start_button()

  uint8_t ADCReading < ADCThreshold);

  // if the input is HIGH, handle it as a START
  if (ADCReading > ADCThreshold) {
    if (status == START) {
      status = RUNNING;
      currTime = millis()-startTime;
    }
    else if (status == RUNNING) {
      status = RUNNING;
      currTime = millis()-startTime;
    }
    else if (status == STOP) {
      status = STOPPED;
      currTime = 0;
    }
    else { 
      status = START;
      currTime = 0;
    }
  }

  // otherwise the input is LOW, handle it as a STOP
  else {
    if (status == START) {
      status = STOP;
      currTime = millis()-startTime;
    }
    else if (status == RUNNING) {
      status = STOP;
      currTime = millis()-startTime;
    }
    else if (status == STOP) {
      status = STOPPED;
      currTime = 0;
    }
    else { 
      status = STOPPED;
      currTime = 0;
    }
  }
}

// Poll each thermocouple value and get an average value.
// Adjusting by 32 for each tempSensor value
// eliminates the SPI disconnection.
void check_temp_sensors()
{
  float tempSum=0;
  int count=0;
  tempSensor1 = thermocouple1.readFarenheit()+sensor1;
  if (tempSensor1>32) {
    count=count+1;
    tempSum=tempSum+tempSensor1;
  }
  delay(10);
  tempSensor2 = thermocouple2.readFarenheit()+sensor2;
  if (tempSensor2>32) {
    count=count+1;
    tempSum=tempSum+tempSensor2;
  }
  delay(10);
  tempSensor3 = thermocouple3.readFarenheit()+sensor3;
  if (tempSensor3>32) {
    count=count+1;
    tempSum=tempSum+tempSensor3;
  }
  delay(10);
  tempSensor4 = thermocouple4.readFarenheit()+sensor4;
  if (tempSensor4>32) {
    count=count+1;
    tempSum=tempSum+tempSensor4;
  }
  delay(10);
{'fraction_non_alphanumeric': 0.12060386269714093, 'fraction_numerical': 0.04166315256810323, 'mean_word_length': 3.245614035087719, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 28, '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': '7306726', 'n_tokens_mistral': 4529, 'n_tokens_neox': 4189, 'n_words': 1168}