**Found the problem. Thanks for the input.
I have a problem with trying to compare 2 float values with a if (value >=value) statement.
My goal is to compare 2 voltages(one that is from an analog pin, the other is a "set" voltage by 2 buttons) within a hundredth of a volt.
I cannot wrap my head around on how to complete this task and the result of the values not being accurately compared is a nasty loop that slows down my program to 1 complete cycle of void loop() every 5 seconds.
Heres my code:
/* WIGGLES' Universal VMod tool V0.2
This program is used to control up to 6 different voltage rails up to 5V
utilizing a MCP42XXX or MCP41XXX series digital potentiometer. Currently
uses USB Console to view set and read voltages. -Wiggles5289, Overclock.net
ISSUES:
1: NEED TO CONVERT CHxVRead and CHxVtgt from 100ths to without interference
from logic in voltageAdjust
2: Result of Issue 1 is that the program goes at an arduous rate of 1 complete cycle of void loop()
every 5 seconds. adjustVoltage() is the cause of this, it should stop if only voltage values are
compared in 100ths instead of near infinite.
CHANGE LOG:
Disabled printOut() as wrong or no values were being printed on console
Added print function to readVoltage()
This code used to control the digital potentiometer
MCP4XXXX connected to arduino Board
CS >>> D10
SCLK >> D13
DI >>> D11
PA0 TO VCC
PBO TO GND
SHDN >> 9
PW0 TO led with resistor 100ohm. JUST TO TEST.
Thanks to all who helped, Ill be listing people here.
*/
const int buttonUpCH0Pin = 2;
const int buttonDownCH0Pin = 3;
const int buttonUpCH1Pin = 4;
const int buttonDownCH1Pin = 5;
const int sensor0Val = A0;
const int sensor1Val = A1;
int buttonUpCH0State = 0;
int buttonDownCH0State = 0;
int buttonUpCH1State = 0;
int buttonDownCH1State = 0;
int lastButtonUpCH0State = 0;
int lastButtonDownCH0State = 0;
int lastButtonUpCH1State = 0;
int lastButtonDownCH1State = 0;
int CH0VRead = 0;
int CH1VRead = 0;
float CH0Vtgt = 1;
float CH1Vtgt = 1;
#include <SPI.h>
byte addressPot0 = 0b00010001; //To define potentiometer use last two BITS 01= POT 0
byte addressPot1 = 0b00010010; //To define potentiometer use last two BITS 10= POT 1
byte addressPot0and1 = 0b00010011; //To define potentiometer use last two BITS 10= POT 0 and 1
byte addressPotNA = 0b00000000; //To define no write to potentiometer use BITS 5 and 6 or last two BITS= 00
byte CS= 10; //Chip control goes to pin 10
byte SHDN = 9; //Chip SHUTDOWN - PIN 9
byte RS = 8; //Chip RESET - PIN 8
void setup()
{
pinMode (CS, OUTPUT); //CS - When High, sets chip to read the data.
pinMode (SHDN, OUTPUT); //CS - When High, sets chip to read the data.
pinMode (RS, OUTPUT); //CS - When High, sets chip to read the data.
pinMode(buttonUpCH0Pin, INPUT);
pinMode(buttonDownCH0Pin, INPUT);
pinMode(buttonUpCH1Pin, INPUT);
pinMode(buttonDownCH1Pin, INPUT);
Serial.begin(9600);
digitalWrite(SHDN, HIGH); //Power ON (HIGH)
digitalWrite(RS, HIGH); //Power NO RESET (LOW)
SPI.begin();
delay(50); // Increase to 3000 to allow for breakout boards to initialize or external device boot
}
void loop(){
setVoltageCH0(); // Sets voltage via 2 buttons
setVoltageCH1(); // Sest voltage via 2 buttons but will be changed to 1 set and a single "Channel Select" button
readVoltage(); // Reads voltage from Analog pins
voltageAdjust(); // Determines if the voltage needs to be changed based upon read voltage
delay(10); //Delay to allow Pots to adjust voltage feedback.
}
void setVoltageCH0() {
// Sets resistance up or down from 100 to 210 with 2 buttons:
buttonUpCH0State = digitalRead(buttonUpCH0Pin);
if (buttonUpCH0State != lastButtonUpCH0State) {
if (buttonUpCH0State == HIGH) {
Serial.println("UP CH0"); // REMOVE POST DEBUG
if (CH0Vtgt >= 1.00 && CH0Vtgt < 2.10){
CH0Vtgt += 0.01;
}
} else {
Serial.println("UP CH0 Released"); // REMOVE POST DEBUG
}
delay(5);
}
buttonDownCH0State = digitalRead(buttonDownCH0Pin);
if (buttonDownCH0State != lastButtonDownCH0State) {
if (buttonDownCH0State == HIGH) {
Serial.println("DOWN CH0"); // REMOVE POST DEBUG
if (CH0Vtgt > 1.00 && CH0Vtgt <= 2.10){
CH0Vtgt -= 0.01;
}
} else {
Serial.println("Down CH0 Released"); // REMOVE POST DEBUG
}
delay(5);
}
Serial.print("Channel 1 Target: ");
Serial.print(CH0Vtgt);
Serial.print("\t");
lastButtonDownCH0State = buttonDownCH0State;
lastButtonUpCH0State = buttonUpCH0State;
}
void setVoltageCH1() {
// Sets resistance up or down from 100 to 210 with 2 buttons:
buttonUpCH1State = digitalRead(buttonUpCH1Pin);
if (buttonUpCH1State != lastButtonUpCH1State) {
if (buttonUpCH1State == HIGH) {
Serial.println("UP CH1"); // REMOVE POST DEBUG
if (CH1Vtgt >= 1.00 && CH1Vtgt < 2.10){
CH1Vtgt += 0.01;
}
} else {
Serial.println("UP CH1 Released"); // REMOVE POST DEBUG
}
delay(5);
}
buttonDownCH1State = digitalRead(buttonDownCH1Pin);
if (buttonDownCH1State != lastButtonDownCH1State) {
if (buttonDownCH1State == HIGH) {
Serial.println("DOWN CH1"); // REMOVE POST DEBUG
if (CH1Vtgt > 1.00 && CH1Vtgt <= 2.10){
CH1Vtgt -= 0.01;
}
} else {
Serial.println("Down CH1 Released"); // REMOVE POST DEBUG
}
delay(5);
}
Serial.print("Channel 2 Target: ");
Serial.print(CH1Vtgt);
Serial.print("\t");
lastButtonDownCH1State = buttonDownCH1State;
lastButtonUpCH1State = buttonUpCH1State;
}
void readVoltage() {
// Reads voltage from Analog Pins
{
int sensor0Val = analogRead(A0);
float CH0VRead = sensor0Val * (5.0 / 1023.0);
Serial.print("Channel 1 Voltage: ");
Serial.print(CH0VRead);
Serial.print("\t");
}
{
int sensor1Val = analogRead(A1);
float CH1VRead = sensor1Val * (5.0 / 1023.0);
Serial.print("Channel 2 Voltage: ");
Serial.println(CH1VRead);
}
//Duplicate or simplify above code for up to 6 channels
delay(10); //delay between readys for Pot adj CAN BE REMOVED
}
void voltageAdjust(){
// Adjusts Voltage up or down
if (CH0VRead != CH0Vtgt || CH1VRead != CH1Vtgt) {
if (CH0VRead > CH0Vtgt && CH0VRead != CH0Vtgt) {
voltageDecrease(addressPot0);
} else {
voltageIncrease(addressPot0);
}
if (CH1VRead > CH1Vtgt && CH1VRead != CH1Vtgt) {
voltageDecrease(addressPot1);
} else {
voltageIncrease(addressPot1);
}
delay(10); //Delay to allow for voltage feedback
}
}
void voltageDecrease(byte address){
// Decreases Voltage increasing resistance 1/255 away from Terminal A(Pin PAx)
for (int i = 0; i <= 255; i++){
digitalPotWrite(i, address);
delay(10);
}
}
void voltageIncrease(byte address){
// Increases Voltage by decreasing resistance 1/255 towards Terminal A(Pin PAx)
for (int i = 255; i >= 0; i--){
digitalPotWrite(i, address);
delay(10);
}
}
int digitalPotWrite(byte value, byte address)
{ //SPI Write program. Single Chip, 2 Channels.
digitalWrite(CS, LOW); //Set Chip Active
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(CS, HIGH); //Set Chip Inactive
}