Pin 13 blinks, and void loop doesnt loop

I am trying to get a sort of menu, so that I can control a motor like thing through a python interface. I try to use the serial read to first read 1 byte of the code, and depending on that one byte, it directs the code for the arduino to pick up the rest of the serial data, and move the motor like thing a certain amount of ticks.

#include <zoSmsMaster.h>
#include <stdio.h>
#include <stdlib.h>
#define NodeID 0x04


int inputValueFinal = 0;
boolean State = true;
  
void setup(){
  zoCommandStart(NodeID);
  zoSmsMasterInitI2c();
  Serial.begin(9600);
}

int PythonValue(){
  
  char inputValues[4]; 
  if(Serial.available() > 0){
    for(int i = 0; i < 4; i++){
     inputValues[i] = Serial.read();
    }
  }
    
   int inputValueFinal = atoi(inputValues);
   
   return inputValueFinal;
}


void loop(){

  if(State = false){
    zoCommandStart(NodeID);
  }
  while(Serial.available() > 0){
    int Cstate = (Serial.read()-48);
    Serial.println(Cstate);
    
    
    
     if (Cstate = 1){
      zoCommandVelocityMove(NodeID,5000);
      delay(3000);
    }
    
    else if(Cstate = 2){
      int i = PythonValue();
      if(i < 0){
      }
      zoCommandRelativePositionMove(NodeID,i);
      delay(3300);
    }
    
    else if(Cstate = 3){
      int i = PythonValue();
      if(i < 0){
        zoCommandAbsolutePositionMove(NodeID,i);
        delay(3300);
      }
    }
  }
  
}

Whenever I upload that. my led on pin 13 starts to blink at a regular interval. and doesn't do anything. The serial.print at the very top of void loop() doesn't even print. Im using a duevnova with an ATmega168 chip.

*** The zoCommands are part of the motor library. I tested them all, and they shouldnt cause this problem

Not sure if this is the problem, but at first glance you've mistaken the assignment (=) operator with the compare (==) operator a number of times.

2nd thought: nothing ever arrives on the serial port... so Serial.available() is always false (i.e. !> 0), and your while loop never executes.

 switch Cstate;
        
     1){
      zoCommandVelocityMove(NodeID,5000);
      delay(3000);
    }

Huh? How do you get that to compile?

@drhex That was a small type in the copy paste, I fixed it

@Mitch_CA Ill try to edit it to see what I can do

Thanks for answering so fast! =D

It's either

switch (Cstate) {
  case 1:  // do something
             break;
  case 2: // do something else
            break;
}

or

if (Cstate == 1) {
    // do something
} else if (Cstate == 2) {
   // do something else
}

Don't mix if and switch.

if(Serial.available() > 0){
    for(int i = 0; i < 4; i++){
     inputValues[i] = Serial.read();
    }

Imagine what happens if "Serial.available" returns 1.

int Cstate = Serial.read()- '0';

is so much easier to read.

int inputValueFinal = atoi(inputValues);

This can work only if you terminate your string.

I tried rewriting it, with your comments, and I still get no response. Its as if it just does not go into the loop function at all.

#include <zoSmsMaster.h>
#include <stdio.h>
#include <stdlib.h>
#define NodeID 0x04


int inputValueFinal = 0;
boolean State = true;
  
void setup(){
  zoCommandStart(NodeID);
  zoSmsMasterInitI2c();
  Serial.begin(9600);
}

void loop(){
  Serial.println("Starting Program");
  
  char inputValues[5]; 
  
  if(Serial.available() > 0){
    for(int i = 0; i < 5; i++){
     inputValues[i] = Serial.read();
    }
   long inputValueFinal = atol(inputValues);
   
    Serial.println(inputValueFinal);
    
    long Cstate = inputValueFinal;
    
    
    if (Cstate >= 10000 && Cstate <= 19999){
      zoCommandVelocityMove(NodeID,5000);
      delay(3000);
    }
    
    else if(Cstate >= 20000 && Cstate <= 29999){
      int i = Cstate - 20000;
      zoCommandRelativePositionMove(NodeID,i);
      delay(3300);
    }
    
    else if(Cstate >= 30000 && Cstate <= 39999){
      int i = Cstate - 30000;
        zoCommandAbsolutePositionMove(NodeID,i);
        delay(3300);
      }
    }
}

Try some more thorough troubleshooting to track this down...

Write a Serial.print statement in the setup function. Think about the results.
Is your Serial client configured to listen properly to Arduino serial output?

    if (Cstate >= 10000 && Cstate <= 19999){
      zoCommandVelocityMove(NodeID,5000);
      delay(3000);
    }

    else if(Cstate >= 20000 && Cstate <= 29999){
      int i = Cstate - 20000;
      zoCommandRelativePositionMove(NodeID,i);
      delay(3300);
    }

    else if(Cstate >= 30000 && Cstate <= 39999){
      int i = Cstate - 30000;
        zoCommandAbsolutePositionMove(NodeID,i);
        delay(3300);
      }
    }

When I commented that out, it started my "Start Program" print in the beginning. But as soon as I uncommented that block, it started hanging, and didnt send anything at all.

So add some more prints, track that bug down, and squash it!

I debugged, and its the If function definetly. I just now do not know why it is that it stops it completly

its the If function

But which "if" statement?

Any of them, I commented out all but the first if statement, and it didnt allow for the loop function to proceed

I would encourage you to think about this a little more systematically. It's being suggested that you use the serial.print and comment tokens as tools to start tracking this down precisely on your own since there's nothing glaringly wrong.

Think about your last post too... You have proven that the first if statement causes the problem. You haven't proven that the 2nd and 3rd if statements don't cause the problem.

Regardless, you've narrowed down the problem to within an if block. So keep pressing. Find out which statement within the if block is the issue. Until you have more/different information I suspect you'll mostly get a "keep digging" response here.

Hope I don't sound too lecturing. Debugging is it's own skill but it isn't difficult. Follow the logic and you'll find the problem.

Hey there

I find that when making a program that I now always make a function called "StopProgram".

It usually looks something like this:

void StopProgram(int A_variable) {

Serial.print(A_variable); // to see what a variable is doing at a certain point...

for(;;); //Program will stop here
}

The advantage of using this is that you can debug your program one line at a time.

Also, if you are having trouble with:

    if (Cstate >= 10000 && Cstate <= 19999){
      zoCommandVelocityMove(NodeID,5000);
      delay(3000);
    }

    else if(Cstate >= 20000 && Cstate <= 29999){
      int i = Cstate - 20000;
      zoCommandRelativePositionMove(NodeID,i);
      delay(3300);
    }

    else if(Cstate >= 30000 && Cstate <= 39999){
      int i = Cstate - 30000;
        zoCommandAbsolutePositionMove(NodeID,i);
        delay(3300);
      }
    }

... Maybe you could try:

    if (Cstate >= 10000 && Cstate <= 19999){
      zoCommandVelocityMove(NodeID,5000); 
      delay(3000);
    }

    else {
      Long i = Cstate % 10000;    //<-----See below
      zoCommandRelativePositionMove(NodeID,i);
      delay(3300);
    }

------> because Cstate is not an integer: It is a "Long".

Hope all goes well :slight_smile: