Newbee w/SPI problem(s)

Hi,

I have been trying to get a digipot to work and have been running into nothing but trouble. I have two different Arduino boards, two different computers, and two different digipots, and nothing is working.

My boss put together a metro board w/ a mcp4142 digipot and code on his computer and after some tweeking managed to get things working.

I have tried his board, his chip, his code, but nothing will work with my computer. I get flashing lights of the bootloader and other lights showing the code loading, but the resistor doesn’t change value.

I have tried uninstalling Arduino and reinstalling with the latest version, but still no love.

What am I doing wrong?

Here is the code?

#include <SPI.h>  // include the SPI library
const int slaveSelectPin = 10;         //set pin 10 as the slave select for the digital pot
String inputString = "";               // a string to hold incoming data
boolean stringComplete = false;        // whether the string is complete

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  inputString.reserve(200);

  pinMode(slaveSelectPin, OUTPUT);     // set the slaveSelectPin as an output:
  // initialize SPI:
  SPI.begin();                         // initialize SPI:
  digitalWrite(slaveSelectPin, HIGH);  //was not in original code.  The SPI was ignoring first command.
}

void loop() {
  // put your main code here, to run repeatedly:
  if (stringComplete) {
    int commaPos = inputString.indexOf(',');
    String partone = inputString.substring(0, commaPos);
    String parttwo = inputString.substring(commaPos + 1);
    //Serial.print(commaPos);
    //Serial.print("\t");
    //Serial.print(partone);
    //Serial.print("\t");
    //Serial.print(parttwo);

    int addcomm = partone.toInt();
    int value = parttwo.toInt(); //convert partwo to int
    digitalPotWrite(addcomm, value);   //0 in address is write to 00h, or 0000000
    inputString = "";
    stringComplete = false;
  }
}

void serialEvent() {
  while (Serial.available()) {

    char inChar = (char)Serial.read();// get the new byte:

    inputString += inChar;            // add it to the inputString:
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;

    }
  }
}

void digitalPotWrite(int adress, int value) {
  digitalWrite(slaveSelectPin, LOW);
  SPI.transfer(adress);
  SPI.transfer(value);
  digitalWrite(slaveSelectPin, HIGH);
}[code]

[/code]

If you look at the SPI example in the IDE, you will notice a 100msec delay after changing the slaveselectPin state. The chip might require that time to become fully functional.

Also, move your serial reading out of serialEvent() and just put it at the start of loop().

I don't understand what/where to move serialEvent(). According to the serialEvent tutorial this is declared as void serialEvent() {, or as a function after void loop() {}. Could you show me the code as you see it.

Also, I should note that I have had this program working on my bosses computer, but it won't work on mine. I have reloaded/updated Arduino on my computer, but nothing works. I get power to the digipot, a resistance on the digipot, flashing lights on the arduino, but no control of anything. Do you have any ideas as to what is going on?

Did you try adding in the delay as I stated in reply #1?

Also, I did not say move serialEvent(), just the code within the function. Put that code in loop() and you don't need serialEvent().

Did you try adding in the delay as I stated in reply #1?

Also, I did not say move serialEvent(), just the code within the function. Put that code in loop() and you don’t need serialEvent().

@OP

If you follow the above instructions, your sketch will appear like below which you may upload and see the result.

#include <SPI.h>  // include the SPI library
const int slaveSelectPin = 10;         //set pin 10 as the slave select for the digital pot
String inputString = "";               // a string to hold incoming data
boolean stringComplete = false;        // whether the string is complete

void setup()
{
  Serial.begin(9600);
  inputString.reserve(200);
  pinMode(slaveSelectPin, OUTPUT);     // set the slaveSelectPin as an output:
  // initialize SPI:
  SPI.begin();                         // initialize SPI:
  digitalWrite(slaveSelectPin, HIGH);  //was not in original code.  The SPI was ignoring first command.
}

void loop()
{
  //----------------------------------------------
  while (Serial.available())
  {

    char inChar = (char)Serial.read();// get the new byte:

    inputString += inChar;            // add it to the inputString:
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n')
    {
      stringComplete = true;
    }
  }
  //-----------------------------------------------------------------------

  // put your main code here, to run repeatedly:
  if (stringComplete)
  {
    int commaPos = inputString.indexOf(',');
    String partone = inputString.substring(0, commaPos);
    String parttwo = inputString.substring(commaPos + 1);
    //Serial.print(commaPos);
    //Serial.print("\t");
    //Serial.print(partone);
    //Serial.print("\t");
    //Serial.print(parttwo);

    int addcomm = partone.toInt();
    int value = parttwo.toInt(); //convert partwo to int
    digitalPotWrite(addcomm, value);   //0 in address is write to 00h, or 0000000
    inputString = "";
    stringComplete = false;
  }
}


//void serialEvent()
//{

//}

void digitalPotWrite(int adress, int value)
{
  digitalWrite(slaveSelectPin, LOW);
  delay(100);
  SPI.transfer(adress);
  SPI.transfer(value);
  digitalWrite(slaveSelectPin, HIGH);
}