Controlling solenoids using an equation

Share your projects using MegunoLink Pro here!
Post Reply
brunopeniche
Posts: 1
Joined: Fri Mar 29, 2019 1:19 pm

Fri Mar 29, 2019 2:14 pm

Hi people! I am really a novice regarding programming and stuff :D
Previously I managed to control a solenoid by computer with arduino by using a code that a guy from the youtube used with LEDs: [url]https://www.youtube.com/watch?v=vHeG3Gt6STE&t=241s[url] 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.
Do you guys have any idea how to do that using Megunolink? I prefer Megunolink because its much simpler than visual studio. What should I begin with? Continuing using the code of the guy of the video (I will post the code above) and make some alterations?

Code: Select all

#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();
    lcd.print("Ready to connect");    
  }
  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);
  lcd.print("Ready to connect");
}

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:
    char inChar = (char)Serial.read();
    // 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 for your attention!!!
Bruno
philr
Posts: 446
Joined: Mon May 26, 2014 10:58 am

Wed Apr 03, 2019 8:18 am

Hi Bruno, this example might be useful. It shows changing parameters from an interface and saving them to an eeprom. You could do this for your equation parameters.
https://github.com/Megunolink/MLP/tree/ ... WithEEPROM

Cheers
Phil
Post Reply