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.

Arduino solenoid valve code. Particular issues with declaration and if statments.

abuet

Coder
Hi, I am very new to Arduino coding and I am trying to make a solenoid valve code to run on an if..else statement. However, I keep getting errors like:
'solenoidPin1' was not declared in this scope. Here is my code so far, with an if else statement at the bottom. The
Code:
void setup()
section is where I keep getting this error, and it appears again when I reference each valve in
Code:
digitalWrite(solenoidPin1, LOW)

full code:

Code:
   #include <Arduino.h>
#include <stdint.h>
        int main(){
int solenoidPin1 = 1; //connected to Digital 1
int solenoidPin2 = 2; //connected to Digital 2
int solenoidPin3 = 3;
int solenoidPin4 = 4;
int solenoidPin5 = 5;
int solenoidPin6 = 6;
int solenoidPin7 = 7;
int solenoidPin8 = 8;
        }

void setup() {
// put your setup code here, to run once:
pinMode(solenoidPin1, OUTPUT); //Sets the pin as an output
pinMode(solenoidPin2, OUTPUT); //Sets the pin as an output
pinMode(solenoidPin3, OUTPUT); //Sets the pin as an output
pinMode(solenoidPin4, OUTPUT); //Sets the pin as an output
pinMode(solenoidPin5, OUTPUT); //Sets the pin as an output
pinMode(solenoidPin6, OUTPUT); //Sets the pin as an output
pinMode(solenoidPin7, OUTPUT); //Sets the pin as an output
pinMode(solenoidPin8, OUTPUT); //Sets the pin as an output
}

void loop() {
// put your main code here, to run repeatedly:
if digitalWrite(solenoidPin1, HIGH){ //Switch Solenoid ON
delay(1000); //Wait 1 Second
digitalWrite(solenoidPin1, LOW); //Switch Solenoid ON
delay(1000); //Wait 1 Second
digitalWrite(solenoidPin6, HIGH);
delay(1000);
digitalWrite(solenoidPin6, LOW);
digitalWrite(solenoidPin7, HIGH);
delay(1000);
digitalWrite(solenoidPin7, LOW);
else digitalWrite(solenoidPin1, LOW);
}
}

Any help is greatly appreciated, I have tried everything I know, and could really use some guidance from someone more experienced.
 
What is unclear about the error message ? Your variables are local to the main() function, and therefore not recognized in setup() and loop().
Move these declarations up, outside main(), so they'll be global and your code will at least compile (if not actuall DO anything...).
 
What is unclear about the error message ? Your variables are local to the main() function, and therefore not recognized in setup() and loop().
Move these declarations up, outside main(), so they'll be global and your code will at least compile (if not actuall DO anything...).
thank you so much! I really appreciate your help especially since I'm so new at this.
 
thank you so much! I really appreciate your help especially since I'm so new at this.
Glad I could help.
I don't like the look of your loop() function though. Does that even compile, with that else inside the if branch ?

Personally I'd use shorter variable and function names, and incorporate the optional delay in the digitalWrite() function. So that code could then look like this
C#:
if ( set(pin1, HI, 0) )
{
    delay(1000);
    set(pin1, LO, 1000);
    set(pin6, HI, 1000);
    set(pin6, LO, 0);
    set(pin7, HI, 1000);
    set(pin7, LO, 0);
}
else
{
    set(pin1, LO, 0);
}

(note the added parentheses and curly brackets). Keeping things simple and short will help your programming.
 
Glad I could help.
I don't like the look of your loop() function though. Does that even compile, with that else inside the if branch ?

Personally I'd use shorter variable and function names, and incorporate the optional delay in the digitalWrite() function. So that code could then look like this
C#:
if ( set(pin1, HI, 0) )
{
    delay(1000);
    set(pin1, LO, 1000);
    set(pin6, HI, 1000);
    set(pin6, LO, 0);
    set(pin7, HI, 1000);
    set(pin7, LO, 0);
}
else
{
    set(pin1, LO, 0);
}

(note the added parentheses and curly brackets). Keeping things simple and short will help your programming.
I have been trying to make that work, but the Arduino IDE ends up not recognizing a lot of the variables. The last suggestion you gave me helped a lot, and allowed the code to compile. I have also been trying to take an SD card file, and assemble each individual character into an array. I have been using File::read() to read one character at a time, however I cant seem to find a way to declare the letters A, G, T, C, and U into valid parts of the if statements for solenoids 1-5.
My goal is to make the solenoids activate when they read one of the given letters assigned (like solenoid 1 would activate on reading A, 2 on G, and so on).
I have been trying this:
Code:
  if (File::read(C)){
but I get the error "
'C' was not declared in this scope".
Any suggestions?
 
Sorry I forgot to add this on. I have also been trying to add a final if statement for when the program reaches the end of the text file. I'm not sure where to start here exactly. Sorry to bother you again.
 
Sorry I forgot to add this on. I have also been trying to add a final if statement for when the program reaches the end of the text file. I'm not sure where to start here exactly. Sorry to bother you again.
Sorry, I'm totally unsure what your problems now are !
If the compiler says "'C' was not declared in this scope" then I guess you need to declare C.
... a final if statement ???
You need to provide code.
You wrote you're new to Arduino coding, but do you have C(++) programming experience ?
 
Sorry, I'm totally unsure what your problems now are !
If the compiler says "'C' was not declared in this scope" then I guess you need to declare C.
... a final if statement ???
You need to provide code.
You wrote you're new to Arduino coding, but do you have C(++) programming experience ?
Sorry, I'm totally unsure what your problems now are !
If the compiler says "'C' was not declared in this scope" then I guess you need to declare C.
... a final if statement ???
You need to provide code.
You wrote you're new to Arduino coding, but do you have C(++) programming experience ?
I have been trying to modify the code so each if statement starts with a dependency for each letter (A,G,T,C,U) read from text file with the statement
" if (File::read(A)){ ". The "final statement" I was referring to was the use of " if (File::read('\0')){ " to allow the if statement for running robotmotor to be dependent on the end of the file. I thought that the use of '\0' characterized the end of the text file content, so I used it instead of "file.end" or something. Again, I am very new to this, so Im unsure if these commands are correct. For these statements, I have been having trouble since (A,G,T,C,U) are not declared, but when I try to declare it as " int solenoidPin1 = analogRead(A0); ", it conflicts with the original declaration of the solenoid pin as a digital pin connection. In the case of solenidPin1, it would conflict with " int solenoidPin1 = 1; ".




Code:
#include <Arduino.h>
#include <stdint.h>
#include <SD.h>
#include <SPI.h>

int dataPin = 2; // DIOA
int blPin = 3; // BL
int polPin = 4; // POL
int dirPin = 6; // DIR
int clockPin = 5; // CLK
int latchPin = 7; // LE
int solenoidPin1 = 1; //connected to Digital 1
int solenoidPin2 = 2; //connected to Digital 2
int solenoidPin3 = 3;
int solenoidPin4 = 4;
int solenoidPin5 = 5;
int solenoidPin6 = 6;
int solenoidPin7 = 7;
int solenoidPin8 = 8;
int robotmotor = 9;

uint64_t one = 1;
uint64_t data;
int d = 2000;

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);
}

File dataFile;
void open_me(){
dataFile = SD.open("oligo.txt");
if (dataFile) {
  while (dataFile.available()) {
      Serial.write(dataFile.read());

}
dataFile.close();
    open_me();
}
}


 int solenoidPin1 = analogRead(A0);
  int solenoidPin2 = analogRead(G);
 int solenoidPin3 = analogRead(C);
  int solenoidPin4 = analogRead(T);
   int solenoidPin5 = analogRead(U);
    int robotmotor = analogRead('\0');
 int totaal[0];


void setup() {
  pinMode(13, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  pinMode(blPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(polPin, OUTPUT);
  pinMode(solenoidPin1, 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);

  if (File::read(A)){
    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);

  }
  else digitalWrite(1, LOW);

  if (File::read(G)){

    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 digitalWrite(2, LOW);

  


  if (File::read(C)){
        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 digitalWrite(3, LOW);




  if (File::read(T)){
    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 digitalWrite(4, LOW);



  if (File::read(U)){
    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 digitalWrite(5, LOW);


}

  if (File::read('\0')){
    digitalWrite(9, HIGH); //Switch Solenoid ON
  delay(1000); //Wait 1 Second
digitalWrite(9, LOW); //Switch Solenoid ON
delay(1000); //Wait 1 Second
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, LOW);


  }
  else digitalWrite(9, LOW);

int delayTime = 250;
void loop() {
  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);
}

If you have any ideas on this It would be much appreciated! Thank you again.
 
I have been trying to modify the code so each if statement starts with a dependency for each letter (A,G,T,C,U) read from text file with the statement
" if (File::read(A)){ ". The "final statement" I was referring to was the use of " if (File::read('\0')){ " to allow the if statement for running robotmotor to be dependent on the end of the file. I thought that the use of '\0' characterized the end of the text file content, so I used it instead of "file.end" or something. Again, I am very new to this, so Im unsure if these commands are correct. For these statements, I have been having trouble since (A,G,T,C,U) are not declared, but when I try to declare it as " int solenoidPin1 = analogRead(A0); ", it conflicts with the original declaration of the solenoid pin as a digital pin connection. In the case of solenidPin1, it would conflict with " int solenoidPin1 = 1; ".

If you have any ideas on this It would be much appreciated! Thank you again.
Hm, this code is real hard to follow because of the inconsistent indentation and the dependency on Arduino include files. I don't really want to know about Arduino, sorry.... so I can only comment on C-technical issues. If something like A is not defined in your include files, I can't help you there.
Couple things that catch my eye:

1) Function open_me() is recursive. Looks to me you'll never get out of it as long as oligo.txt can be opened. Why is it programmed like that ?

2) if (File::read(A)) What kind of thingy is A ? What file is being read here ? What exactly do you expect this statement to do ?

3) File::read('\0') This looks even weirder to me.... Is this supposed to be an end-of-file condition ? You better do some research on reading a file in Arduino.

4) What is this loop() function doing there ? It seems to be inside the setup() function... I did not know this was even allowed.
 
Hm, this code is real hard to follow because of the inconsistent indentation and the dependency on Arduino include files. I don't really want to know about Arduino, sorry.... so I can only comment on C-technical issues. If something like A is not defined in your include files, I can't help you there.
Couple things that catch my eye:

1) Function open_me() is recursive. Looks to me you'll never get out of it as long as oligo.txt can be opened. Why is it programmed like that ?

2) if (File::read(A)) What kind of thingy is A ? What file is being read here ? What exactly do you expect this statement to do ?

3) File::read('\0') This looks even weirder to me.... Is this supposed to be an end-of-file condition ? You better do some research on reading a file in Arduino.

4) What is this loop() function doing there ? It seems to be inside the setup() function... I did not know this was even allowed.
1. The function open_me() is programmed like that because I wanted the program to run on the insertion of the SD card, which holds the oligo.txt file.
2. A, G, C, T, and U are all supposed to be the letters read from the oligo.txt file. I wanted the statement to only run when it reads the given letter. Like in the case of if (File::read(A)), I wanted the code to only run on reading letter A in the text file.
3. File::read('\0') is supposed to be the end-of-file condition, where the code in the statement only runs when it reads no more characters in the file. I was to understand that using '\0' was a way to characterize the end of a file, but I wasn't entirely sure so I figured I should seek help.
4. The loop() function is essentially just there to allow the software to complete its function, however the code there is functional. I did not know that that wasn't allowed, I just progressively went with it and it worked so I just let it be.
 

New Threads

Buy us a coffee!

Back
Top Bottom