MCP4231 Digital Potentiometer Circuit

I have been trying to use a MCP4231 digital Pot with Arduino. I found the following example on the Internet. How to Build a Dual Digital Potentiometer Circuit with a MCP4231
I have built the example circuit, and tried to follow the code exactly. My first attempt got the code to run without error messages, but the circuit did not perform correctly. My recent attempt has the code failing with an error message. An error message appears on the second time digitalPotWrite Red(i) comes up in the code. Below is the code copied from the example on the Internet. Is the code screwed up or am I missing something?

#include <SpI.h>
byte addressRedLED = B00010000;
byte addressGreenLED = B00000000;
int CS=10;
void setup()
{
pinMode (CS,OUTPUT); // put your setup code here, to run once:
SPI.begin();
}
void loop()
{
for(int i=0;i<=128;i++) // put your main code here, to run repeatedly:
{
int j=128-i;
digitalPotWriteRed(i);
digitalPotWriteGreen(j);
delay(10);
}
delay(2000);
for(int i=128;i>= 0;i--)
{
int j=128-i;
digitalPotWriteRED(i);
digitalPotWriteGreen(j);
delay(10);
}
}
int digitalPotWriteRed(int valueRed)
{
digitalWrite(CS,LOW);
SPI.transfer(addressRedLED);
SPI.transfer(valueRed);
digitalWrite(CS,HIGH);
}
int digitalPotWriteGreen(int valueGreen)
{
digitalWrite(CS,LOW);
SPI.transfer(addressGreenLED);
SPI.transfer(valueGreen);
digitalWrite(CS,HIGH);
}

The error message is below the delay(2000) on the code line that says:
digitalPotWriteRed(i)
Below is a copy of the error message. I have no idea why it was not declared in the scope!
exit status 1
'digitalPotWriteRED' was not declared in this scope

Please take a moment and read this: How to get the best out of this forum - Using Arduino / Installation & Troubleshooting - Arduino Forum
and then edit your post to include code tags around your code.

Hint: Case matters

Also, the SPI library is SPI.h, not SpI.h [get get away with it on Windows, but not Linux]

blh64 thanks! Will read what you suggested. New to posting on forum and new to Arduino.

You call

But your function is actually named

As @blh64 says case matters.

Note to define that function as returning a variable of type int ( integer) but your function does not return anything so you should use void not int.
The same goes for the green write function.

Thanks Grumpy_Mike. Ever since I started working on this sketch that I found on the Internet, I wondered about it returning a variable, because it did not seem to me it returned anything.
I am so green at coding I don't know where to put your suggestion void not int. Where does that go? Thanks!

Should be
void digitalPotWriteRed(int valueRed)

and that
int digitalPotWriteGreen(int valueGreen)
should be
void digitalPotWriteGreen(int valueGreen)

It sounds like you should put off internet code for a while and focus on all the examples in the IDE (File->examples->...)

Thanks Grumpy_Mike, I will try that and see what happens.

blh64, I copied off How to get the best out of this forum. I am going to read every page closely. Thanks for suggesting that.

blh64, I did what you suggested. I copied the 8 pages of "How to get the best out of this forum".
I still don't understand anything about code tags (</>) after 8 p0ages of reading. I am not a stupid person, just new to Arduino. I will try my best to use what suggestions I understand in the 8 pages I read. Thanks again for trying to help me.

Grumpy_Mike I did what you suggested. I changed <int digitalPot Write Red(int valueRed)>/ changed to <void digitalPotWriteRed(int valueRed)>
I also changed the green write function also. The same error message came up as before. Doing what you suggested changed nothing.
I have read the 8 pages of How to get the best out of this forum, but still don't understand code tags!!!!!! Tried to use them in the above example. If I used them wrong call me stupid or an idiot, or what ever makes you feel good. Thanks for trying to help me.

When this happens you should post your code again in a new reply so we can all check it.

When I did those changes I checked that the code compiles so you should see a change.
I am assuming you changed the capitalisation of the function name like I said in reply #5.

Hi,
A tip for your code.
In the IDE press < CTRL > < T > to auto format your code.
This will auto format your code and indent { and } to make your code easy to follow.

What model Arduino are you using?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Yes to both. How closely did you "copy" it; Cut&Paste or your own sort of transcription?
Case and spelling are super-relevant (e.g. 'RED' is not the same as 'Red').
Since you aren't catching on --

#include <SPI.h>
byte addressRedLED = B00010000;
byte addressGreenLED = B00000000;
int CS=10;

void setup()
{
  pinMode (CS,OUTPUT); // put your setup code here, to run once:
  SPI.begin();
}

void loop()
{
  for(int i=0;i<=128;i++) // put your main code here, to run repeatedly:
  {
    int j=128-i;
    digitalPotWriteRed(i);
    digitalPotWriteGreen(j);
    delay(10);
  }
  delay(2000);
  for(int i=128;i>= 0;i--)
  {
    int j=128-i;
    digitalPotWriteRed(i);
    digitalPotWriteGreen(j);
    delay(10);
  }
}

int digitalPotWriteRed(int valueRed)
{
  digitalWrite(CS,LOW);
  SPI.transfer(addressRedLED);
  SPI.transfer(valueRed);
  digitalWrite(CS,HIGH);
}

int digitalPotWriteGreen(int valueGreen)
{
  digitalWrite(CS,LOW);
  SPI.transfer(addressGreenLED);
  SPI.transfer(valueGreen);
  digitalWrite(CS,HIGH);
}

PE - I went to the link and saw that sketch, did a Copy&Paste and there were no problems, must have been PICNIC.

I will try to answer each person's questions: Grumpy_Mike Yes, I changed the capitalization like you said in reply #5.
TomGeorge I don't know where to find the in the IDE to autoformat my code. I am using Arduino UNO.
runaway_pancake thanks so much for your effort to help, I will try your code.
Thanks to all who have tried to help me. I have battled this thing for days and last night got really frustrated. I am a drafted Marine of the Vietnam War. They used to tell me winners never quit and quitters never win. I will try this again tonight and get back with the results. Thanks again to all.

@KeithHilton I believe this is you, too?
Digital Potentiometers Using SPI Bus - Using Arduino / Programming Questions - Arduino Forum

Yes runaway_pancake it is me also. It is the same project, same code, same digital pot. I continue to fight this thing. I am new to the C language and Arduino. I had some experience many years ago with Parallax and Basic Stamp. Thanks for trying to help me!

How's that Copy & Paste going?

I copied and pasted your code and it works. The problem is it does not work as expected. By that I mean the red light comes on, and stays on longer than the green light. I was expecting the transition from off to on to be equal in both LEDs. The green light simply does a flash, whereas the red light stays on for a time. In reading the code I would think both LEDs would transition on and off at the same rate. There must be a reason in code why the green light is simply flashing when it comes on. I thought there might be a difference in the Green and Red LEDs, so I installed both red LEDs and the same thing happened. One of the Red LEDs stays on for a while when it is it's turn, and the other Red LED quickly flashes when it is it's turn. Transition and time on should be the same. Wonder what is going wrong? Thanks for helping.