can someone please help with my automation program

the wash system in the shop went brain dead and i took on the repair challenge i tried to make what i am doing fairly clear for me to read and edit but its been a couple years since ive toyed with my arduinos and im running in circles out of the gate again lol

heres the code i wrote

void setup() {


//POSITION SENSORS AND FUNCTION SWITCHES
const int START = A0;
const int STOP = A1;
const int HOME = A2;
const int TANK1 = A3;
const int TANK2 = A4;
const int DRYEROPENED = A5;
const int DRYERCLOSED = A6;
const int WASHDONE = A7;
const int DRAIN1DONE = A8;
const int RINSEDONE = A9;
const int DRAINTANK2DONE = A10;
const int DRYERDONE = A11;
const int HOISTUP = A12;
const int PROGRAM1 = A13;
const int PROGRAM2 = A14;
const int DRYERHOME = A15;
int OFF = 0;
int ON = 255;

//MOTOR AND PNUMATIC CONTROL PINS

const int DRAIN2TIMER = 2;
const int RINSETIMER = 3;
const int DRAIN1TIMER = 4;
const int WASHTIMER = 5;
const int DRYERON = 6;
const int TURBO2 = 7;
const int TURBO1 = 8;
const int BASKETRIGHT = 9;
const int BASKETLEFT = 10;
const int BASKETUP = 11;
const int BASKETDOWN = 12;
const int DRYEROPEN = 13;
const int DRYERCLOSE = 14;
const int BASKETROTATE = 15;
const int WAIT = 16;
const int DRYERTIMER = 17;

  pinMode(DRAIN2TIMER, OUTPUT);
  pinMode(RINSETIMER, OUTPUT);
  pinMode(DRAIN1TIMER, OUTPUT);
  pinMode(WASHTIMER, OUTPUT);
  pinMode(DRYERON, OUTPUT);
  pinMode(TURBO2, OUTPUT);
  pinMode(TURBO1, OUTPUT);
  pinMode(BASKETRIGHT, OUTPUT);
  pinMode(BASKETLEFT, OUTPUT);
  pinMode(BASKETUP, OUTPUT);
  pinMode(BASKETDOWN, OUTPUT);
  pinMode(DRYEROPEN, OUTPUT);
  pinMode(DRYERCLOSE, OUTPUT);
  pinMode(BASKETROTATE, OUTPUT);
  pinMode(WAIT, OUTPUT);
  pinMode(DRYERTIMER, OUTPUT);


void loop


  
if((analogRead(START, 255)) and (analogRead(PROGRAM1, 255))){
  digitalWrite(WAIT,ON);
  digitalWrite(BASKETUP,ON);
  while((analogRead(HOME, 0)) and (analogRead(HOISTUP, 0))){
    digitalWrite(BASKETUP,OFF);
    digitalWrite(BASKETRIGHT,ON);
    while(analogRead(TANK1, 0)){
      digitalWrite(BASKETRIGHT,OFF);
      digitalWrite(BASKETDOWN,ON);
      digitalWrite(TURBO1,ON);
      delay(10100);
      digitalWrite(BASKETDOWN,OFF);
      digitalWrite(BASKETROTATE,ON);
      digitalWrite(WASHTIMER,ON);
      while(analogRead(WASHDONE, 0)){
        digitalWrite(WASHTIMER,OFF);
        digitalWrite(TURBO1,OFF);
        digitalWrite(BASKETROTATE,OFF);
        digitalWrite(BASKETUP,ON);
        while(analogRead(HOISTUP, 0)){
          digitalWrite(BASKETUP,OFF);
          digitalWrite(DRAIN1TIMER,ON);
          repete(10){
            digitalWrite(BASKETROTATE,ON);
            delay(3000);
            digitalWrite(BASKETROTATE,OFF);
            delay(10000);
          }
          while(analogRead(DRAIN1DONE, 0)){
            digitalWrite(DRAIN1TIMER,OFF);
            digitalWrite(BASKETROTATE,OFF);
            digitalWrite(BASKETRIGHT,ON);
            while(analogRead(TANK2, 0)){
              digitalWrite(BASKETRIGHT,OFF);
              digitalWrite(RINSETIMER,ON);
              digitalWrite(BASKETDOWN,ON);
              digitalWrite(TURBO2,ON);
              delay(10100);
              digitalWrite(BASKETDOWN,OFF);
              digitalWrite(BASKETROTATE,ON);
              while(analogRead(RINSEDONE, 0)){
                digitalWrite(TURBO2,OFF);
                digitalWrite(BASKETROTATE,OFF);
                digitalWrite(RINSETIMER,OFF);
                digitalWrite(BASKETUP,ON);
                while(analogRead(HOISTUP, 0)){
                  digitalWrite(BASKETUP,OFF);
                  digitalWrite(DRAIN2TIMER,ON);
                  digitalWrite(DRYEROPEN,ON);
                  repeat(10){
                    digitalWrite(BASKETROTATE,ON);
                    delay(3000);
                    digitalWrite(BASKETROTATE,OFF);
                    delay(10000);
                  }
                  while((analogRead(DRAINTANK2DONE, 0)) and (analogRead(DRYEROPENED, 0))){
                    digitalWrite(DRAIN2TIMER,OFF);
                    digitalWrite(BASKETROTATE,OFF);
                    digitalWrite(BASKETRIGHT,ON);
                    while(analogRead(DRYERHOME, 0)){
                      digitalWrite(BASKETRIGHT,OFF);
                      digitalWrite(DRYERON,ON);
                      digitalWrite(DRYEROPEN,OFF);
                      digitalWrite(DRYERCLOSE,ON);
                      while(analogRead(DRYERCLOSED, 0)){
                        digitalWrite(DRYERTIMER,ON);
                        digitalWrite(BASKETROTATE,ON);
                        while(analogRead(DRYERDONE, 0)){
                          digitalWrite(DRYERCLOSE,OFF);
                          digitalWrite(DRYEROPEN,ON);
                          while(analogRead(DRYEROPENED, 0)){
                            digitalWrite(DRYERON,OFF);
                            digitalWrite(DRYEROPEN,OFF);
                            digitalWrite(BASKETROTATE,OFF);
                            digitalWrite(BASKETLEFT,ON);
                            while(analogRead(HOME, 0)){
                              digitalWrite(BASKETLEFT,OFF);
                              digitalWrite(BASKETDOWN,ON);
                              delay(10100);
                              digitalWrite(BASKETDOWN,OFF);
                              digitalWrite(WAIT,OFF);
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

}
digitalWrite (DRAIN2TIMER, OFF);
digitalWrite (RINSETIMER, OFF);
digitalWrite (DRAIN1TIMER, OFF);
digitalWrite (WASHTIMER, OFF);
digitalWrite (DRYERON, OFF);
digitalWrite (TURBO2, OFF);
digitalWrite (TURBO1, OFF);
digitalWrite (BASKETRIGHT, OFF);
digitalWrite (BASKETLEFT,OFF);
digitalWrite (BASKETUP, OFF);
digitalWrite (BASKETDOWN, OFF);
digitalWrite (DRYEROPEN, OFF);
digitalWrite (DRYERCLOSE, OFF);
digitalWrite (BASKETROTATE, OFF);
digitalWrite (WAIT, OFF);
digitalWrite (DRYERTIMER, OFF);

and these are the errors i have so far

Arduino: 1.8.5 (Windows 7), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\Fated DarkStar\Documents\WASH_PROGRAM_C__\WASH_PROGRAM_C__.ino: In function 'void setup()':

WASH_PROGRAM_C__:66: error: expected initializer before 'if'

if((analogRead(START, 255)) and (analogRead(PROGRAM1, 255))){

^

WASH_PROGRAM_C__:179: error: expected '}' at end of input

digitalWrite (DRYERTIMER, OFF);

^

exit status 1
expected initializer before 'if'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

if anyone could provide some advice it would be greatly apreciated

photos of the project

  void loop
  if ((analogRead(START, 255)) and (analogRead(PROGRAM1, 255)))
  {

No parameter brackets or opening brace for the loop() function and wrong syntax for analogRead() for starters

Hi there!

It seems like there is a small syntax issue. In one of the lines, there is the command "void loop". When you try to compile/upload this code, the compiler is going to look for some other characters after that, which are not in your code. Try replacing "void loop" with "void loop() {" and see what happens.

I am not even getting that far when I try to compile what you have.

:roll_eyes:

bos1714:
Hi there!

It seems like there is a small syntax issue. In one of the lines, there is the command "void loop". When you try to compile/upload this code, the compiler is going to look for some other characters after that, which are not in your code. Try replacing "void loop" with "void loop() {" and see what happens.

ive been playing with that and i keep getting the

WASH_PROGRAM_C__:62: error: a function-definition is not allowed here before '{' token

void loop(){

error but through my reading im finding it may be an opening bracket { without a closing bracket } could the issue be that i stacked the brackets inside each other like this ?

{
{while this do (this)
{if this do (this)
}
}

ive been playing with that and i keep getting the

We can't see your code.

NewTylerJH:
ive been playing with that and i keep getting the

WASH_PROGRAM_C__:62: error: a function-definition is not allowed here before '{' token

void loop(){

error but through my reading im finding it may be an opening bracket { without a closing bracket } could the issue be that i stacked the brackets inside each other like this ?

{
{while this do (this)
{if this do (this)
}
}

It is possible. Not to be rude or anything, but the style of the code is a little messy and that can create syntax and mismatched-brace problems very very easily. I would try to format the code so that corresponding left and right braces match up with each other. That way it can easily be seen if there is an extra (or missing) brace.

Also, the scope rules say that variables defined in one function are not in scope in another, so you've got a heap of editing to do.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html

Where is the closing } for the void setup().
Also declare your contanstants and variables before the void setup(), not in it.
The way you have them, the constants and variables will not be global.

I suggest you put a } on a lone before the void loop()

Tom.. :slight_smile:

So I went through the code you originally posted and reformatted it so that it looks a little nicer. Each left brace had its corresponding right brace as supposed to.

I did notice that you have several digitalWrite(); statements after the closing brace for the loop portion. This is what probably had caused your errors. Any code that is outside of the setup() or loop() portions must be contained by its own set of curly braces {}. The digitalWrites are not enclosed, and as such the compiler thinks that they are part of the loop, but the closing brace for the loop portion has already occurred.

It seems like the issue is confusion over the flow of an Arduino sketch. When a sketch is initiated, the program will flow from line 1 all the way down until it reaches the setup() area. Any functions that it sees before this will be skipped over. The setup() code is then run once and only once. Then the code procedes to the loop() portion, which then repeats itself over and over. The code that you appear to have written seems to be based of the assumption that Arduino code is sequential, which is it, but is also not. I would recommend you look up a youtube tutorial on how Arduino sketches are formatted.

I also notices a few grammar errors. For instance, when you call your digitalWrite commands, you call digitalWrite(pin, OFF). You have an integer that defines off to be 0, and another that sets on to be 255. The only inputs the digitalWrite can take are the pin number, and then a HIGH or a LOW(could also do 1 or 0). So just read up on how to use the different Arduino functions and your code will work wonderfully.

I attached a formatted version of your code, so try to run it and see what happens!

Code_Formatted.ino (6.17 KB)

bos1714:
The only inputs the digitalWrite can take are the pin number, and then a HIGH or a LOW(could also do 1 or 0).

It would perhaps be nice if that were so, but it’s not.
0 takes the pin low, any other value (even negative) takes it high.

i dont know if it will help but what im basically trying to do is power relays control large machinery moving a basket to its next stage then stopping and waiting for the machines next input before continuing the program as for the inputs i removed all of the 220v power from the limit switches and ran a 5v power supply to them so it is a simple open close circuit ive written i understand my work is sloppy lol im not very well versed in c could anyone point me towards some references that i could use to learn how to do what im trying to do?

You seem to be using "analogRead(pin, 0)" when you mean "digitalRead(pin) == LOW)". You can generally treat analog input pins as digital I/O pins (except A6 and A7 on a Nano).

You forgot to set pinMode() on your input pins. Generally done in setup().

Any code that is outside of the setup() or loop() portions must be contained by its own set of curly braces {}

Close, but wrong

Any code that is outside of the setup() or loop() functions must be within another function. It is then true that the code will be between contained by curly braces {}

UKHeliBob:
Close, but wrong

Any code that is outside of the setup() or loop() functions must be within another function. It is then true that the code will be between contained by curly braces {}

I know that, but from earlier posts it seems like the original poster is not entirely familiar with the flow of an Arduino sketch, so I am trying to put it into terms that might be better understood from his/her viewpoint

its true im a dummy and some of the terms you guys are using are adding some confusion lol im a g code programmer im not entirely used to c++

bos1714:
I know that, but from earlier posts it seems like the original poster is not entirely familiar with the flow of an Arduino sketch, so I am trying to put it into terms that might be better understood from his/her viewpoint

By all means put it into terms that might be better understood, but what you said was wrong. Merely putting code in curly braces when it is not in a
the setup() or loop() functions achieves nothing.

i think i might reconfigure the program using all if commands if inputs 1 and 2 then run this section seeing as theres no other simple way that i can find for it to pause the loop and continue only when an input is detected now my question would be if i were to do that is there a possibility of some kind of interference like say as its running a section of code in 1 if command and its hits say a limit switch while its doing that will the arduino run that if command at that point? i cant really have that happening and wrecking an expensive piece of equipment but i need to get this equipment running automatically again

is there a possibility of some kind of interference like say as its running a section of code in 1 if command and its hits say a limit switch while its doing that will the arduino run that if command at that point?

The secret is to never stop the loop() function running freely. You really don't want to pause at any point, which means no while loops or delay()s

If there is a long term action then do it in stages so that you can check inputs between stages. To take the limit switches as an example. Your code should move the device a little, check whether a limit switch has been activated, move the device a little, check whether a limit switch has been activated and so on until a limit is reached.

Can I suggest that you look at Several things at the same time and note how the actions are split into small sections and how the examples use loop() to keep the program moving.

Hi,
Read ALL your inputs at the start of the void loop() and assign results to variables.
This way you take a snapshot of you project.
Act on the snapshot values and set your outputs as you work through the input results.

Also do your code in stages, that is get each I/O working separately to prove your code and hardware.
Then start combining code parts. one at a time, getting the code to work before adding the next part.

Tom... :slight_smile: