Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ Arduino solenoid valves not working

abuet

Coder
Hi so I have been working on a project with several solenoid valves, an arduino uno, two L3 motor shields, and an SD card reader. My code has compiled and is partially working, but the solenoid valves don't turn on when the SD card is being read. It is supposed to read the text file in the SD card, and activate solenoid valves based on which of the letters A, G, T, C, or U are read using several if statements in the loop() function. I have tried working through this, but I am wondering if it is an issue with the SD card being read or the solenoid valve if statements themselves.
Here is my full code:
Code:
#include <Arduino.h>
#include <stdint.h>
#include <SD.h>
#include <SPI.h>

int dataPin = 10; // DIOA
int blPin = 11; // BL
int polPin = 12; // POL
int dirPin = 13; // DIR
int clockPin = 14; // CLK
int latchPin = 15; // LE
int solenoidPin1 = 1;
int solenoidPin2 = 2;
int solenoidPin3 = 3;
int solenoidPin4 = 4;
int solenoidPin5 = 5;

uint64_t one = 1;
uint64_t data;
int d = 2000;
int solenoidPin6 = 6;
int solenoidPin7 = 7;
int solenoidPin8 = 8;
int robotmotor = 9;

void writeData(uint64_t data) {
  char* arr = (char*) &data;
  digitalWrite(latchPin, LOW);
  for (int i=0; i < 8; ++i) {
    char c = arr[i];
    shiftOut(dataPin, clockPin, LSBFIRST, c);
  }
  digitalWrite(latchPin, HIGH);
}






//char solenoidPin1 = A; //connected to Digital 1
//char solenoidPin2 = G; //connected to Digital 2
//char solenoidPin3 = C;
//char solenoidPin4 = T;
//char solenoidPin5 = U;





byte serialData[1];
int nBytes = 0;






// PINS
#define PIN_FEEDBACK (A0)
#define PIN_LE (A1)
#define PIN_CLK (A2)
#define PIN_BL (A3)
#define PIN_DI (A4)
#define PIN_SWA (12)
#define PIN_SWB (11)

// SETTINGS
#define ELECTRODE_ARRAY_WIDTH 8
#define ELECTRODE_ARRAY_HEIGHT 8
const byte HV507_LOOKUP_TABLE [] = {4,5,6,7,12,13,15,14,20,22,23,21,28,31,30,29,36,39,38,37,47,46,45,44,55,62,61,53,54,63,60,52,51,59,56,49,58,50,57,48,43,42,41,40,34,33,32,35,26,25,24,27,18,16,17,19,9,8,10,11,0,1,2,3};
#define BAUD_RATE 115200
#define SERIAL_BUFFER_LENGTH 10
#define TEST_PROG

// VARS
bool electrodes[ELECTRODE_ARRAY_WIDTH][ELECTRODE_ARRAY_HEIGHT];
char serialBuffer[SERIAL_BUFFER_LENGTH];
uint8_t currentIndex=0;

// Electrode array code
void clearElectrodes() {
  for (int x = 0; x <ELECTRODE_ARRAY_WIDTH ; x++)
    for (int y = 0; y <ELECTRODE_ARRAY_HEIGHT ; y++)
      setElectrode(x,y,false);
}

void setElectrodes() {
  for (int x = 0; x <ELECTRODE_ARRAY_WIDTH ; x++)
    for (int y = 0; y <ELECTRODE_ARRAY_HEIGHT ; y++)
    setElectrode(x,y,true);
}

void setElectrode(int x,int y,bool state) {
  if(state != electrodes[x][y]) {
    electrodes[x][y]=state;
    sendElectrode(x,y);
  }
}

void sendElectrode(int x,int y){
  Serial.print(x);
  Serial.print(",");
  Serial.print(y);
  Serial.print(",");
  Serial.print(electrodes[x][y]);
  Serial.print("\n");
}

// Refer to HV507 datasheet
void writeHV507() {
  digitalWrite(PIN_LE, LOW);
  digitalWrite(PIN_CLK, LOW);
  
  for (int i = 0; i <64 ; i++) {
    digitalWrite(PIN_DI,electrodes[HV507_LOOKUP_TABLE[i]%8][HV507_LOOKUP_TABLE[i]/8]);
    digitalWrite(PIN_CLK, HIGH);
    digitalWrite(PIN_CLK, LOW);
  }

  digitalWrite(PIN_LE, HIGH);
  digitalWrite(PIN_LE, LOW);
}
File myFile;
String txtMsg = "";
int relay = 2;

void setup() {
  Serial.begin(BAUD_RATE);
 
  pinMode(PIN_LE, OUTPUT);
  digitalWrite(PIN_LE,LOW);
  pinMode(PIN_CLK, OUTPUT);
  digitalWrite(PIN_CLK,LOW);
  pinMode(PIN_BL, OUTPUT);
  digitalWrite(PIN_BL,HIGH);
  pinMode(PIN_DI, OUTPUT);
  digitalWrite(PIN_DI,LOW);
 
  pinMode(PIN_SWA, INPUT_PULLUP);
  pinMode(PIN_SWB, INPUT_PULLUP);

  clearElectrodes();
  writeHV507();
  digitalWrite(PIN_BL,HIGH);

  for (int x = 0; x <ELECTRODE_ARRAY_WIDTH ; x++)
    for (int y = 0; y <ELECTRODE_ARRAY_HEIGHT ; y++)
      sendElectrode(x,y);

  pinMode(13, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT); 
  pinMode(clockPin, OUTPUT);
  pinMode(blPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(polPin, OUTPUT);
  pinMode(1, OUTPUT); //Sets the pin as an output
pinMode(2, OUTPUT); //Sets the pin as an output
pinMode(3, OUTPUT); //Sets the pin as an output
pinMode(4, OUTPUT); //Sets the pin as an output
pinMode(5, OUTPUT); //Sets the pin as an output
pinMode(6, OUTPUT); //Sets the pin as an output
pinMode(7, OUTPUT); //Sets the pin as an output
pinMode(8, OUTPUT); //Sets the pin as an output
  Serial.begin(9600);
  digitalWrite(blPin, LOW);
  digitalWrite(polPin, LOW); // INVERTING ALL OUTPUT
  digitalWrite(clockPin, LOW);
  digitalWrite(dataPin, LOW);
  digitalWrite(latchPin, HIGH);
  digitalWrite(dirPin, LOW);
  delay(1000);
  digitalWrite(blPin, HIGH);


Serial.begin(9600);
  pinMode(relay, OUTPUT);
  pinMode(10, OUTPUT);
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
myFile = SD.open("oligo.TXT");
while (myFile.available()) {
      Serial.write(myFile.read());
    }
 

}

 
 



#ifdef TEST_PROG
  int step = 0;
  long unsigned int lastT = millis();
#endif

int delayTime = 250;
void loop() {
  if(serialReadCommand()){
    writeHV507();
  }

  // Test program
  #ifdef TEST_PROG
    if(millis()-lastT>500){
      lastT = millis();
      switch(step){
    
        case 0: setElectrode(4,3,false);
        setElectrode(4,4,true);
        break;
        case 1:setElectrode(4,4,false);
        setElectrode(4,5,true);
        break;
        case 2:setElectrode(4,5,false);
        setElectrode(5,5,true);
        break;
        case 3:setElectrode(5,5,false);
        setElectrode(5,4,true);
        break;
        case 4:setElectrode(5,4,false);
        setElectrode(5,3,true);
        break;
        case 5:setElectrode(5,3,false);
        setElectrode(4,3,true);
        break;
        case 6:setElectrode(3,4,true);
        setElectrode(4,3,true);      }
      writeHV507();
      step ++;
      if(step > 5) step = 0;
    }
 
 
 
 
  #endif

{
  if (Serial.readBytes((char*)'U',1)){
digitalWrite(5, HIGH); //Switch Solenoid ON
  delay(1000); //Wait 1 Second
digitalWrite(5, LOW); //Switch Solenoid ON
delay(1000); //Wait 1 Second
digitalWrite(6, HIGH);
delay(1000);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
delay(1000);
digitalWrite(7, LOW);
}
 
  else if (Serial.readBytes((char*)'T',1)){
    digitalWrite(4, HIGH); //Switch Solenoid ON
  delay(1000); //Wait 1 Second
digitalWrite(4, LOW); //Switch Solenoid ON
delay(1000); //Wait 1 Second
digitalWrite(6, HIGH);
delay(1000);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
delay(1000);
digitalWrite(7, LOW);
  }
  else if (Serial.readBytes((char*)'C',1)){
    digitalWrite(3, HIGH); //Switch Solenoid ON
  delay(1000); //Wait 1 Second
digitalWrite(3, LOW); //Switch Solenoid ON
delay(1000); //Wait 1 Second
digitalWrite(6, HIGH);
delay(1000);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
delay(1000);
digitalWrite(7, LOW);
  }
  else if (Serial.readBytes((char*)'G',1))
{
    digitalWrite(2, HIGH); //Switch Solenoid ON
  delay(1000); //Wait 1 Second
digitalWrite(2, LOW); //Switch Solenoid ON
delay(1000); //Wait 1 Second
digitalWrite(6, HIGH);
delay(1000);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
delay(1000);
digitalWrite(7, LOW);
}
   else if (Serial.readBytes((char*)'A',1)){
    digitalWrite(1, HIGH); //Switch Solenoid ON
  delay(1000); //Wait 1 Second
digitalWrite(1, LOW); //Switch Solenoid ON
delay(1000); //Wait 1 Second
digitalWrite(6, HIGH);
delay(1000);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
delay(1000);
digitalWrite(7, LOW);
   }
  writeData(one<<43);
  delay(delayTime);
  writeData(one<<42);
  delay(delayTime);
  writeData(one<<41);
  delay(delayTime);
  writeData(one<<40);
  delay(delayTime);
  writeData(one<<41);
  delay(delayTime);
  writeData(one<<42);
  delay(delayTime);

 





}
}


uint8_t serialReadCommand(){
  uint8_t number_of_commands = 0;
  while (Serial.available() > 0) {
    char recieved = Serial.read();

    serialBuffer[currentIndex] = recieved;
    currentIndex ++;
    if (currentIndex >= SERIAL_BUFFER_LENGTH) {
      // Invalid command: command is too long
      currentIndex = 0;
    }

    if (recieved == '\n' || recieved == '\r') {
      // Close string
      serialBuffer[currentIndex-1] = '\0';
      
      // Clear buffer for next serial transaction
      currentIndex = 0;

      // Split the string
      char* commaIndex = strchr(serialBuffer, ',');
      if (commaIndex==NULL) {
        // Invalid command: command is malformed
        continue;
      }
      commaIndex[0] = '\0';
      char* secondCommaIndex = strchr(commaIndex+1, ',');
      if (secondCommaIndex==NULL) {
        // Invalid command: command is malformed
        continue;
      }
      secondCommaIndex[0] = '\0';

      int x = atoi(serialBuffer);
      int y = atoi(commaIndex+1);
      if(x<0 || x>=ELECTRODE_ARRAY_WIDTH || y<0 || y>=ELECTRODE_ARRAY_HEIGHT){
        // Invalid command: out of bound
        continue;
      }
      
      setElectrode(x,y,strcmp(secondCommaIndex+1,"0")!=0);
      number_of_commands += 1;
    }
  }
  return number_of_commands;
}

/*void btnMatrixTest(){
  for (int y = 0; y <ELECTRODE_ARRAY_WIDTH ; y++)
    for (int x = 0; x <ELECTRODE_ARRAY_HEIGHT ; x++){
      clearElectrodes();
      electrodes[x][y]=true;
      writeHV507();
      Serial.print(x);
      Serial.print("\t");
      Serial.println(y);
      delay(500);
      while(digitalRead(PIN_SWA)){}
    }
}*/

Any help is greatly appreciated.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom