Beginner questions debugging a program.

I am using:

Wild Thumper controller, Arduino compatible,

Arduino 1.0 environment, set up for above,

Building off of the sample code for the board.

I had a lot of code that was a mess, so I broke it into several functions to clean it up. Further, I broke related functions into seperate .h files. I still have one set of functions to complete, but this problem doesn’t have anything to do with that part.

After cleaning up several basic errors, I am coming up with this error in my first .h file:

“‘digitalRead’ was not declared in this scope”

for this line:

AConMOD = digitalRead(MODEinA);

is digitalRead part of something that I need to #include, or is this a problem with using it in a .h file? I used it in comparison to how analogRead was used in the supplied code and in the example code for the digital IR reflectance sensor (though in this case all I’m reading is a switch on a digital pin.)

Also, I would like direction setting up things for proper debugging. I only have a few months experence with C, and other than logically following a program, don’t have any experience debugging.

(I have a long list of other errors, but this is a fundamental that should clean others up).

update:

Went through and piled everthing back into the main file. Looks like I need to start fresh again, as I’m getting errors with the origional example code now, such as:

Line .cpp: In function ‘void setup()’:

Line :183: error: ‘Servo0’ was not declared in this scope

Line :183: error: ‘DropServo’ was not declared in this scope

Line :195: error: ‘DServoDrop’ was not declared in this scope

Line :199: error: ‘Charger’ was not declared in this scope

Line :202: error: ‘Cmode’ was not declared in this scope

Line :204: error: ‘Brate’ was not declared in this scope

Line .cpp: In function ‘void loop()’:

Line :214: error: ‘Battery’ was not declared in this scope

Line :215: error: ‘LmotorC’ was not declared in this scope

Line :216: error: ‘RmotorC’ was not declared in this scope

Line :222: error: ‘Leftmaxamps’ was not declared in this scope

Line :224: error: ‘LmotorA’ was not declared in this scope

Line :225: error: ‘LmotorB’ was not declared in this scope

Line :226: error: ‘RmotorA’ was not declared in this scope

Line :227: error: ‘RmotorB’ was not declared in this scope

Line :232: error: ‘Rightmaxamps’ was not declared in this scope

Line :234: error: ‘LmotorA’ was not declared in this scope

Line :235: error: ‘LmotorB’ was not declared in this scope

Line :236: error: ‘RmotorA’ was not declared in this scope

Line :237: error: ‘RmotorB’ was not declared in this scope

Line :242: error: ‘lowvolt’ was not declared in this scope

Line :253: error: ‘Charger’ was not declared in this scope

Line :266: error: ‘batvolt’ was not declared in this scope

Line :268: error: ‘chargetimeout’ was not declared in this scope

Line :271: error: ‘Charger’ was not declared in this scope

Line :280: error: ‘Cmode’ was not declared in this scope

Line :299: error: ‘overloadtime’ was not declared in this scope

Line :304: error: ‘LmotorA’ was not declared in this scope

Line :305: error: ‘LmotorB’ was not declared in this scope

Line :319: error: ‘overloadtime’ was not declared in this scope

Line :324: error: ‘RmotorA’ was not declared in this scope

Line :325: error: ‘RmotorB’ was not declared in this scope

Line :342: error: ‘LmotorA’ was not declared in this scope

Line :343: error: ‘LmotorB’ was not declared in this scope

Line :344: error: ‘RmotorA’ was not declared in this scope

Line :345: error: ‘RmotorB’ was not declared in this scope

Line .cpp: In function ‘void RCmode()’:

Line :354: error: ‘RCleft’ was not declared in this scope

Line :355: error: ‘RCright’ was not declared in this scope

Line :361: error: ‘RCdeadband’ was not declared in this scope

Line :362: error: ‘RCdeadband’ was not declared in this scope

Line :364: error: ‘Mix’ was not declared in this scope

Line :383: error: ‘Leftcenter’ was not declared in this scope

Line :383: error: ‘RCdeadband’ was not declared in this scope

Line :384: error: ‘Rightcenter’ was not declared in this scope

Line :384: error: ‘RCdeadband’ was not declared in this scope

Line :386: error: ‘Leftcenter’ was not declared in this scope

Line :386: error: ‘scale’ was not declared in this scope

Line :389: error: ‘Rightcenter’ was not declared in this scope

…etc.

so something I did in the headder of the file invalidated everything.

Arduino has some bizzare gotcha’s when it comes to include files. Although I am not an expert, from what I understand, the compiler is not traditional in that it compiles your whole program in one step and not piecemeal and then links it together. This means that the code for various ‘C’ files in your project are simply concatenated together. This means that H files need to be included once from the top of the main program (the pde that bears the name of the project). Makes a nice OO design a bit ackward, but keeping the concatenation thing in mind will help you solve the issue. This can be solved with external libraries, but sometimes that is just as ackward.

fll-freak:
Arduino has some bizzare gotcha’s when it comes to include files. Although I am not an expert, from what I understand, the compiler is not traditional in that it compiles your whole program in one step and not piecemeal and then links it together. This means that the code for various ‘C’ files in your project are simply concatenated together. This means that H files need to be included once from the top of the main program (the pde that bears the name of the project). Makes a nice OO design a bit ackward, but keeping the concatenation thing in mind will help you solve the issue. This can be solved with external libraries, but sometimes that is just as ackward.

Thanks for the reply so quick!

So does this mean that the #includes have to be the immediate first lines in the program? That’s all I can think of that is different between the origional and what I did, that I left a lot of comments at the beginning before the #includes.

As far as including more files, I figured I was safe since it had 2 .h files already, that I could just add more. Are there rules to this? Currently I’m in the middle of copying all my code without the extra commentary over to a fresh copy of the Thumper sketch to see what happens, but later I might break them all back out (1045 lines of code is hard to keep track of in one file for me, scrolling through it and hopping around. Seperate files were much easier).

Edit:

Thinking about it, one of the errors I had before dumping everything into one file was declarations carrying over from one file to the other. I put #includes in all files for all the others and that helped with the vars, but not the functions, but I did not put the #includes in the first few lines, so… still not figuring out what I did wrong.

Also, does the file extention have to remain .pde? Once I save the program it changes the extention to .ino

.ino is for the new version of the Arduino IDE. That works.

As I said, I am no expert. I had a similar issue with a multi file project I wrote a year ago. I ended up not having any includes in any file but the main project file. I also had to declare any global variables in the main file as well to prevent backward declarations.

There is a fair amount of info on this issue in various forums. What I found the easiest was to write the code as if it were just one file split into slices. All the “extern”, file scope variables all go out the window. And just like you do not normally place an include in the middle of a file, put all the includes at the start of the main/top file/slice.

I think that works. That was my understanding before, but whatever bug I ran into threw me for a loop. Working on it as one file for the moment, stepping though to make sure what I thought was right is still ok. As such, those other issues are not popping up anymore.

Now my problem is:

‘Ivalue required as left operand of assignment’

for the line:

if (LoopValid==1 && CheckLoop=0) 

LoopValid is logical, and CheckLoop is numeric, the lines feeding this are:

  int LastCount = LoopCount;  //  Save old LoopCount to help with rollover
  LoopCount = LoopCount+1;  //  Loop counter if needed, NEEDS WORK, add loop limit/reset, 
  if (LoopCount>LastCount)  //  See if the loop has reset
  {
    int LoopValid=1;
  }
  else
  {
    int LoopValid=0;
  }

and

  int CheckLoop=(LoopCount%LoopDelay);

What I’m trying to do:

The whole program works in a loop. It checks inputs, makes calculations, then writes this to the motor controller section (in the same loop), and I’m trying to do this without delays.

I have one process that has a delay in it that I can’t seem to get around (a digital IR reflectance sensor that charges a capacitor then waits for it to bleed off), but I don’t need to check it every time through the loop.

So I figured I’d set up a loop counter (and I know there’s a better way to index, I just haven’t changed it yet.) and when the counter (LoopCount) is a multiple of a variable (LoopDelay), it would check the DIR.

TrollHammer:
Now my problem is:

‘Ivalue required as left operand of assignment’

for the line:

if (LoopValid==1 && CheckLoop=0) 

Shouldn’t that be …

... && CheckLoop==0)

A classic bug. Know it well. Have seen people “#define EQ ==” to prevent using = instead of == by always using EQ.

(Dumped my post…)

Thanks a lot! that helped bring my errors down to just a few things that I haven’t entered yet and two servo writes (that were ok before… hmmh.)

Maybe I’ll get everything done tomarrow and be able to upload… I hope!

Not strictly needed, but my coding standard requires paraphrases around each section like:

if ( (LoopValid==1) && (CheckLoop==0))

This prevents any confusion with order of precedence. Very few people know the whole list by heart. Much better to have some extra () then screw up something that could take hours or days to track down.

fll-freak:
Not strictly needed, but my coding standard requires paraphrases around each section like:

if ( (LoopValid==1) && (CheckLoop==0))

This prevents any confusion with order of precedence. Very few people know the whole list by heart. Much better to have some extra () then screw up something that could take hours or days to track down.

Just to add to that:

This does not mean extra lines to your output code. Your output code should be the same, and if it isn’t, it is now written how you want it.

That make sense. I have tried to make sure and use ()s anywhere it seemed necessary, but I’ll go back through and check all the ones on ==s (as they may not have hit me as being anything other than highest priority, I only worried about arithmatic and comparisons) I’m sure I missed a few more (and I have a LOT of equasions mixed throughout!) Thanks for the reminder!

In later revisions I’ll try simplifying things and make them more efficient (read something about ‘if’ taking longer to process than other methods. I use a lot of 'if’s and 'switch’es… which is faster?)

The only thing I can think of that’s glaring at me that I’d like to clean up is the digital IR sensor (and the servo writes, but I need to look at it to figure out what I need to ask.)

This sensor: https://www.sparkfun.com/products/9454 is a digital IR reflectance sensor.

It measures the time it takes for the capacitor on board to discharge to determine how much is reflected. I need to detect three states: Reflective tape (3M), Black plastic, and nothing.

This is the sample buildr code for the sensor:

//Code for the QRE1113 Digital board
//Outputs via the serial terminal - Lower numbers mean more reflected
//3000 or more means nothing was reflected.

int QRE1113_Pin = 2; //connected to digital 2

void setup(){
  Serial.begin(9600);
}

void loop(){

  int QRE_Value = readQD();
  Serial.println(QRE_Value); 

}

int readQD(){
  //Returns value from the QRE1113 
  //Lower numbers mean more refleacive
  //More than 3000 means nothing was reflected.
  pinMode( QRE1113_Pin, OUTPUT );
  digitalWrite( QRE1113_Pin, HIGH );
  delayMicroseconds(10);
  pinMode( QRE1113_Pin, INPUT );

  long time = micros();

  //time how long the input is HIGH, but quit after 3ms as nothing happens after that
  while (digitalRead(QRE1113_Pin) == HIGH && micros() - time < 3000);
  int diff = micros() - time;

  return diff;
}

Instead of printing to the serial port, I just read it when I need to see if something is attached and how it is attached (plugged in one way it sees the reflector, the other way it sees black plastic, and when it isn’t plugged in it doesn’t see anything- no reflectance)

Is there any way to speed it up? It looks to me that it will basically stop the loop while it waits for the cap to discharge, which can be variable in time up to 3ms, which isn’t bad I suppose, but as I have only processing delays in the rest of the code it would be nice to trim it up if I can. I plan to shorten the “reads nothing” time after I test it and get the value of black plastic (plus a buffer for variance in dirt, material, etc), but I’d prefer a method that doesn’t stop the program at all. I have a piezo on an analog pin that I’m trying to keep the interval between reads as low as possible so I don’t miss an event. I’d use another analog IR but I’m out of analog inputs (already have 4 analog IRs in use that are higher priority for being fast).

Is there a method that could allow the DIR or the piezo to work as an interrupt, or set a flag or something independant of the program loop? I’m only about halfway through Sam’s and haven’t learned interrupts yet (if Arduinos have them?) and haven’t seen a program that uses one.

I have another quandry, and I’m not sure what the proper terminology for it would be.

Having experience in ladder logic and descrete logic, the best way I can describe it is a “latch”.

I have push button inputs (4), of which two set a mode in the controller. This mode needs to be maintained while the loop cycles even after the button is released until other input conditions are met (IR reads or other button press), so that the branches in the loop are followed the same way each time (other inputs won’t change, or produce an immediate result).

Currently, the method I’m using basically works like this:

Input= 0,1,2,3,4 (four buttons, with 0= no button being pressed, only one button can be pressed at a time).

If Input = 1 or 2, set InputLatch = Input (and if the input = 0, 3, 4, it leaves InputLatch alone)

Each loop:

Check input, set latch if necessary.

Compare InputLatch and branch accordingly.

Branches can reset latch according to conditions.

Actually seems simple and efficent written out this way, but I have about half a dozen latches set up this way, as well as a couple dozen conditionals (so… probably a couple dozen different branches? Seems that I’ve written a pick-your-path book that gets read a few hundred times a second.) Just didn’t know if I’m doing it right.

Ok, last one for the moment. Figured I’d put it here instead of opening a new thread. I was going to try getting sensor reads using my 5v Pro that I’ve never used, and noticed a few things I didn’t see before.

Am I correct in assuming that D0 and D1 of any Arduino compatible system are basically dedicated serial communications (or similar)? Can I wire an RS232 straight to these and ground and hook it to the computer without any additional level translation or other circuitry?

As I haven’t sent a program that changed communications yet, Is there a way I could goof up and not be able to upload new code (somehow overwrite the D0 and D1 pins so the USB on the Thumper or Rx and TX on the Pro) won’t read anything anymore?

What determines how the different communications (SPI, TTL serial, I2C) are selected? I had assumed I could use all the pins that I had access to for analog or digital I/O.

D0 and D1 are by default logic level serial pins. If you want to connect to a PC you will need a logic to 232 level converter unless this was included in your thumper.

If you change the program to a different baud rate, you can still reload code by reseting the board prior to uploading code to it.

At the lowest level, what you put into various registers tells the micro what peripherals are in use connected to what pins. This is made simpler with the Arduino code that does this for you. If you use an SPI library, the pins are set up for SPI. If you use a pinSelect, you get digital IO, etc.

TrollHammer:
Am I correct in assuming that D0 and D1 of any Arduino compatible system are basically dedicated serial communications (or similar)? Can I wire an RS232 straight to these and ground and hook it to the computer without any additional level translation or other circuitry?

You most definitely can NOT connect RS232 levels to those pins. They run at the MCUs Vcc, 5 or 3.3 V. To talk RS232 you’d need a [converter IC. Also note that on the normal (non “Pro”) Arduinos these pins are shared with the onboard USB-serial converter. The onboard device has a direct connection to the Atmega MCU, the D0 and D1 pins have 1k resistors btw the pins and the MCU (pins).](https://www.sparkfun.com/products/589)

Well, I’ve learned quite a bit, answered a lot of my own questions, but sprouted a few new ones.

I have been trying to figure out a way to keep the loop rate high to read the peizo sensor as many times a second as possible, and other than reading it several times a loop, I cant help but occasionally miss an event it would seem, for two reasons, I suspect.

First off, I seem to have a lot of noise coming in on the sensor. It is taped to a piece of plastic, with a 1meg resistor across it, with about 4ft of twisted pair between it and a resistor network (wheatstone bridge so I can get both positive and negative readings, as well as detect when it is plugged in). Im getting upwards of 50-100 points positive and negative swing (0.0049volts per point). The signal I want to get seems to be coming in randomly just more than that. It should be much higher, but by oscope it runs the full range from 0-5v (averaging about 2-3v p-p)

So, its not sampling fast enough, and is noisy. Part of the sample rate issues, I think, is with the digital IR code, in that it has a variable delay.

A solution would be if either the analog piezo input could be used as an interrupt, or if the return on the digital IR could be set up as an interrupt or something.

I looked up software interrupts in my C manuals, and they were sketchy. The only “complete” referance I could find was one for “normal computers”, that returned keystrokes while a loop ran. How much different is Arduino C to normal ANSI C? Would I have access to the same commands and libraries?

Well, down to the wire again and hitting a brick wall.

I have been using serial.print for debugging, btw. Crude, and I cant alwasy get all the info I want without locking up the port, but it works for me for now.

Anyone know of any good simulators? Im running into an issue with unexpected output based on conditions that I cant trace logically or with serial.print markers, and limited by logical limit switches (3). The thing is due monday so I dont have time to start from scratch (now that I have an idea of what Im doing).

Essentially, the motors (WildThumper) are supposed to run one way at a certain speed according to three sets of conditionals

Edit: looks like my post got chopped for some reason.

Anyway, I wanted to elaborate on the question:

Can anyone recommend which simulator to use? I did a search and came up with only two that were free downloads, but Im still not sure if they will handle my board or 1600+ lines of code( 10.5kb compile). I dont have a budget that I can request paid software, and Im sure since even “free” software is still ‘software’ they would have a fit anyway for not going through IS… But anyway…

Just would like something that I could step through my program line by line with a table of variabkes so I can see where my logic errors are.