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
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.
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.
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".