I am a beginner when it comes to electronics, coding and things like that (this is practically the first time I have done anything like this), so when building an application that is supposed to vibrate a vibration motor with a strength based on the distance measurement made by the ultrasonic rangefinder, I bumped up with a problem.
The MCP 41010 digital potentiometer seems to let electricity through only when it is given a SPI.transfer(128). Any other number stops the flow of electricity completely.
I have tried some addresses such as 00, 0x00, 0x01 and 0x04 to replace the 0 and the result was the same. With addresses 0x11 and 0x08 the digital potentiometer didn't react at all.
In addition to the code I have here I have tried just copy-pasting some of the tutorials I found from the internet, for example this one Digital Potentiometer not fading LED without really understanding the commands formats thing all that well. With that code I got a vibration that didn't seem to fade in or fade out at all, so the problem remains (I lack a way to properly test that though).
So... what am I doing wrong and how to fix it?
Thank you!
Many tutorials were used when making the code.
#include <SPI.h> int vcc = 2; // attach pin 2 to vcc int trig = 3; // attach pin 3 to Trig int echo = 4; // attach pin 4 to Echo int gnd = 5; // attach pin 5 to GND int shortest = 250; int longest = 12000; int CS = 10; // for digi pot void setup() { SPI.begin (); pinMode (vcc,OUTPUT); pinMode (gnd,OUTPUT); pinMode (CS, OUTPUT); // for digi pot // initialize serial communication: Serial.begin(9600); } void loop() { digitalWrite(vcc, HIGH); // establish variables for duration of the ping, // and the distance result in inches and centimeters: long duration; // The PING))) is triggered by a HIGH pulse of 2 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: pinMode(trig, OUTPUT); digitalWrite(trig, LOW); delayMicroseconds(2); digitalWrite(trig, HIGH); delayMicroseconds(5); digitalWrite(trig, LOW); // The same pin is used to read the signal from the PING))): a HIGH // pulse whose duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. pinMode(echo,INPUT); duration = pulseIn(echo, HIGH); if (duration > 200 && duration < 220) // Limiting out an unwanted, randomly appearing distance measurement // made useless by later code though { Serial.print("Error "); Serial.print(duration); Serial.print("\n"); return; } // Compressing the result of the distance measurement into the scale of 0 - 128 if (duration < shortest) duration = shortest; if (duration > longest) duration = longest; duration = (1.0f-((float)(duration - shortest) / (float)(longest - shortest))) * 128; digitalWrite(CS, LOW); SPI.transfer(0); // 0x01 and 0x04 seem to "work" too. With 0x11 and 0x08 no voltage goes through SPI.transfer(duration); // sending a number from 0 to 128 to the digital potentiometer digitalWrite(CS, HIGH); Serial.print(duration); // printing the result of the measurement and calculations Serial.print("\n"); delay(100); }