Click here to Skip to main content
15,844,903 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
so my Arduino programming fails after it reaches void stageSeven and my mega2560 does not do anything and I have to reset. please help with resolving the problematic part or how to redo everything properly.

C#
const int closerpin = 3;
const int openerpin = 4;
const int autopin = 7;
const int closemotorpin= 10;
const int openmotorpin= 12;
const int sensorpin= 5;
int openstate = 0;
int closestate = 0;
int autostate = 0;
int sensorstate = 0;



void setup(){
  pinMode(closerpin, INPUT);
  pinMode(openerpin, INPUT);
  pinMode(closemotorpin, OUTPUT);
  pinMode(openmotorpin, OUTPUT);
  pinMode(autopin, INPUT);
  pinMode(sensorpin, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  sensorstate = digitalRead(sensorpin);
  autostate = digitalRead(autopin);
  openstate = digitalRead(openerpin);
  closestate = digitalRead(closerpin);
  if (openstate == HIGH) {
    stageOne();
  }
  else {
    stageTwo();
  }

}
void stageOne(){
  sensorstate = digitalRead(sensorpin);
  autostate = digitalRead(autopin);
  openstate = digitalRead(openerpin);
  closestate = digitalRead(closerpin);

  digitalWrite(openmotorpin, HIGH);
  delay(8000);
  digitalWrite(openmotorpin, LOW);
  stageTwo();
}   //open
void stageTwo() {
  sensorstate = digitalRead(sensorpin);
  autostate = digitalRead(autopin);
  openstate = digitalRead(openerpin);
  closestate = digitalRead(closerpin);

  if (autostate == HIGH) {
    stageFour();
  }
  else if (closestate == HIGH) {
    stageThree();
  }
  else {
     stageTwo();
  }
}
//close n auto check
void stageThree() {
  sensorstate = digitalRead(sensorpin);
  autostate = digitalRead(autopin);
  openstate = digitalRead(openerpin);
  closestate = digitalRead(closerpin);
  digitalWrite(closemotorpin, HIGH);
  delay(8000);
  digitalWrite(closemotorpin, LOW);
  stageFive();
}  //close
void stageFour()  {
  sensorstate = digitalRead(sensorpin);
  autostate = digitalRead(autopin);
  openstate = digitalRead(openerpin);
  closestate = digitalRead(closerpin);
  if (sensorstate == HIGH){
    digitalWrite(closemotorpin, HIGH);
    delay(8000);
    digitalWrite(closemotorpin, LOW);
     stageSeven();
  }
  else if (closestate == HIGH){
    stageThree();
  }
  else if (openstate == HIGH) {
    stageOne();
  }
  else if (sensorstate == LOW){
     stageFour();
  }
}
void stageFive() {
  sensorstate = digitalRead(sensorpin);
  autostate = digitalRead(autopin);
  openstate = digitalRead(openerpin);
  closestate = digitalRead(closerpin);
  if (openstate == HIGH) {
    stageOne();
  }
  else if  (autostate == HIGH)  {
    stageFour();
  }
  else {
    stageFive();
  }
}
//open n auto check


void stageSeven(){
  if (sensorstate == LOW){
    digitalWrite(openmotorpin, HIGH);
    delay(8000);
    digitalWrite(openmotorpin, LOW);
    if (closestate == HIGH){
      stageThree();
    }
    else if (sensorstate == HIGH) {
      digitalWrite(closemotorpin, HIGH);
      stageSeven();
    }
    else{
       stageSeven();
    }
  }
  else if (openstate == HIGH) {
    stageOne();
  }
  else{
     stageSeven();
  }

}
Posted
Comments
DaveAuld 15-Dec-13 16:34pm    
It looks like it is possible that Stage7 can be caught in an endless loop depending on the states. I would be tempted to refactor the code and put some Serial statements in to aid debugging.
DaveAuld 16-Dec-13 1:34am    
Having had another look at the code, I would not be relying on the delays to allow states to change, that is asking for a fail. I would hold in a loop until the expected state is reached or until a timer times out in which case take to a hold state and announce 'fault'.
Member 10468057 16-Dec-13 6:39am    
all the other lines with delay work fine except the last void doesnt function after uploading.what could have gone wrong at the last void?
DaveAuld 16-Dec-13 6:49am    
I will have another look when I get a chance, but is there any reason why there is no Stage6? Have you checked all the variable names, Block Braces match etc. Does it build properly in the IDE?
DaveAuld 16-Dec-13 7:00am    
See my solution, does that fix it.....

1 solution

At the start of every stage you are reading the input states

At Stage 7 you have ommitted this.

Does that fix it.....
 
Share this answer
 
Comments
Member 10468057 16-Dec-13 9:06am    
I just put the readings at almost every stage to be sure, but as I have done lots of testings on other programs it doesnt make any difference but it should be on void loop though

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900