Small CMS

Ja, som det kan ses af nedenstående, så skriver jeg nogle gange på Engelsk.
Teksten til højre for // er en kort beskrivelse af hvad hver linje gør.

/*
 * Small basic CMS
 *
 * 8 Digital Outputs
 * 4 Digital Inputs
 * 1 Temperatur 0-100°C
 * 2 analoge 0-5000mV
 *
 * D0 1 equal to DI 1
 * D0 2 equal to DI 2
 * D0 3 equal to DI 3
 * D0 4 equal to DI 4
 * D0 5 for Output when A1 comes over setpoint
 * D0 6 for Output when A2 comes over setpoint
 * D0 7 for Output to heater when the surrounding temperatur comes below setpoint
 * D0 8 for Output to ventilator when the surrounding temperatur comes over setpoint
 *
 * DI 1 for simulating normal High input
 * DI 2 for simulating normal Low input
 * DI 3 for simulating normal Low input
 * DI 4 for simulating normal Low input
 *
 * Temp for controlling surrounding temperature for the electronic
 *
 * A1 for simulating 0-5000mV
 * A2 for simulating 0-5000mV 
 *
 */

const int DO1 = 2;  // Arduino Uno board pin 2 configuration as Digital Output 1
const int DO2 = 3;  // Arduino Uno board pin 3 configuration as Digital Output 2
const int DO3 = 4;  // Arduino Uno board pin 4 configuration as Digital Output 3
const int DO4 = 5;  // Arduino Uno board pin 5 configuration as Digital Output 4
const int DO5 = 6;  // Arduino Uno board pin 6 configuration as Digital Output 5
const int DO6 = 7;  // Arduino Uno board pin 7 configuration as Digital Output 6
const int DO7 = 8;  // Arduino Uno board pin 8 configuration as Digital Output 7
const int DO8 = 9;  // Arduino Uno board pin 9 configuration as Digital Output 8

const int DI1 = 10;  // Arduino Uno board pin 10 configuration as Digital Input 1
const int DI2 = 11;  // Arduino Uno board pin 11 configuration as Digital Input 2
const int DI3 = 12;  // Arduino Uno board pin 12 configuration as Digital Input 3
const int DI4 = 13;  // Arduino Uno board pin 13 configuration as Digital Input 4

int DI1state = 0;  // Variable for reading the state of Digital Input 1
int DI2state = 0;  // Variable for reading the state of Digital Input 2
int DI3state = 0;  // Variable for reading the state of Digital Input 3
int DI4state = 0;  // Variable for reading the state of Digital Input 4

int analogValue0 = 0;  // Variable for analog value on A0
int outputValue0 = 0;  // Making mapping possible for A0
int analogValue1 = 0;  // Variable for analog value on A1
int outputValue1 = 0;  // Making mapping possible for A1
int analogValue2 = 0;  // Variable for analog value on A2
int outputValue2 = 0;  // Making mapping possible for A2
int HeatSet = 10;      // Setpoint in °C for heater
int HeatHystUp = 1;    // 
int HeatHystDown = 1;  // 
int VentSet = 30;      // Setpoint in °C for ventilator
int VentHystUp = 1;    // 
int VentHystDown = 1;  // 
int A1set = 100;       // A1 hysteresis set point 100mV
int hystA1up = 10;     // A1 hysteresis up trigger at 110mV
int hystA1down = 10;   // A1 hysteresis down trigger at 90mV
int A2set = 1000;      // A2 hysteresis set point 1000mV
int hystA2up = 20;     // A2 hysteresis up trigger at 1020mV
int hystA2down = 0;    // A2 hysteresis down trigger at 1000mV

void setup()  // Run once, when the sketch starts
{
  Serial.begin(9600);  // Setting communication up at 9600 bps on TX pin at Arduino Uno

  Serial.write(0xFE);  // Special Command Extended
  Serial.write(0x01);  // Clear Display
  delay(10);  // Delay for clearing the Display

  Serial.write(0x7C);  // Special Command
  Serial.write(150);  // Backlight brightness level (128 = Off, 140 = 40%, 150 = 73%, 157 = Fully On)
  delay(10);  // Delay for changing Backlight brightness

  pinMode (DO1, OUTPUT);  // Digital Output 1 pin configuration
  pinMode (DO2, OUTPUT);  // Digital Output 2 pin configuration
  pinMode (DO3, OUTPUT);  // Digital Output 3 pin configuration
  pinMode (DO4, OUTPUT);  // Digital Output 4 pin configuration
  pinMode (DO5, OUTPUT);  // Digital Output 5 pin configuration
  pinMode (DO6, OUTPUT);  // Digital Output 6 pin configuration
  pinMode (DO7, OUTPUT);  // Digital Output 7 pin configuration
  pinMode (DO8, OUTPUT);  // Digital Output 8 pin configuration

  pinMode (DI1, INPUT);  // Digital Input 1 pin configuration
  pinMode (DI2, INPUT);  // Digital Input 2 pin configuration
  pinMode (DI3, INPUT);  // Digital Input 3 pin configuration
  pinMode (DI4, INPUT);  // Digital Input 4 pin configuration

  Serial.write(0xFE);  // Special Command
  Serial.write(0x80+0x00);  // Move to Row 1 Column 1
  Serial.write("Status:");  // Pretext for which status the CMS are in

  Serial.write(0xFE);  // Special Command
  Serial.write(0x80+0x40);  // Move to Row 2 Column 1
  Serial.write("DO 1 2 3 4 5 6 7 8");  // Pretext for Digital Outputs

  Serial.write(0xFE);  // Special Command
  Serial.write(0x80+0x14);  // Move to Row 3 Column 1
  Serial.write("DI 1 2 3 4");  // Pretext for Digital Inputs

  Serial.write(0xFE);  // Special Command
  Serial.write(0x80+0x20);  // Move to Row 3 Column 13
  Serial.write("Temp");  // Pretext for Temperature
  Serial.write(0xFE);  // Special Command
  Serial.write(0x80+0x26);  // Move to Row 3 Column 19
  Serial.write(0xDF);  // Symbol °
  Serial.print("C");  // Temp Scale Centigrade

  Serial.write(0xFE);  // Special Command
  Serial.write(0x80+0x54);  // Move to Row 4 Column 1
  Serial.write("A1 mV");  // Pretext for Analog In 1

  Serial.write(0xFE);  // Special Command
  Serial.write(0x80+0x5F);  // Move to Row 4 Column 12
  Serial.write("A2 mV");  // Pretext for Analog In 2
}

void loop()  // Run over and over again
{
  DI1state = digitalRead(DI1);  // Reading which state Digital Input 1 are in
    if (DI1state == HIGH)  //
    {
      digitalWrite(DO1, HIGH);  // Setting Digital Output 1 High
      Serial.write(0xFE);  // Special Command Extended
      Serial.write(0x80+0x18); // Move to Row 3 Column 5
      Serial.print("H");  // Writing H for High
      Serial.write(0xFE);  // Special Command Extended
      Serial.write(0x80+0x44); // Move to Row 2 Column 5
      Serial.print("H");  // Writing H for High
      Serial.write(0xFE);  // Special Command Extended
      Serial.write(0x80+0x08); // Move to Row 1 Column 9
      Serial.print("Drift OK");  // Writing Drift OK as status
    }
    else
    {
      digitalWrite(DO1, LOW);  // Setting Digital Output 1 Low
      Serial.write(0xFE);  // Special Command Extended
      Serial.write(0x80+0x18); // Move to Row 3 Column 5
      Serial.print("L");  // Writing L for Low
      Serial.write(0xFE);  // Special Command Extended
      Serial.write(0x80+0x44); // Move to Row 2 Column 5
      Serial.print("L");  // Writing L for Low
      Serial.write(0xFE);  // Special Command Extended
      Serial.write(0x80+0x08); // Move to Row 1 Column 9
      Serial.print("Stoppet ");  // Writing Stoppet as status
    }

  DI2state = digitalRead(DI2);  // Reading which state Digital Input 2 are in
    if (DI2state == HIGH)  //
    {
      digitalWrite(DO2, HIGH);  // Setting Digital Output 2 High
      Serial.write(0xFE);  // Special Command Extended
      Serial.write(0x80+0x1A); // Move to Row 3 Column 7
      Serial.print("H");  // Writing H for High
      Serial.write(0xFE);  // Special Command Extended
      Serial.write(0x80+0x46); // Move to Row 2 Column 7
      Serial.print("H");  // Writing H for High
    }
    else
    {
      digitalWrite(DO2, LOW);  //Setting Digital Output 2 Low
      Serial.write(0xFE);  // Special Command Extended
      Serial.write(0x80+0x1A); // Move to Row 3 Column 7
      Serial.print("L");  // Writing L for Low
      Serial.write(0xFE);  // Special Command Extended
      Serial.write(0x80+0x46); // Move to Row 2 Column 7
      Serial.print("L");  // Writing L for Low
    }

  DI3state = digitalRead(DI3);  // Reading which state Digital Input 3 are in
    if (DI3state == HIGH)  //
    {
      digitalWrite(DO3, HIGH);  // Setting Digital Output 3 High
      Serial.write(0xFE);  // Special Command Extended
      Serial.write(0x80+0x1C); // Move to Row 3 Column 9
      Serial.print("H");  // Writing H for High
      Serial.write(0xFE);  // Special Command Extended
      Serial.write(0x80+0x48); // Move to Row 2 Column 9
      Serial.print("H");  // Writing H for High
    }
    else
    {
      digitalWrite(DO3, LOW);  // Setting Digital Output 3 Low
      Serial.write(0xFE);  // Special Command Extended
      Serial.write(0x80+0x1C); // Move to Row 3 Column 9
      Serial.print("L");  // Writing L for Low
      Serial.write(0xFE);  // Special Command Extended
      Serial.write(0x80+0x48); // Move to Row 2 Column 9
      Serial.print("L");  // Writing L for Low
    }    

  DI4state = digitalRead(DI4);  // Reading which state Digital Input 4 are in
    if (DI4state == HIGH)  //
    {
      digitalWrite(DO4, HIGH);  // Setting Digital Output 4 High
      Serial.write(0xFE);  // Special Command Extended
      Serial.write(0x80+0x1E); // Move to Row 3 Column 11
      Serial.print("H");  // Writing H for High
      Serial.write(0xFE);  // Special Command Extended
      Serial.write(0x80+0x4A); // Move to Row 2 Column 11
      Serial.print("H");  // Writing H for High
    }
    else
    {
      digitalWrite(DO4, LOW);  // Setting Digital Output 3 Low
      Serial.write(0xFE);  // Special Command Extended
      Serial.write(0x80+0x1E); // Move to Row 3 Column 11
      Serial.print("L");  // Writing L for Low
      Serial.write(0xFE);  // Special Command Extended
      Serial.write(0x80+0x4A); // Move to Row 2 Column 11
      Serial.print("L");  // Writing L for Low
    }

  analogValue0 = analogRead(0);  // Reading of the analog input pin A0
  outputValue0 = map(analogValue0, 0, 1023, 0, 500); // 10mV pr. °C (5V = 500°C)
  if (outputValue0>(HeatSet+HeatHystUp))  // Higher than upper trigger point?
  {
    digitalWrite(DO7, LOW);  // Setting Digital Output 5 Low
    Serial.write(0xFE);  // Special Command Extended
    Serial.write(0x80+0x50); // Move to Row 2 Column 17
    Serial.print("L");  // Writing L for Low 
  }
  if (outputValue0<(HeatSet-HeatHystDown))  // Lower than lowest trigger point?
  {
    digitalWrite(DO7, HIGH);  // Setting Digital Output 5 High
    Serial.write(0xFE);  // Special Command Extended
    Serial.write(0x80+0x50); // Move to Row 2 Column 17
    Serial.print("H");  // Writing H for High
  }
  if (outputValue0>(VentSet+VentHystUp))  // Higher than upper trigger point?
  {
    digitalWrite(DO8, HIGH);  // Setting Digital Output 5 High
    Serial.write(0xFE);  // Special Command Extended
    Serial.write(0x80+0x52); // Move to Row 2 Column 19
    Serial.print("H");  // Writing H for High 
  }
  if (outputValue0<(VentSet-VentHystDown))  // Lower than lowest trigger point?
  {
    digitalWrite(DO8, LOW);  // Setting Digital Output 5 Low
    Serial.write(0xFE);  // Special Command Extended
    Serial.write(0x80+0x52); // Move to Row 2 Column 19
    Serial.print("L");  // Writing L for Low 
  }
  Serial.write(0xFE);  // Special Command Extended
  Serial.write(0x80+0x24); // Move to Row 3 Column 17
    if (outputValue0 > 10)  // Correct placement of 10's
    {
      Serial.print(outputValue0);  // * * * Temp value * * *
    }
    else  // Correct placement of 1's
    {
      Serial.write(" "); // Writing "space" for blanking the 10's
      Serial.print(outputValue0);  // * * * Temp value * * *
    }

  analogValue1 = analogRead(1);  // Reading of the analog input pin A1
  outputValue1 = map(analogValue1, 0, 1023, 0, 5000); // Read out in mV
  if (outputValue1>(A1set+hystA1up))  // Higher than upper trigger point?
  {
    digitalWrite(DO5, HIGH);  // Setting Digital Output 5 High
    Serial.write(0xFE);  // Special Command Extended
    Serial.write(0x80+0x4C); // Move to Row 2 Column 13
    Serial.print("H");  // Writing H for High 
  }
  if (outputValue1<(A1set-hystA1down))  // Lower than lowest trigger point?
  {
    digitalWrite(DO5, LOW);  // Setting Digital Output 5 Low
    Serial.write(0xFE);  // Special Command Extended
    Serial.write(0x80+0x4C); // Move to Row 2 Column 13
    Serial.print("L");  // Writing L for Low
  }
  Serial.write(0xFE);  // Special Command Extended
  Serial.write(0x80+0x57); // Move to Row 4 Column 4
    if (outputValue1 < 1000)  // Correct placement of 100's
    {
      Serial.write(" "); // Writing "space" for blanking the 1000's
    }
    if (outputValue1 < 100)  // Correct placement of 10's
    {
      Serial.write(" "); // Writing 2 "space" for blanking the 100's
    }  
    if (outputValue1 < 10)  // Correct placement of 1's
    {
      Serial.write(" "); // Writing 3 "space" for blanking the 10's
    }
  Serial.print(outputValue1);  // * * * A1 value * * *

  analogValue2 = analogRead(2);  // Reading of the analog input pin A2 
  outputValue2 = map(analogValue2, 0, 1023, 0, 5000); // Read out in mV
  if (outputValue2>(A2set+hystA2up))  // Higher than upper trigger point?
  {
    digitalWrite(DO6, HIGH);  // Setting Digital Output 6 High
    Serial.write(0xFE);  // Special Command Extended
    Serial.write(0x80+0x4E); // Move to Row 2 Column 15
    Serial.print("H");  // Writing H for High 
  }
  if (outputValue2<(A2set-hystA2down))  // Lower than lowest trigger point?
  {
    digitalWrite(DO6, LOW);  // Setting Digital Output 6 Low
    Serial.write(0xFE);  // Special Command Extended
    Serial.write(0x80+0x4E); // Move to Row 2 Column 15
    Serial.print("L");  // Writing L for Low
  }
  Serial.write(0xFE);  // Special Command Extended
  Serial.write(0x80+0x62); // Move to Row 4 Column 15 
    if (outputValue2 < 1000)  // Correct placement of 100's
    {
      Serial.write(" "); // Writing "space" for blanking the 1000's
    }
    if (outputValue2 < 100)  // Correct placement of 10's
    {
      Serial.write(" "); // Writing 2 "space" for blanking the 100's
    }  
    if (outputValue2 < 10)  // Correct placement of 1's
    {
      Serial.write(" "); // Writing 3 "space" for blanking the 10's
    }  
  Serial.print(outputValue2);  // * * * A2 value * * *

  delay(100); // Time for Display read out 
}

Den opmærksomme læser vil nok bemærke, at der ingen midling er af de analoge værdier.
Der er sikret i koden, at de analoge værdier højre stilles.
Der er ingen selvhold funktion programmeret ind når status skifter fra “Drift OK” til “Stoppet”.