# Controlling solenoids/LEDs using an equation

Hi people!

Previously I managed to control a solenoid by computer with arduino by using a code that a guy from the youtube used with LEDs: Arduino Tutorial: C# to Arduino Communication. Send data and commands from Computer to an Arduino. - YouTube using visual studio 2017. Now the next step its controlling the solenoids trough an equation. I will explain better: I have a microflow chemical reactor and I wanted to control 2 or 3 solenoids to switch the flow from the solvent bottle into the reactor to the reagent bottle and then back to the solvent bottle and so on… With the previous code from the guy in the youtube video I managed to change the flow from one bottle to another by clicking on the computer to turn the LED ON and OFF (which in my case changed the flows). Now, I wanted a code/programme to control the amount of time that the solenoid would stay on in order to inject the right concentration of reagent into the reactor and then, switch again to the solvent flow. In order to do that I know that I would need to know the flow rate (that in my case can vary depending on the pressure applied in the reactor, but I would always know previously before doing the experiment) and the concentration of the solution. For example: I wanted 0.022 moles of a chemical compound to be injected and knowing that the flow rate is 0.5 ml/min and the concentration of the reagent solution in the bottle (0.5M), the solenoid would need to switch to the reagent bottle (stay ON if we take into account the example of the LEDs) for 8.8 minutes and then switch back to the solvent bottle (stay OFF), I wanted to just digit the flow rate, the amount of reagent needed and the concentration of the solution and then let the programme switch the solenoid ON for 8.8 minutes and then automatically turn it OFF. To support this, I am using Megunolink together with arduino IDE. Do you guys know how to do that?
Other option (maybe simpler) is that I have the graphs with the equation of the line with the flow rate on the Y axis and the pressure on the X axis. I wanted to enter the volume needed and the pressure that I was using and I wanted to the solenoids stay open for x time based on the graph/equation of the line

Here is the code so far in arduino IDE:

``````#include <LiquidCrystal.h>

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
String commandString = "";

int led1Pin = 19;
int led2Pin = 20;
int led3Pin = 21;

boolean isConnected = false;

LiquidCrystal lcd(8,9,4,5,6,7);

void setup() {

Serial.begin(9600);
pinMode(led1Pin,OUTPUT);
pinMode(led2Pin,OUTPUT);
pinMode(led3Pin,OUTPUT);
initDisplay();
}

void loop() {

if(stringComplete)
{
stringComplete = false;
getCommand();

if(commandString.equals("STAR"))
{
lcd.clear();
}
if(commandString.equals("STOP"))
{
turnLedOff(led1Pin);
turnLedOff(led2Pin);
turnLedOff(led3Pin);
lcd.clear();
}
else if(commandString.equals("TEXT"))
{
String text = getTextToPrint();
printText(text);
}
else if(commandString.equals("LED1"))
{
boolean LedState = getLedState();
if(LedState == true)
{
turnLedOn(led1Pin);
}else
{
turnLedOff(led1Pin);
}
}
else if(commandString.equals("LED2"))
{
boolean LedState = getLedState();
if(LedState == true)
{
turnLedOn(led2Pin);
}else
{
turnLedOff(led2Pin);
}
}
else if(commandString.equals("LED3"))
{
boolean LedState = getLedState();
if(LedState == true)
{
turnLedOn(led3Pin);
}else
{
turnLedOff(led3Pin);
}
}

inputString = "";
}

}

void initDisplay()
{
lcd.begin(16, 2);
}

boolean getLedState()
{
boolean state = false;
if(inputString.substring(5,7).equals("ON"))
{
state = true;
}else
{
state = false;
}
return state;
}

void getCommand()
{
if(inputString.length()>0)
{
commandString = inputString.substring(1,5);
}
}

void turnLedOn(int pin)
{
digitalWrite(pin,HIGH);
}

void turnLedOff(int pin)
{
digitalWrite(pin,LOW);
}

String getTextToPrint()
{
String value = inputString.substring(5,inputString.length()-2);
return value;
}

void printText(String text)
{
lcd.clear();
lcd.setCursor(0,0);
if(text.length()<16)
{
lcd.print(text);
}else
{
lcd.print(text.substring(0,16));
lcd.setCursor(0,1);
lcd.print(text.substring(16,32));
}
}

void serialEvent() {
while (Serial.available()) {
// get the new byte:
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
``````

Thank you!!

(I edited the first message with a more simpler plan maybe)

Here's also the visual studio code (the previous one was from arduino)

``````using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace ComputerToArduino
{
public partial class Form1 : Form

{
bool isConnected = false;
String[] ports;
SerialPort port;

public Form1()
{
InitializeComponent();
disableControls();
getAvailableComPorts();

foreach (string port in ports)
{
Console.WriteLine(port);
if (ports[0] != null)
{
comboBox1.SelectedItem = ports[0];
}
}
}

private void button1_Click(object sender, EventArgs e)
{
if (!isConnected)
{
connectToArduino();
} else
{
disconnectFromArduino();
}
}

void getAvailableComPorts()
{
ports = SerialPort.GetPortNames();
}

private void connectToArduino()
{
isConnected = true;
string selectedPort = comboBox1.GetItemText(comboBox1.SelectedItem);
port = new SerialPort(selectedPort, 9600, Parity.None, 8, StopBits.One);
port.Open();
port.Write("#STAR\n");
button1.Text = "Disconnect";
enableControls();
}

private void Led1CheckboxClicked(object sender, EventArgs e)

{
if(isConnected)
{
if(checkBox1.Checked)
{
port.Write("#LED1ON\n");
}else
{
port.Write("#LED1OF\n");
}
}
}

private void Led2CheckboxClicked(object sender, EventArgs e)

{
if (isConnected)
{
if (checkBox2.Checked)
{
port.Write("#LED2ON\n");
}
else
{
port.Write("#LED2OF\n");
}
}
}

private void Led3CheckboxClicked(object sender, EventArgs e)

{
if (isConnected)
{
if (checkBox3.Checked)
{
port.Write("#LED3ON\n");
}
else
{
port.Write("#LED3OF\n");
}
}
}

private void disconnectFromArduino()
{
isConnected = false;
port.Write("#STOP\n");
port.Close();
button1.Text = "Connect";
disableControls();
resetDefaults();
}

private void button2_Click(object sender, EventArgs e)
{
if (isConnected)
{
port.Write("#TEXT" + textBox1.Text + "#\n");
}
}

private void enableControls()
{
checkBox1.Enabled = true;
checkBox2.Enabled = true;
checkBox3.Enabled = true;
button2.Enabled = true;
textBox1.Enabled = true;
groupBox1.Enabled = true;
groupBox3.Enabled = true;

}

private void disableControls()
{
checkBox1.Enabled = false;
checkBox2.Enabled = false;
checkBox3.Enabled = false;
button2.Enabled = false;
textBox1.Enabled = false;
groupBox1.Enabled = false;
groupBox3.Enabled = false;
}

private void resetDefaults()
{
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
textBox1.Text = "";

}

private void groupBox3_Enter(object sender, EventArgs e)
{

}
}
}
``````