This code is trying to find digipot settings to get an enhanced current limiting diode to work (varying the resistance of a resistor varies the current with this design. The ultimate goal is two arrays holding the step settings for each digipot. I'm using two digipots, one 50 kOhm and one 10 kOhm ("digipot50" and "digipot10") http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en010498
SPIfunction is used for writing values to my devices, it works perfectly.
I is the current per circuit I'm looking for.
This is one point where it's breaking: currentMap50[k]=1.001*currentMap50[k-1];
It doesn't like the rewriting of currentMap50[k] in particular, regardless of what's on the right.
Then it also fails with this condition: while((currentMap50[m]-(i+1)*I) < 0)
Changing that to while (m<60) or similar makes it work.
Curiously, I have those twice in this function but only the first two break it (last night the opposite was the case, I don't know what changed).
With the debugging println("Map") it prints ¥ (with no new line). I'm writing at 9600 and the serial monitor is at 9600 as well.
The final two println don't show up on the serial monitor.
However, the function apparently completes because I can call other functions otherwise and they work.
In addition, I'm using serial commands.
if (stringComplete){
if (serialRead=="map") {
Serial.println("Map");
mapping();
serialRead="";
}
In that case, the debugging line yields different values each time I call map: MØ, -Ø, Ma, M°, again with no new line. Putting the debugging line after mapping() yields no output in the serial monitor. Removing mapping(), I get Map, as expected.
If anyone can help at all, it'd be much appreciated. I can't make any sense out of this whatsoever.
void mapping(){ //connect to B and W; this function determines the digipot settings for 1-6 lighted circuits; it should be I*(number of circuits)
Serial.println("Map");
for (int i=0;i<6;i++){
float currentMap50[256] = { //this holds the currents measured at every setting of digipot50
};
SPIfunction(pow(2,(i+1))-1,cellSwitchPin); //this sets the number of open dot loops
for(int k=0;k<256;k++){ //this loop goes through all the digipot50 settings to fill out currentMap50
SPIfunction(k,digipot50Pin);
currentMap50[k]=currentSense();
if (k>=1) /*idea here is to keep the currentMap monotonically increasing to allow searches to be done and the function is actually monotonical
increasing except for errors*/
if (currentMap50[k] <= currentMap50[k-1])
currentMap50[k]=1.001*currentMap50[k-1]; //this is one of the lines causing problems
}
int m = 0; //this determines which position in currentMap50 is closest to the target current
while((currentMap50[m]-(i+1)*I) < 0) //while the error is decreasing; this condition is the other line causing problems
m++;
if (m > 255) //subtracting 26 (5 kOhm) so that digipot10 finds the precise value
digipot50Set[i]=229;
else if (m<26)
digipot50Set[i]=0;
else
digipot50Set[i]=m-26;
SPIfunction(digipot50Set[i],digipot50Pin);
//then repeat the above
float currentMap10[256] = {
};
for(int n=0;n<256;n++){
SPIfunction(n,digipot10Pin);
currentMap10[n]=currentSense();
if ((currentMap10[n] <= currentMap10[n-1]) && (n >= 1))
currentMap10[n]=1.001*currentMap10[n-1];
}
int p = 0;
while((currentMap10[p]-(i+1)*I)<0){ //while the error is decreasing
p++;
}
if (p > 255)
digipot10Set[i]=255;
else
digipot10Set[i]=p;
}
for(int i=0;i<6;i++)
Serial.println(digipot50Set[i]);
for(int i=0;i<6;i++)
Serial.println(digipot10Set[i]);
}