//***********************************************************************
// MPRO1152 - LAB 14.2 - Arduino.2 - DO YOU HAVE A PULSE?
//
// Initial: November 15, 2023 - by V Paraunda
//
// Program Description: Controlling Pulse Width and Serial Output (with modifications)
//***********************************************************************
// setting up global variables:
int iDuty, iCycle = 0, iInput;
bool bIncrease = true;
//***********************************************************************
//***********************************************************************
void setup()
{
//initializing baud rate:
Serial.begin(115200);
//initializing pin modes:
pinMode(11, OUTPUT); //arduino pin 11 is set as an OUTPUT
pinMode(13, OUTPUT); //arduino pin 13 is set as an OUTPUT
digitalWrite(13, LOW); //led is initially OFF
//Arduino Main Serial Output
Serial.println("Lab 13 Arduino.1 by V");
//flashing LED 10 times (indicating start of program):
delay(100);
LED_Flash(9);
Serial.println("");
Serial.println("Enter brightness level: 0 (OFF) -> 255 (full ON)");
Serial.println("");
Serial.println("'0' -> LED OFF");
Serial.println("'1' -> setting 1: 50/255");
Serial.println("'2' -> setting 2: 100/255");
Serial.println("'3' -> setting 3: 150/255");
Serial.println("'4' -> setting 4: 200/255");
Serial.println("'5' -> setting 5: 255/255");
}//end of setup()
//***********************************************************************
void loop()
{
delay(50);
if (Serial.available() > 0) //checks the serial buffer
{
iInput = Serial.read(); // Read the incoming data
//Serial.println(cChar, DEC);
Serial.println("");
switch(iInput)
{
case '0': Serial.println("LED OFF"); LED_Var(0); break;
case '1': Serial.println("20% Duty Cycle"); LED_Var(50); break;
case '2': Serial.println("40% Duty Cycle"); LED_Var(100); break;
case '3': Serial.println("60% Duty Cycle"); LED_Var(150); break;
case '4': Serial.println("80% Duty Cycle"); LED_Var(200); break;
case '5': Serial.println("100% Duty Cycle"); LED_Var(255); break;
//check if 'q' is entered
case 'q':
{
Serial.println("Program suspended, press 'r' to restart");
// Enter an endless loop until 'r' is entered
while (1)
{
if (Serial.available() > 0) //checks the serial buffer again
{
iInput = Serial.read();
if (iInput == 'r')
{
Serial.println("Restarting...");
LED_Var(255);
break; // Exit the endless loop
}//end of if('r')
}//end of if(Serial.available part 2)
}//end of while(1)
}//end of case('q')
}//end of switch()
} //end of overall if(Serial.available())
}//end of loop()
//***********************************************************************
//***********************************************************************
void LED_Flash (int iFlash)
{
for(int iFlash2 = 0; iFlash2 <= iFlash; iFlash2++)
{
digitalWrite(13, HIGH);
delay(400);
digitalWrite(13, LOW);
delay(500);
}
}//end of LED_Flash
//***********************************************************************
//***********************************************************************
void LED_Var(int iDCycle)
{
analogWrite(11,iDCycle);
}//end of LED_var
//***********************************************************************