problems with creating MCP3201 library

Hello,
I want to creat a MCP3201 (ADC 12 bits SPI) library. I wrote thess files but i have compilation errors like MCP3201 class was nt declared.

sketch (just for compilation test) :

#include "MCP3201.h"

//MCP3201 adc(1,2,3,4);

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

header :

// MCP3201.h
// classe destinee a gerer le CAN 12 bits SPI MCP3201
//
#if _MCP3201_H_
#define _MCP3201_H_

#include <Arduino.h>
#include <SPI.h>

class MCP3201
{
private :
  int cs;
public :
  MCP3201(int cs, int clk, int dout, int spi_clk=SPI_CLOCK_DIV4);
  short read();
};

#endif

definitions :

// MCP3201.cpp

#include "MCP3201.h"

// constructeur
MCP3201::MCP3201(int cs, int clk, int dout, int spi_clk)
{
    this->cs = cs ;
    SPI.begin();
    SPI.setBitOrder(MSBFIRST);
    SPI.setClockDivider(spi_clk);
    pinMode(cs, OUTPUT);
    digitalWrite(cs, HIGH);
}

// methode read
short MCP3201::read()
{
    short val;
    byte inByte;
    digitalWrite(cs, LOW);  // activer echange
    val = SPI.transfer(0x00);  // recuperer octet MSB
    val = val << 8;  // decaler pour mettre en MSB
    inByte = SPI.transfer(0x00);  // recuperer octet LSB
    val = val | inByte;  // assembler MSB et LSB
    digitalWrite(cs, HIGH);  // desactiver echange 
    val = val >> 1;    // eliminer lsb
    return val & 0x3FF;  // conserver les 12 bits
}

Errors :

Arduino : 1.6.3 (Windows 7), Carte : "Arduino Uno"

Les options de compilation ont été modifiées, tout sera recompilé



C:\Users\maison\AppData\Roaming\Arduino15\packages\arduino\tools\avr-gcc\4.8.1-arduino2/bin/avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10603 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -IC:\Users\maison\AppData\Roaming\Arduino15\packages\arduino\hardware\avr\1.6.2\cores\arduino -IC:\Users\maison\AppData\Roaming\Arduino15\packages\arduino\hardware\avr\1.6.2\variants\standard C:\Users\maison\AppData\Local\Temp\build2929621838537111412.tmp\class_mcp3201.cpp -o C:\Users\maison\AppData\Local\Temp\build2929621838537111412.tmp\class_mcp3201.cpp.o 

C:\Users\maison\AppData\Roaming\Arduino15\packages\arduino\tools\avr-gcc\4.8.1-arduino2/bin/avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10603 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -IC:\Users\maison\AppData\Roaming\Arduino15\packages\arduino\hardware\avr\1.6.2\cores\arduino -IC:\Users\maison\AppData\Roaming\Arduino15\packages\arduino\hardware\avr\1.6.2\variants\standard C:\Users\maison\AppData\Local\Temp\build2929621838537111412.tmp\MCP3201.cpp -o C:\Users\maison\AppData\Local\Temp\build2929621838537111412.tmp\MCP3201.cpp.o 

C:\Users\maison\AppData\Local\Temp\build2929621838537111412.tmp\MCP3201.cpp:6:1: error: 'MCP3201' does not name a type

 MCP3201::MCP3201(int cs, int clk, int dout, int spi_clk)

 ^

C:\Users\maison\AppData\Local\Temp\build2929621838537111412.tmp\MCP3201.cpp:17:7: error: 'MCP3201' has not been declared

 short MCP3201::read()

       ^

C:\Users\maison\AppData\Local\Temp\build2929621838537111412.tmp\MCP3201.cpp: In function 'short int read()':

C:\Users\maison\AppData\Local\Temp\build2929621838537111412.tmp\MCP3201.cpp:20:5: error: 'byte' was not declared in this scope

     byte inByte;

     ^

C:\Users\maison\AppData\Local\Temp\build2929621838537111412.tmp\MCP3201.cpp:20:10: error: expected ';' before 'inByte'

     byte inByte;

          ^

C:\Users\maison\AppData\Local\Temp\build2929621838537111412.tmp\MCP3201.cpp:21:18: error: 'cs' was not declared in this scope

     digitalWrite(cs, LOW);  // activer echange

                  ^

C:\Users\maison\AppData\Local\Temp\build2929621838537111412.tmp\MCP3201.cpp:21:22: error: 'LOW' was not declared in this scope

     digitalWrite(cs, LOW);  // activer echange

                      ^

C:\Users\maison\AppData\Local\Temp\build2929621838537111412.tmp\MCP3201.cpp:21:25: error: 'digitalWrite' was not declared in this scope

     digitalWrite(cs, LOW);  // activer echange

                         ^

C:\Users\maison\AppData\Local\Temp\build2929621838537111412.tmp\MCP3201.cpp:22:11: error: 'SPI' was not declared in this scope

     val = SPI.transfer(0x00);  // recuperer octet MSB

           ^

C:\Users\maison\AppData\Local\Temp\build2929621838537111412.tmp\MCP3201.cpp:24:5: error: 'inByte' was not declared in this scope

     inByte = SPI.transfer(0x00);  // recuperer octet LSB

     ^

C:\Users\maison\AppData\Local\Temp\build2929621838537111412.tmp\MCP3201.cpp:26:22: error: 'HIGH' was not declared in this scope

     digitalWrite(cs, HIGH);  // desactiver echange 

                      ^

Erreur lors de la compilation.

thanks in advance

All is OK now :slight_smile:

1 - I found the first error in the header file (MCP3201.h)
#if instead of #ifndef

2 - I find documentation about SPI library. It's necessary to include SPI.h in the sketch AND in the definition file (MCP3201.cpp)

In the sketch use

#include <MCP3201.h>

and in the library .cpp file use

#include "MCP3201.h"

The original intention of the different syntaxes is "look in system includes" for <>
and "look in current directory" for "". Having said that people are sloppy and a lot
of systems look in both places, but in different order.