Quantcast
Channel: Arduino - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 747

How do I get controlP5 generated knob in Processing to change values in Arduino code?

$
0
0

Background: I am working on a electric brewing project using an Arduino Uno R3, two DS18b20 digital temp sensors and Processing IDE. I am using the sensors to monitor and control two separate heating elements on SSRs for various steps in beer brewing. I have successfully used the following Arduino and Processing code to send serial data to Processing to generate a dumb temp output GUI showing two separate temps (ACTUAL1,2), relay set points (SETPOINT1,2) and relay states (HEATPIN1,2).

Problem: The control knob generated with the controlP5 library is in the Processing sketch as you will see below but I am asking for help getting it to communicate back to Arduino. I may be going about this all wrong dataflow wise: Arduino to serial to Processing...back to serial? back to Arduino? I tried to scrap most of the Arduino code and use Firmata but I still need to code in the Dallas sensors into Arduino. Any suggestions would be greatly appreciated, very new to coding, I have the brewing process, hardware and wiring down but lack experience with code. Thank you in advance.

Arduino Code:

//#include <Boards.h>
//#include <Firmata.h>

#include <OneWire.h>
#include <DallasTemperature.h>
int ACTUAL1;
int ACTUAL2;
int SETPOINT1 = 145;
int SETPOINT2 = 150;
int HEATPIN1 = 13;
int HEATPIN2 = 12;
#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress Probe1 = { 0x28, 0xFF, 0x14, 0x23, 0x16, 0x15, 0x03, 0xB9 };
DeviceAddress Probe2 = { 0x28, 0xFF, 0xBA, 0x0D, 0x16, 0x15, 0x03, 0x4D };

void setup(void)
{
  pinMode(HEATPIN1, OUTPUT);
  pinMode(HEATPIN2, OUTPUT);
  Serial.begin(9600);
  sensors.begin();
  sensors.setResolution(Probe1, 10);
  sensors.setResolution(Probe2, 10);
}

void printTemperature(DeviceAddress deviceAddress)
{
  float TempC = sensors.getTempC(deviceAddress);
  if (TempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    ACTUAL1 = (DallasTemperature::toFahrenheit(TempC));
    ACTUAL2 = (DallasTemperature::toFahrenheit(TempC));
    Serial.print(ACTUAL1);
    //Serial.print(ACTUAL2);
  }
}

void loop(void)
{
  delay(2000);
  sensors.requestTemperatures();

  //Serial.read();
  //SETPOINT1 = Serial.parseInt();

  printTemperature(Probe1);
  if (ACTUAL1 < 100) {
    Serial.print(" ");
  }
  Serial.print(",");
  Serial.print(SETPOINT1);
  if (SETPOINT1 < 100) {
    Serial.print(" ");
  }
  if (ACTUAL1 >= SETPOINT1)
  {
    Serial.print ("OFF");
    digitalWrite(HEATPIN1, LOW);
  }
  else
  {
    Serial.print ("ON ");
    digitalWrite(HEATPIN1, HIGH);
  }

  printTemperature(Probe2);
  if (ACTUAL2 < 100) {
    Serial.print(" ");
  }
  Serial.print(SETPOINT2);
  if (SETPOINT2 < 100) {
    Serial.print(" ");
  }
  if (ACTUAL2 >= SETPOINT2)
  {
    Serial.print ("OFF.");
    digitalWrite(HEATPIN2, LOW);
  }
  else
  {
    Serial.print ("ON .");
    digitalWrite(HEATPIN2, HIGH);
  }
}

Processing Code:

import controlP5.*;
ControlP5 cp5;
Knob myKnobA;
Knob myKnobB;

import processing.serial.*;
Serial port;
String ACTUAL1 = "";
String ACTUAL2 = "";
String SETPOINT1 = "";
String SETPOINT2 = "";
String HEATPIN1 = "";
String HEATPIN2 = "";
String data = "";
int index = 0;
PFont font;


void setup()
{

  smooth();
  noStroke();
  cp5 = new ControlP5(this);
  myKnobA = cp5.addKnob("SETPOINT 1")
    .setRange(50, 215)
      .setValue(50)
        .setPosition(50, 350)
          .setRadius(50)
            .setNumberOfTickMarks(33)
              .setTickMarkLength(10)
                .snapToTickMarks(true)
                  .setColorForeground(color(255))
                    .setColorBackground(color(0, 160, 100))
                      .setColorActive(color(255, 255, 0))
                        .setDragDirection(Knob.VERTICAL)
                          ;
  myKnobB = cp5.addKnob("SETPOINT 2")
    .setRange(50, 215)
      .setValue(50)
        .setPosition(200, 350)
          .setRadius(50)
            .setNumberOfTickMarks(33)
              .setTickMarkLength(10)
                .snapToTickMarks(true)
                  .setColorForeground(color(255))
                    .setColorBackground(color(0, 160, 100))
                      .setColorActive(color(255, 255, 0))
                        .setDragDirection(Knob.VERTICAL)
                          ;

  size(360, 500);
  port = new Serial(this, "COM4", 9600);
  port.bufferUntil('.'); 
  font = loadFont("AgencyFB-Bold-200.vlw");
  textFont(font, 100);
}

void draw()
{


  background(0, 0, 0);
  fill(46, 209, 2);
  text(ACTUAL1, 50, 100);
  fill(0, 102, 153);
  text(SETPOINT1, 50, 200);
  fill(50, 2, 100);
  text(HEATPIN1, 50, 300);

  fill(46, 209, 2);
  text(ACTUAL2, 200, 100);
  fill(0, 102, 153);
  text(SETPOINT2, 200, 200);
  fill(50, 2, 100);
  text(HEATPIN2, 200, 300);

  float S1;
  S1 = (myKnobA.getValue());
  println(S1);
}

void keyPressed() {
  switch(key) {
    case('1'):
    myKnobA.setValue(50);
    break;
    case('2'):
    myKnobA.setValue(215);
    break;
    case('3'):
    myKnobB.setValue(50);
    break;
    case('4'):
    myKnobB.setValue(215);
    break;
  }
}

void serialEvent (Serial port)
{
  data = port.readStringUntil('.');
  data = data.substring(0, data.length() - 1);
  index = data.indexOf(",");

  ACTUAL1 = data.substring(0, data.length() - 16);
  SETPOINT1 = data.substring(index+1, data.length() - 12);
  HEATPIN1 = data.substring(index+4, data.length() - 9);

  ACTUAL2 = data.substring(index+7, data.length() - 6);
  SETPOINT2 = data.substring(index+10, data.length() - 3);
  HEATPIN2 = data.substring(index+13, data.length());
}

Viewing all articles
Browse latest Browse all 747

Latest Images

Trending Articles



Latest Images