#include <Servo.h>

const int potPin = 0;
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:

int m = 0;
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
char incoming = 0;			//character reciewed from keyboard
//int button = 0;
Servo servo01;				//Servo lable: servo01


void setup() {
	Serial.begin(57600);		//initialize the serial comminication
	pinMode(buttonPin, INPUT);	// initialize the button pin as a input
	pinMode(ledPin, OUTPUT);	// initialize the LED as an output
	servo01.attach(9);			// attaches the servo on pin 11 to the servo object
	menu();						// calls for menu
}

void loop(){
	if (Serial.available() > 0) {
		incoming = Serial.read();		// read the incoming character:
		
		Serial.print("I received: ");	// say what you got:
		Serial.println(incoming);
	}
	else if(incoming == 'r'){			//calls for menu once and resets push button counter
		resetAndMenuOnce();
	}

	switch (incoming){					// runs different function selected by user
	case '1':
		m = 0;
		ledOnOff();
		break;
	case '2':
		m = 0;
		ledBlinkRate();
		break;
	case '3':
		m = 0;
		dimmer();
		break;
	case '4':
		m = 0;
		servoMan();
		break;
	default:
		digitalWrite(ledPin, LOW);
		break;
	}
}

// FUNCTIONS-----------------------------------------------------------------------------

void ledOnOff() {
	//calls for function which returns button counter/state
	int button = bCounter(); 

	// turns on the LED every other button pushes by 
	// checking the modulo of the button push counter
	// the modulo function gives you the remainder of 
	// the division of two numbers:
	if (button % 2 == 0) {
		digitalWrite(ledPin, HIGH);
	} 
	else {
		digitalWrite(ledPin, LOW);
	}

}
void ledBlinkRate(){
	int button = bCounter(); 						//calls for function which returns button counter/state

	int potVal = analogRead(potPin);
	int blinkRate = map(potVal, 0, 1023, 100, 1000);

	if (button % 2 == 0){
		digitalWrite(ledPin, HIGH);
		delay(blinkRate);
		digitalWrite(ledPin, LOW);
		delay(blinkRate);
	}
	else{
		digitalWrite(ledPin, LOW);
	}
}

void servoMan(){
	int button = bCounter();					//calls for function which returns button counter/state
	int potVal = analogRead(potPin);            // reads the value of the potentiometer (value between 0 and 1023) 
	int pos = map(potVal, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
	if (button % 2 == 0){
		digitalWrite(ledPin, HIGH);
		servo01.write(pos);                  // sets the servo position according to the scaled value 
		delay(2);
	}
	else{
		digitalWrite(ledPin, LOW);
	}
}
void dimmer(){
	int button = bCounter();							//calls for function which returns button counter/state
	if(button % 2 == 0){
		int potVal = analogRead(potPin);            	// reads the value of the potentiometer (value between 0 and 1023) 
		int brightnes = map(potVal, 0, 1023, 0, 255);   // scale it to use it with the analog output (value between 0 and 255)
		analogWrite(ledPin, brightnes);
	}
	else{
		digitalWrite(ledPin, LOW);
	}
}
int bCounter(){
	// read the pushbutton input pin:
	buttonState = digitalRead(buttonPin);

	// compare the buttonState to its previous state
	if (buttonState != lastButtonState) {
		// if the state has changed, increment the counter
		if (buttonState == HIGH) {
			// if the current state is HIGH then the button
			// wend from off to on:
			buttonPushCounter++;
			Serial.println("  on");
			Serial.print("Number of button pushes:  ");
			Serial.println(buttonPushCounter);
		} 
		else {
			// if the current state is LOW then the button
			// wend from on to off:
			Serial.println("  off"); 
		}
	}
	// save the current state as the last state, 
	//for next time through the loop
	lastButtonState = buttonState;
	return buttonPushCounter;
}

void menu(){
	Serial.println("  **************************** MENU *************************************");
	Serial.println(" |                                                                       |");
	Serial.println(" |   Please select following:                                            |");
	Serial.println(" |                                                                       |");
	Serial.println(" |   1 - Led ON and OFF                                                  |");
	Serial.println(" |   2 - Led blink rate control                                          |");
	Serial.println(" |   3 - Dimmer                                                          |");
	Serial.println(" |   4 - Button ON: servo ON and control by POT, button OFF servo off    |");
	Serial.println(" |                                                                       |");
	Serial.println(" |   Any other keyboard input than 1, 2, 3 and 4 turns functions OFF     |");
	Serial.println(" |   Press r key to display MENU and reset the push button counter       |");
	Serial.println(" |                                                                       |");
	Serial.println("  ***********************************************************************");
}
void resetAndMenuOnce(){
	while( m == 0){
		Serial.println("  **************************** RESET ************************************");
		Serial.println(" |                                                                       |");
		Serial.println(" |                  Push button counter is reset                         |");
		Serial.println(" |                                                                       |");
		menu();
		digitalWrite(ledPin, LOW);
		buttonPushCounter = 1;
		m++;
	}
}
















