Hello everybody and thanks for taking a peek here!
I am working on some code for some switch cases, and I am stuck on something that isnt making sense to me at this time.. Ive never used the switch case, but I dont think I should be having this much trouble with it either..
I want to have each switch case output a specific PWM signal to an LED, output a voltage from a digital potentiometer, and read the potentiometer's output voltage from the arduino analog pin A0. It seems to works perfectly fine for all cases 1-10 without any analogWrite functions yet. It also works when case 1 has its analogWrite functions. But as soon as I add an analogWrite() to case 2, it stops displaying feedback on LCD and the LED doesn't light up for case 1 & 2.. The other cases still display LCD feedback though.
This code right here is what it looks like just before I add the analogWrite() to case 2..
while(Serial.available() == 1) {
// while user input is available, complete this switch statement
serialInput= Serial.parseInt();
Serial.end(); // end serial interface with monitor
switch (serialInput) {
case 1:
analogWrite(4, 230); // turns on LED to 90% brightness
SPI.begin(); // begin serial interface with MCP4151
digitalWrite(CS_pin, LOW); // chip select pin is written LOW to enable data transfer;
SPI.transfer(wiper1); // transfer wiper address
SPI.transfer(voltageOutput1); // transfer write value to set wiper to
digitalWrite(CS_pin, HIGH); // write the chip select pin HIGH to end serial transaction
SPI.end();
// print feedback to LCD
lcd.setCursor(0, 0);
lcd.print("voltage feedback" );
lcd.setCursor(0, 1);
lcd.print(analogRead(A0));
delay(LEDduration);
analogWrite(4, 0);
SPI.begin(); // begin serial interface with MCP4151
digitalWrite(CS_pin, LOW); // chip select pin is written LOW to enable data transfer;
SPI.transfer(wiper1); // transfer wiper address
SPI.transfer(voltageOutput10); // transfer write value to set wiper to
digitalWrite(CS_pin, HIGH); // write the chip select pin HIGH to end serial transaction
SPI.end();
break;
case 2:
SPI.begin(); // begin serial interface with MCP4151
digitalWrite(CS_pin, LOW); // chip select pin is written LOW to enable data transfer;
SPI.transfer(wiper1); // transfer wiper address
SPI.transfer(voltageOutput2); // transfer write value to set wiper to
digitalWrite(CS_pin, HIGH); // write the chip select pin HIGH to end serial transaction
SPI.end();
// print feedback to LCD
lcd.setCursor(0, 0);
lcd.print("voltage feedback" );
lcd.setCursor(0, 1);
lcd.print(analogRead(A0) );
delay(LEDduration);
SPI.begin(); // begin serial interface with MCP4151
digitalWrite(CS_pin, LOW); // chip select pin is written LOW to enable data transfer;
SPI.transfer(wiper1); // transfer wiper address
SPI.transfer(NoVoltageOutput); // transfer write value to set wiper to
digitalWrite(CS_pin, HIGH); // write the chip select pin HIGH to end serial transaction
break;
Has anybody else ever dealt with this issue, and can anyone point out something that Im majorly overlooking?