Programming from the arduino IDE

I am using a SV-328 and have been going through the “Programming Orangutans and the 3pi Robot from the Arduino Environment” section of the site. I am using arduino 0021 with pololu library 080929.

I have gotten the blink program to function without any trouble, I have also used other simple examples from other sources and those work with modifying only pin numbers, but when I get to more complex examples which make use of orangutan libraries, I have issues.

I am attempting to compile “OrangutanAnalogExample” and I get this output.

OrangutanAnalogExample.cpp.o: In function `__static_initialization_and_destruction_0':
C:\DOCUME~1\Michael\LOCALS~1\Temp\build8280955802922569218.tmp/OrangutanAnalogExample.cpp:21: undefined reference to `OrangutanLEDs::OrangutanLEDs()'
C:\DOCUME~1\Michael\LOCALS~1\Temp\build8280955802922569218.tmp/OrangutanAnalogExample.cpp:22: undefined reference to `OrangutanAnalog::OrangutanAnalog()'
OrangutanAnalogExample.cpp.o: In function `setup':
C:\DOCUME~1\Michael\LOCALS~1\Temp\build8280955802922569218.tmp/OrangutanAnalogExample.cpp:34: undefined reference to `OrangutanAnalog::startConversion(unsigned char, unsigned char)'
OrangutanAnalogExample.cpp.o: In function `loop':
C:\DOCUME~1\Michael\LOCALS~1\Temp\build8280955802922569218.tmp/OrangutanAnalogExample.cpp:41: undefined reference to `OrangutanAnalog::conversionResult()'
C:\DOCUME~1\Michael\LOCALS~1\Temp\build8280955802922569218.tmp/OrangutanAnalogExample.cpp:42: undefined reference to `OrangutanAnalog::startConversion(unsigned char, unsigned char)'

I assume I have incorrectly installed the libraries as instructed at step 5 (pololu.com/docs/0J17/5), but I am not sure where I went wrong.

When I pull up “sketch>import library” all the libraries appear in the menu, but when I compile the code, I get those errors shown above.

Would it be worthwhile to revert to arduino0012? I noticed that the file paths for 0021 are slightly different, when compared to the instructions at pololu.com/docs/0J17/5

Hello.

Unfortunately, as you can see from the version of the Arduino IDE that was current when that guide was written, the Arduino Orangutan Libraries are rather out of date, especially compared with the Pololu AVR libraries. Is there any particular reason why you want to use the Arduino environment? I can help you try to get it working, but at this point we strongly recommend using the Pololu AVR library with AVR Studio.

- Ben

Hi Ben,

I have a lot of code and examples written for the arduino IDE.

It would save me a lot of work if I could copy and paste it without having to make a large amount of modifications.

For example I am currently interested in getting the SV-328 to use an SHT15 humidity sensor for a project, and someone else has already posted code which will interface with the sensor.

I could not get it to compile in AVR studio, so I went about learning how to use the arduino IDE with the sv-328.

Is there a way to get AVR studio to compile code written for arduino?

The code I am currently playing with, used to interface to the SHT15 sensor, is below. I have plans to make it do more, but this is just for pulling values from the sensor.

int dataPin = 0;
int sckPin = 5;

void resetSHT()
{
  pinMode(dataPin,OUTPUT);
  pinMode(sckPin,OUTPUT);

  shiftOut(dataPin, sckPin, LSBFIRST, 255);
  shiftOut(dataPin, sckPin, LSBFIRST, 255);
  
  digitalWrite(dataPin,HIGH);
  for(int i = 0; i < 15; i++){
     digitalWrite(sckPin, LOW);
     digitalWrite(sckPin, HIGH);
  }
}

//Specific SHT start command
void startSHT()
{
  pinMode(sckPin,OUTPUT);
  pinMode(dataPin,OUTPUT);
  digitalWrite(dataPin,HIGH);
  digitalWrite(sckPin,HIGH);
  digitalWrite(dataPin,LOW);
  digitalWrite(sckPin,LOW);
  digitalWrite(sckPin,HIGH);
  digitalWrite(dataPin,HIGH);
  digitalWrite(sckPin,LOW);
}

void writeByteSHT(byte data)
{ 
  pinMode(sckPin,OUTPUT);
  pinMode(dataPin,OUTPUT);  
  
//  digitalWrite(dataPin,LOW);
  shiftOut(dataPin,sckPin,MSBFIRST,data);
  
  pinMode(dataPin,INPUT);

  //Wait for SHT15 to acknowledge by pulling line low
  while(digitalRead(dataPin) == 1);
  
  digitalWrite(sckPin,HIGH);
  digitalWrite(sckPin,LOW);  //Falling edge of 9th clock
  
  //wait for SHT to release line
  while(digitalRead(dataPin) == 0 );
 
  //wait for SHT to pull data line low to signal measurement completion
  //This can take up to 210ms for 14 bit measurments
  int i = 0;
  while(digitalRead(dataPin) == 1 )
  {
    i++;
    if (i == 255) break;
    
    delay(10);
  } 
  
  //debug
  i *= 10;
  Serial.print("Response time = ");
  Serial.println(i);
}

//Read 16 bits from the SHT sensor
int readByte16SHT()
{
  int cwt = 0;
  unsigned int bitmask = 32768;
  int temp;
  
  pinMode(dataPin,INPUT);
  pinMode(sckPin,OUTPUT);
  
  digitalWrite(sckPin,LOW);
  
  for(int i = 0; i < 17; i++) {
    if(i != 8) {
      digitalWrite(sckPin,HIGH);
      temp = digitalRead(dataPin);
//      Serial.print(temp,BIN);
      cwt = cwt + bitmask * temp;
      digitalWrite(sckPin,LOW);
      bitmask=bitmask/2;
    }
    else {
      pinMode(dataPin,OUTPUT);
      digitalWrite(dataPin,LOW);
      digitalWrite(sckPin,HIGH);
      digitalWrite(sckPin,LOW);
      pinMode(dataPin,INPUT); 
    }
  }
  
  //leave clock high??
  digitalWrite(sckPin,HIGH);
  
//  Serial.println();
  
  return cwt;
}

int getTempSHT()
{
  startSHT();
  writeByteSHT(B0000011);
  return readByte16SHT();
}

int getHumidSHT()
{
  startSHT();
  writeByteSHT(B00000101);
  return readByte16SHT();
}


void setup() {
  pinMode(dataPin,OUTPUT);
  pinMode(sckPin,OUTPUT);

  Serial.begin(9600);        // connect to the serial port
  
  Serial.println("Resetting SHT...");
  resetSHT();
}

void loop () {
  delay(2000);
  Serial.println("Starting Temperature/Humidity reading...");
  int temp = getTempSHT();
  Serial.print("Temprature:");
  Serial.println(temp);

  temp = getHumidSHT();
  Serial.print("Humidity:");
  Serial.println(temp);  
}

Arduino code is just standard C++ that has some additional post-processing and a few predefined functions such as pinMode(), digitalWrite(), and shiftOut(). You can get the code you posted to work in AVR Studio with the Pololu AVR library if you replace these function calls with the Pololu AVR library versions. For example, the Arduino shiftOut() function can be rewritten as:

#define LSBFIRST 0
#define MSBFIRST 1

void shiftOut(unsigned char dataPin, unsigned char clockPin, unsigned char bitOrder, unsigned char val)
{
  unsigned char i;

  for (i = 0; i < 8; i++)
  {
    if (bitOrder == LSBFIRST)
      set_digital_output(dataPin, val & (1 << i));
    else	
      set_digital_output(dataPin, val & (1 << (7 - i)));
			
    set_digital_output(clockPin, HIGH);
    set_digital_output(clockPin, LOW);		
  }
}

The digital I/O functions in the Pololu AVR library are set_digital_output(pin, state), set_digital_input(pin, state), and is_digital_input_high(pin), and they are used in almost the same way as the Arduino digital I/O functions, so the conversion is pretty easy. You can find out more about these functions in the command reference. Note that our digital I/O functions compile to two instructions (which execute in 100 ns on the Orangutans) when the arguments are constants, which allows them to be significantly more efficient than their Arduino counterparts.

Therefore, your first function would become:

void resetSHT()
{
  set_digital_output(dataPin, LOW);
  set_digital_output(sckPin, LOW);

  shiftOut(dataPin, sckPin, LSBFIRST, 255);
  shiftOut(dataPin, sckPin, LSBFIRST, 255);

  set_digital_output(dataPin, HIGH);
  for (unsigned char i = 0; i < 15; i++)
  {
    set_digital_output(sckPin, LOW);
    set_digital_output(sckPin, HIGH);
  }
}

Your pin definitions should be of the form IO_Xn. For example, if you want to use pin PC0 as the dataPin, you would put at the top of your code:

#define dataPin IO_C0

I do have one concern about the Arduino shiftOut() code I modified above, however. There seem to be no explicit delays, which means the timing of the signal is entirely determined by the execution time of the digitalWrite() functions, and these functions have now been replaced by much faster set_digital_output() functions. If you end up having problems talking to the sensor, you might want to add some delay_us() calls to decrease the frequency of the software SPI clock. This warning would also apply to the extra 15 bits shifted by the resetSHT() function (why does it do this?).

- Ben

I appreciate the detailed reply.

Good news on reverting to an earlier version of arduino, though. I reverted to 0014 and everything works perfectly with the exception of the analog library.

I compiled OrangutanAnalogExample2 without any issue, but the read outs seem faulty.

It should list the value from the pot and the temperature. The temp reads 456 F and the pot is reading 5000mV and neither of these respond to adjustment.

OrangutanAnalogExample would not compile and returned this error.

o: In function `loop':
C:\DOCUME~1\Michael\LOCALS~1\Temp\build2316.tmp/Temporary_639_8407.cpp:41: undefined reference to `OrangutanAnalog::conversionResult()'


Couldn't determine program size: C:\arduino-0014\hardware/tools/avr/bin/avr-size: 'C:\DOCUME~1\Michael\LOCALS~1\Temp\build2316.tmp\OrangutanAnalogExample.hex': No such file

Any thoughts on what might be causing this? If there is no easy solution, I can work around this, but I thought I would ask.

Many thanks

mmsayre,

I tried to reproduce your problems using arduino-0021 and PololuArduinoLibraries-080929.zip.

  1. In OrangutanLEDs.cpp I had to change PD1 and PD7 to PORTD1 and PORTD7.

  2. In OrangutanAnalog.cpp, I had to remove the word “inline” from this line:

inline unsigned int OrangutanAnalog::conversionResult()

After both of these changes, I successfully compiled OrangutanAnalogExample in arduino-0021. I don’t know why you got so many more errors than I did when using arduino-0021. Maybe you should try downloading the arduino environment and the Pololu libraries again.

For arduino-0014, I think the error message you are getting will be solved if you do #2.

–David

First of all, I want to thank you guys not only for the great products you offer but also for the awesome docs posted on the site. I am a beginner and I was very excited to see that the Orangutan boards can be programmed using the Arduino IDE and to find all the documentation needed to do so.

So I bought a Baby Orangutan, was able to run the blink LED program, then with the help of this thread I was able to run the first analog example (thanks a lot to mmsayre for starting this thread and DavidEGrayson for offering the solution).

I know the Baby Orangutan doesn’t have a buzzer but I had one around so I connected it to PB2 (pin10) as it is connected to the other Orangutan boards and I tried to run the first buzzer example but I got a lot of errors. I got rid of all the references to the LCD (since the Baby doesn’t have one), change PB2 with PORTB2 in OrangutanBuzzer.cpp (same as David did for OrangutanLEDs.cpp) but I still get some errors I don’t know how to fix. I would appreciate any help I can get. Thanks a lot in advance!

The errors:

In file included from OrangutanBuzzerExample1NoLCD.cpp:22:
C:\arduino-0021\hardware\arduino\cores\arduino/WProgram.h:54: error: expected unqualified-id before numeric constant
C:\arduino-0021\hardware\arduino\cores\arduino/WProgram.h:54: error: expected `)’ before numeric constant

I forgot to mention that I am using arduino 0021 with pololu library 080929, same as the OP.

Hello claudiuo,

You can get that example to compile by commenting out this line in OrangutanBuzzer.h:

#define A4				A(4)			// center of the Equal-Tempered Scale

(Recent Arduino environments define A0 through A7 to refer to analog pins in WProgram.h, and our definition of A4 as a musical note conflicts with theirs.)

I am working on updating our libraries to work with the latest Arduino environment, so please let me know if you run into any other problems.

- Kevin

Kevin, you are awesome! Thank you so much! Can’t wait to give it a try tonight. Thanks again.