Code stuck at SPI.transfer()

Hello all,

Below is my code for controlling a digital potentiometer MCP4131. I set up strings to control "the number to steps" and that will then change the output of the digi-pot.

At the moment, my code is stuck at the SPI.transfer(128); line because after I enter "B0D0" in the serial monitor, the last line that it prints is "before steps"

Can someone help me out please? Thanks!

#include <SPI.h>
#define degrees0    3
#define degrees45   4

String readString;

void setup() {
  Serial.begin(9600);
  pinMode (degrees0, OUTPUT); //Make pin available for 0 degrees element
  pinMode(degrees45, OUTPUT); //Make pin available for 45 degrees element
  digitalWrite(degrees0, LOW);
  digitalWrite(degrees45, LOW);
  Serial.println("Serial with Digi-Pots has begun"); // so I can keep track of when the program starts
}

void loop() {

  while (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == 'n') {
      Serial.println(readString);

// 0 Degrees
      if(readString.indexOf("B0D0") >=0) // Button 0 Degrees #0
      { 
        Serial.println("BEFORE SPI");
        digitalWrite(degrees0, LOW);    //select the pin to allow communication with digi-pot
        Serial.println("before steps");
        SPI.transfer(128);              //transfer the "number of steps" to the digi-pot
        Serial.println("after steps");
        digitalWrite(degrees0, HIGH);   //deselect the pin to end communication
        Serial.println("0 Degrees Element is FULLY ON");
        break;
        }
      if(readString.indexOf("B0D1") >=0) // Button 0 Degrees #1
      { 
        digitalWrite(degrees0, LOW);
        SPI.transfer(64);
        digitalWrite(degrees0, HIGH);
        Serial.println("0 Degrees Element is HALF-ON"); 
        break;
        }       
      if(readString.indexOf("B0D2") >=0) // Button 0 Degrees #2
      { 
        digitalWrite(degrees0, LOW);
        SPI.transfer(0);
        digitalWrite(degrees0, HIGH);
        Serial.println("0 Degrees Element is OFF"); 
        break;
        }
      readString=""; //clears variable for new input
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

You don't seem to have SPI.begin() anywhere.

...R

For a start, you need an SPI.begin in setup. And those break statements take you out of the while loop, which means the String doesn't get emptied. Maybe you should comment out all the SPI stuff and make sure you can enter and recognize commands, first.

Cheers,
/dev

Thank you both!
I added SPI.begin(); (rookie mistake...) and also readString = ""; to clear what was in the string. (would this also end the if statement?)

I've also noticed that the 'L' LED (Pin13/SCK) on the Uno is not on after I added the SPI.begin(); and that SPI.transfer(128) does not make the digi-pot fully output 5V (my input is 5V).

Can y'all explain why this is happening? Thanks!

mclaren12c:
Thank you both!
I added SPI.begin(); (rookie mistake...) and also readString = ""; to clear what was in the string. (would this also end the if statement?)

I've also noticed that the 'L' LED (Pin13/SCK) on the Uno is not on after I added the SPI.begin(); and that SPI.transfer(128) does not make the digi-pot fully output 5V (my input is 5V).

Can y'all explain why this is happening? Thanks!

bump

mclaren12c:
bump

What is the Idle state for SCK (clock signal of the SPI interface, depending on the which SPI MODE you choose, It is either High or Low)? If the clock Idle Level is Low you will never see the flicker of the LED.

If you use the default SPI configurations, the SCK signal is changing at 4mhz, way above your visual range.

Chuck.

mclaren12c:
bump

Post your latest code.
Tell us what it actually does and tell us what you want it to do.

...R

Robin2:
Post your latest code.
Tell us what it actually does and tell us what you want it to do.

...R

I apologize, I have been working on the Java portion of my project.
My project is for controlling 8 digital potentiometers, by transferring the number of steps, (SPI.transfer(128), SPI.transfer(64), etc.), it will vary the output voltage.
My updated code is now:

#include <SPI.h>
#define test 8
#define degrees0    3
#define degrees45   4
//#define degrees90   
//#define degrees135   
//#define degrees180   
//#define degrees225   
//#define degrees270   
//#define degrees315   

String readString;

void setup() {
  SPI.begin();
  Serial.begin(9600);
  pinMode (test, OUTPUT);
  pinMode (degrees0, OUTPUT); //Make pin available for 0 degrees element
  pinMode(degrees45, OUTPUT); //Make pin available for 45 degrees element
  digitalWrite(test, LOW);
  digitalWrite(degrees0, HIGH);
  digitalWrite(degrees45, HIGH);

  Serial.println("Serial with Digi-Pots has begun"); // so I can keep track of when the program starts
}

void loop() {

  while (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == '\n') {
      Serial.println(readString); //prints string to serial port out
      }
      else {     
      readString += c; //makes the string readString
      }
    }
  
    
      if(readString.indexOf("T0D0") >=0) // Button 0 Degrees #0
      { 
        Serial.println("BEFORE SPI");
        digitalWrite(degrees0, LOW);    //select the pin to allow communication with digi-pot
        Serial.println("before steps");
        SPI.transfer(128);        //transfer the "number of steps" to the digi-pot
        //delay(1000);
        Serial.println("after steps");
        digitalWrite(degrees0, HIGH);   //deselect the pin to end communication
        Serial.println("0 Degrees Element is FULLY ON");
        Serial.println(readString);
        delay(1000);
        readString = "";
        }
      if(readString.indexOf("T0D1") >=0) // Button 0 Degrees #1
      { 
        digitalWrite(degrees0, LOW);
        SPI.transfer(64);
        digitalWrite(degrees0, HIGH);
        Serial.println("0 Degrees Element is HALF-ON"); 
        readString = "";
        }       
      if(readString.indexOf("T0D2") >=0) // Button 0 Degrees #2
      { 
        digitalWrite(degrees0, LOW);
        SPI.transfer(0);
        digitalWrite(degrees0, HIGH);
        Serial.println("0 Degrees Element is OFF");
        readString = ""; 
        }


      //readString=""; //clears variable for new input
}

To test, I should see a change in the LED's brightness depending on the number of steps. However, it doesn't seem to change anything. (I tried using different digi-pots and still no change)

Does anyone have any advice on why it is not outputting anything? Thanks!

Post a link to the datasheet for your digital potentiometers.
Perhaps there is some demo code for the pots? Have you tried it?

Have you measured the resistance of the pot with a multimeter?

...R

Robin2:
Post a link to the datasheet for your digital potentiometers.
Perhaps there is some demo code for the pots? Have you tried it?

Have you measured the resistance of the pot with a multimeter?

...R

Hello! I actually was able to change output. I had to add SPI.transfer(0); before what I actually want to set it to!