HOW CAN I DEFINE CODES?

Hello my dear Friends,

I created Step Motor and Led System. I have got to much codes in Arduino. I want to define part part. How can I do it? Such as.. I want to define this chapter name of "stepname" and I want this >if "stepname" is 1, It do this chapter..<

    if(stepsname == 1){
    digitalWrite(dirPin, HIGH);
          
            for (int i = 0; i < 0.5*stepsPerRevolution; i++) {
              
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
    Serial.println("↓50");
            }

I defined it likes this.. I need help. What can I use for this chapter. Could you rewrite for me? :frowning:

               int stepsname = 1;

Thanks for reading..

I have got to much codes in Arduino.

Do you mean that you have run out of memory or that you just want to tidy up the program ?

Maybe you will get some ideas from Planning and Implementing a Program

...R

UKHeliBob:
Do you mean that you have run out of memory or that you just want to tidy up the program ?

I am using Arduino and Processing for my system. I put Buttons on Processing. I can't use Buttons together. I must organize Arduino codes. I will define chapter and I will use defined codes. I clicked Buttons but not working at the momnet. It waits to end of process for work other buttons. I want to use together all Buttons.

What is the format of the messages being sent from Processing to the Arduino ?

Have a look at Serial input basics - updated

Robin2:
Maybe you will get some ideas from Planning and Implementing a Program

...R

I will look it. Thanks for helping but my brain stopped. I am looking 3-4 days but anyone don't help me. I need help for rewrite this chapter...

Is this related to this thread?

UKHeliBob:
What is the format of the messages being sent from Processing to the Arduino ?

Have a look at Serial input basics - updated

newww:125:8: error: 'stepsname' was not declared in this scope

if(stepsname == 1){

^

exit status 1
'stepsname' was not declared in this scope

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

meltDown:
Is this related to this thread?

I found a problem. Firstly I need define codes and I should use defined codes. I will put defined code other codes next..

BerkCil:
newww:125:8: error: 'stepsname' was not declared in this scope

if(stepsname == 1){

^

You will need to post the whole sketch, but clearly " 'stepsname' was not declared in this scope" so where is it declared, if at all?

meltDown:
You will need to post the whole sketch, but clearly " 'stepsname' was not declared in this scope" so where is it declared, if at all?

My codes are complicated. I can share with you but You can't understand. I made change a lot of codes..

So show the part where you declared 'stepsname' so we can see what its scope is.

I wonder if it's a typo for 'stepname' which maybe is declared somewhere?

meltDown:
So show the part where you declared 'stepsname' so we can see what its scope is.

I wonder if it's a typo for 'stepname' which maybe is declared somewhere?

int stepsname = 1;
int incomingByte = 0;

 Serial.begin(9600);
 Serial.println("Press 7 to ↓50");
 Serial.println("Press 6 to ↑50");

void loop() {
     if (Serial.available() > 0) {

       incomingByte = Serial.read();

if(incomingByte == '7'){
             stepsname == 1;
             stepsname == 0;
}

if(incomingByte == '6'){
             stepsname == 1;
             stepsname == 0;
}

   if(stepsname == '1'){
   digitalWrite(dirPin, HIGH);
         
           for (int i = 0; i < 0.5*stepsPerRevolution; i++) {
             
   digitalWrite(stepPin, HIGH);
   delayMicroseconds(500);
   digitalWrite(stepPin, LOW);
   delayMicroseconds(500);
   Serial.println("↓50");
   
   if(stepsname == '0'){
                 for (int i = 0; i < 0*stepsPerRevolution; i++) {
  }
 }
}
}

   if(stepsname == '1'){
           digitalWrite(dirPin, LOW);
         
           for (int i = 0; i < 0.5*stepsPerRevolution; i++) {
             
   digitalWrite(stepPin, HIGH);
   delayMicroseconds(500);
   digitalWrite(stepPin, LOW);
   delayMicroseconds(500);
   Serial.println("↑50");
   
   if(stepsname == '0'){
                 for (int i = 0; i < 0.5*stepsPerRevolution; i++) {
    }
   }
  }
 }
}
}

Can you try and get your words right. You use the word "chapter" this has no place in the language of programming in C. It seems like you may want to use the word "variable" because that is what the compiler is talking about when it says "not in scope".

That is not ALL your code.

We need to see it all. You have no setup function.

It is likely that you defined the stepsname variable in the setup function. If so it is only valid or in scope in the setup function. It has to be defined outside any function to be in scope in all functions.

Grumpy_Mike:
Can you try and get your words right. You use the word "chapter" this has no place in the language of programming in C. It seems like you may want to use the word "variable" because that is what the compiler is talking about when it says "not in scope".

That is not ALL your code.

We need to see it all.

My codes interrupt. I need help to rewrite.

int dirPin = 2;
int stepPin = 3;
int ledPin = 4;
int fanPin = 5;

int incomingByte = 0;

int stepsname = 1;

int stepsPerRevolution = 400;

void setup() {

  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(fanPin, OUTPUT);

  Serial.begin(9600);
  Serial.println("Press 9 to FAN INAKTIVE");
  Serial.println("Press 8 to FAN AKTIVE");
  Serial.println("Press 7 to ↓50");
  Serial.println("Press 6 to ↑50");
  Serial.println("Press 5 to GO HOME");
  Serial.println("Press 4 to LED OFF");
  Serial.println("Press 3 to LED ON");
  Serial.println("Press 2 to StepMotor STOP");
  Serial.println("Press 1 to StepMotor >");
  Serial.println("Press 0 to StepMotor <");
}

void loop() {
      if (Serial.available() > 0) {
        incomingByte = Serial.read();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        
             if(incomingByte == '9'){
           digitalWrite(fanPin, LOW);
           Serial.println("FAN INAKTIVE");
        }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
       
             if(incomingByte == '8'){
           digitalWrite(fanPin, HIGH);
           Serial.println("FAN AKTIVE");
        }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      
            if(incomingByte == '7'){
              stepsname = 1;
              stepsname = 0;
            }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      
            if(incomingByte == '6'){
              stepsname = 1;
              stepsname = 0;
            }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
            if(incomingByte == '5'){
            digitalWrite(dirPin, LOW);
          
            for (int i = 4; i < 4*stepsPerRevolution; i++) {
              
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
    Serial.println("GO HOME");
        }
            }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
  
           if(incomingByte == '4'){
           digitalWrite(ledPin, LOW);
           Serial.println("LED OFF");
        }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
           if(incomingByte == '3'){
           digitalWrite(ledPin, HIGH);
           Serial.println("LED ON");
        }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      
           if(incomingByte == '2'){
           Serial.end();
           Serial.println("StepMotor STOP");
        }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
        if(incomingByte == '1'){

            digitalWrite(dirPin, HIGH);
          
            for (int i = 0; i < 1*stepsPerRevolution; i++) {
              
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
    Serial.println("StepMotor >");
        }
        }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      
         if(incomingByte == '0'){

            digitalWrite(dirPin, LOW);
          
            for (int i = 0; i < 1*stepsPerRevolution; i++) {
              
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
    Serial.println("StepMotor <");
            }
        }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////                  
                   
    if(stepsname == '1'){
    digitalWrite(dirPin, HIGH);
          
            for (int i = 0; i < 0.5*stepsPerRevolution; i++) {
              
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
    Serial.println("↓50");
    
    if(stepsname == '0'){
                  for (int i = 0; i < 0*stepsPerRevolution; i++) {
   }
  }
 }
}
    if(stepsname == '1'){
            digitalWrite(dirPin, LOW);
          
            for (int i = 0; i < 0.5*stepsPerRevolution; i++) {
              
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
    Serial.println("↑50");
    
    if(stepsname == '0'){
                  for (int i = 0; i < 0.5*stepsPerRevolution; i++) {
     }
    }
   }
  }
 }
}

That code compiles correctly on my computer. It does not give the error you have shown.

Grumpy_Mike:
That code compiles correctly on my computer. It does not give the error you have shown.

Problem is not here. Are you understand my problem? Code is look like correct but not working at the moment. My processing code is right. I'm sure. My problem is Arduino codes. Could you read again my topic?

Are you understand my problem?

I am understanding what you talked about in the original post about a variable not being in scope.

You seem to have got over that problem without acknowledging it to us in the forum.

Now it seems you have another problem, that is you are not describing at all in a way that anyone who speaks English as a first language can understand.

The phrase:-

My codes interrupt.

Is meaningless, there is clearly no interrupts going on.

Say what your program does and say what you would want it to do. Use English words. Then, and only then, do we stand a chance of understanding your problem.

If you do actually mean the problem in this thread I would you like to process at the moment when press Buttons - Programming Questions - Arduino Forum then I gave you an answer in reply #1.

If you do not understand this then ask about what you don't understand. No matter who answers they will have no other answer.

Grumpy_Mike:
I am understanding what you talked about in the original post about a variable not being in scope.

You seem to have got over that problem without acknowledging it to us in the forum.

Now it seems you have another problem, that is you are not describing at all in a way that anyone who speaks English as a first language can understand.

The phrase:-Is meaningless, there is clearly no interrupts going on.

Say what your program does and say what you would want it to do. Use English words. Then, and only then, do we stand a chance of understanding your problem.

If you do actually mean the problem in this thread I would you like to process at the moment when press Buttons - Programming Questions - Arduino Forum then I gave you an answer in reply #1.

If you do not understand this then ask about what you don't understand. No matter who answers they will have no other answer.

I haven't got problem with English. I can understand you. I can explain my problem and I understand you can't help me Okay! Thanks for everything

Grumpy_Mike:
That code compiles correctly on my computer. It does not give the error you have shown.

BerkCil:
Problem is not here.

Please confirm the stepsname undefined problem has gone away for you, OP. You just wasted my time getting set up to test something which you seem (as mentioned above by Grumpy_Mike) to have fixed but not told us.