summaryrefslogtreecommitdiff
path: root/Arduino/main.c/main.c.ino
blob: 8f651b9064e6e3c4285603ed8104db6d21de6019 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <Wire.h>

bool percentageLoad = false;
double VOLTAGE, P68, P150, PTotal;
double temp, currentLoad, load = 0;
uint8_t Q68, Q150;
bool new_message = false;

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.println("Enter Nominal System Voltage:");
  while (Serial.available() == 0) {;}
  VOLTAGE = Serial.parseFloat(SKIP_ALL);
  Serial.print("System Voltage: ");
  Serial.println(VOLTAGE, DEC);
  P150 = (VOLTAGE * VOLTAGE) / 150;
  P68 = (VOLTAGE * VOLTAGE) / 68;
  PTotal = P150+P68;

  String input = "null";
  Serial.println("Express load as a percentage? (y/n)");
  while ((Serial.available() == 0)||(input!="y")||(input!="n")) 
  {
    input = Serial.readString();
    input.trim();
  }
  if(input=="y")
  {
    percentageLoad=true;
    Serial.print("Enter Load as a percentage of ");
    Serial.println(PTotal, DEC);
  }
  else
  {
    percentageLoad=false;
    Serial.println("Enter Load in kW:");
  }
}

void loop()
{
  Serial.flush();
  if (Serial.available())
  {
    if(percentageLoad) temp = Serial.parseFloat(SKIP_ALL) * 1000;
    else temp = Serial.parseFloat(SKIP_ALL) * (PTotal/100.0);
  }

  if ((temp != load) && (temp > 0))
  {
    load = temp;
    if (VOLTAGE <= 420) //We don't want to use the 68ohm resistors if the voltage is higher than 420 as they would draw excessive power.
    {
      Q68 = load / P68;
      if (Q68 > 5) Q68 = 5;
      Q150 = (load - (Q68 * P68)) / P150;
    }
    else
    {
      Q150 = load / P150;
      Q68 = 0;
    }
    if (Q150 > 15) Q150 = 15;
    Serial.print("A combination of ");
    Serial.print(Q150, DEC);
    Serial.print(" 150ohm and ");
    Serial.print(Q68, DEC);
    Serial.print(" 68ohm resistors will be requested. ");
    //Serial.print(load - ((P150*Q150)+(P100*Q100)),DEC);
    Serial.print(((P150 * Q150) + (P68 * Q68)), DEC);
    Serial.println("W Accounted for.");
    new_message = true;
  }
  delay(50);
  Wire.beginTransmission(0x30); //Controller Board Has Address 0x07
  Wire.write(0x01); //First "EEPROM Address" stores resistor data.
  Wire.write((Q68 << 4) | Q150);
  Wire.endTransmission();


  if (new_message == true) {
    delay(25);
    Wire.requestFrom(0x30, 2);
    while (!Wire.available()) {;}
      int c = Wire.read();
      currentLoad = ((c & 0b00001111)*P150)+(((c & 0b01110000)>>4)*P68);
      if(currentLoad==load)
      {
        new_message=false;
        Serial.println("***Load Matched***");
      }
      /*
      Serial.print("Quantity of 150Ohm: ");
      Serial.print(c & 0b00001111); //Print Q150
      Serial.print(" Quantity of 68Ohm: ");
      Serial.print((c & 0b01110000)>>4);
      Serial.println(" was recieved.");
      */
  }
}