Xkc-y25-npn and state machine

I have been trying to gat this sensor to work in my projekt.
Its a water boiler that when a press a switch, it checks the water level, if low it turns on a relay fort water inlet until water level is high, than it turns on biler relay if temperature is under 93C and turns it of when 93C.

Everything works great accept that the water level in in "reverse". When the water in low it goes to boiling state and it water level is high it turns on the water inlet relay.
It shows temp on a display in real time.

Anyone who can look att my code and see what's wrong?
(There are a lot of other things in this servo but the code for that isn't in the sketch yet.

#include <Servo.h>
#include <AccelStepper.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Pin Definitions

#define TEMPERATURE_PIN 6     // Temperatur sensor
#define BOILER_PIN 8          // Relay for boiler
#define INLET_PIN 9           //Relay for inlet valve
#define ONE_WIRE_BUS 6        // ONE_WIRE_BUS
#define dirPin 11             // Stepper motor pin
#define stepPin 12            // Stepper motor pin
#define enablePin 7           // Stepper motor pin
#define motorInterfaceType 1  // Stepper motor pin
#define SCREEN_WIDTH 96       // OLED display width, in pixels
#define SCREEN_HEIGHT 16      // OLED display height, in pixels
#define OLED_RESET 4          // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C   ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
// AccelStepper for steper motor
AccelStepper stepper(motorInterfaceType, stepPin, dirPin);

// State Definitions
enum State {
  WAIT_FOR_SWITCH_PRESS,
  CHECK_WATER_LEVEL,
  WATER_LEVEL_LOW,
  WATER_LEVEL_HIGH,
  CHECK_TEMPERATURE,
  TEMPERATURE_BELOW_THRESHOLD,
  TEMPERATURE_ABOVE_THRESHOLD
};

Servo myservo;
const int SWITCH_PIN = 5;            // set switch input pin
const int WATER_LEVEL_PIN = 10;      // set water level input pin
int switchState = HIGH;              // initial state of switch
int lastSwitchState = HIGH;          // last state of switch
unsigned long lastDebounceTime = 0;  // last time switch state changed
unsigned long debounceDelay = 35;    // debounce delay in milliseconds
// Global Variables
State currentState = WAIT_FOR_SWITCH_PRESS;
int waterLevel = 0;


void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  // Start serial communication for debugging purposes
  Serial.begin(9600);  // initialize the Serial Monitor at a baud rate of 9600
  // Start up the library
  sensors.begin();    // initialize the DS18B20 temperature sensor:
  myservo.attach(4);  // attach servo to pin 4

  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(WATER_LEVEL_PIN, INPUT_PULLUP);
  pinMode(TEMPERATURE_PIN, INPUT);
  pinMode(BOILER_PIN, OUTPUT);
  pinMode(INLET_PIN, OUTPUT);

  myservo.write(34);                 // move servo to 34 degrees
  stepper.setMaxSpeed(600.0);        // Set maximum speed in steps per second
  stepper.setAcceleration(10000.0);  // Set acceleration in steps per second per second
  pinMode(enablePin, OUTPUT);        // Set enable pin as output
  digitalWrite(enablePin, LOW);      // Enable the stepper motor
}

void loop() {
  // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
  sensors.requestTemperatures();
  Serial.print("Celsius temperature: ");
  // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
  Serial.println(sensors.getTempCByIndex(0));  // get and print the temperature in degree Celsius

  float T = sensors.getTempCByIndex(0);  // let T be temperature in degC from sensor
                                         // floating-point number, with a decimal point
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Temperature: ");  // display temperature in deg Celsius
  display.print(T);
  display.print(" ");
  display.cp437(true);  // code page 437
  display.write(167);   // character 167 is degree
  display.println("C");
  display.display();  // for the changes to make effect
  display.clearDisplay();


  switch (currentState) {
    case WAIT_FOR_SWITCH_PRESS:
      if (digitalRead(SWITCH_PIN) == LOW) {
        currentState = CHECK_WATER_LEVEL;
      }
      break;

    case CHECK_WATER_LEVEL:
      waterLevel = digitalRead(WATER_LEVEL_PIN);
      if (waterLevel == LOW) {
        currentState = WATER_LEVEL_LOW;
      } else {
        currentState = WATER_LEVEL_HIGH;
      }
      break;

    case WATER_LEVEL_LOW:
      digitalWrite(INLET_PIN, HIGH);
      waterLevel = digitalRead(WATER_LEVEL_PIN);
      if (waterLevel == HIGH) {
        digitalWrite(INLET_PIN, LOW);
        currentState = CHECK_TEMPERATURE;
      }
      break;

    case WATER_LEVEL_HIGH:
      currentState = CHECK_TEMPERATURE;
      break;

    case CHECK_TEMPERATURE:
      if (T < 93) {
        currentState = TEMPERATURE_BELOW_THRESHOLD;
      } else {
        currentState = TEMPERATURE_ABOVE_THRESHOLD;
      }
      break;

    case TEMPERATURE_BELOW_THRESHOLD:
      digitalWrite(BOILER_PIN, HIGH);
      if (T > 93) {
        digitalWrite(BOILER_PIN, LOW);
        currentState = WAIT_FOR_SWITCH_PRESS;
      }
      break;

    case TEMPERATURE_ABOVE_THRESHOLD:
      currentState = WAIT_FOR_SWITCH_PRESS;
      break;
  }
}

Okay, I found the solution to my problem. If I connect the forth wire, black, to ground the sensor works as intended and the code works. :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.