//#include <LiquidCrystal_I2C.h>
//LiquidCrystal_I2C lcd(0x27,16,2);
int potPin1 = A0;
float val;
float RPM;
//unsigned int loopcount;
//int potPin2 = A2;
//fot loop count
unsigned int loop_per_sec;
unsigned int period = 1000;
unsigned long time_now;
unsigned int last_loops = 0;
long lastMillis = 0;
long loops = 0;
int s;
void setup() {
//lcd.init(); // initialize the lcd
// Print a message to the LCD.
//lcd.backlight();
//lcd.setCursor(3,0);
//lcd.print("Hello, world!");
//lcd.setCursor(2,1);
//lcd.print("Ywrobot Arduino!");
//pinMode(potPin1, INPUT);
//pinMode(potPin2, INPUT);
// put your setup code here, to run once:
DDRA |= B00111111; //6 LEDs as output from PA0-PA5
DDRB |= B10000000; //for (1<<PB7); to work // to loops count
PORTA &= B00000000; //test the LEDS are working
PORTB &= ~(1<<PB7);
delay(2000);
PORTA &= B11111111; //set LOW all LEDS
PORTB |= (1<<PB7);
}
void loop() {
val = analogRead(potPin1);
RPM = map(val,0,1023,750,3800);
//lcd.clear();
//lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
//lcd.print("target RPM:"); // Prints Sensor Val: to LCD
//lcd.print(RPM); // Prints value on RPM to LCD
//lcd.setCursor(0,1);
//lcd.print("Slider Read:");
//lcd.print(val);
//lcd.setCursor(0,0);
//lcd.print("loop :");
//cd.print(loops);
//lcd.setCursor(0,1);
//lcd.print("loop/sec: ");
//lcd.print(loop_per_sec);
//Common GND, LOW is TRUE or 1
// put your main code here, to run repeatedly:
if(RPM >= 1000){
//PORTA = B00000001; //Sets only PA0 to HIGH
PORTA &= ~(1<<PA0);
} else {PORTA |= (1<<PA0);}
if(RPM >= 1500){
PORTA &= ~B00000010;
} else {PORTA |= B00000010;}
if(RPM >= 2000){
PORTA &= ~(1<<PA2);
} else {PORTA |= (1<<PA2);}
if(RPM >= 2500){
PORTA &= ~(1<<PA3);
} else {PORTA |= (1<<PA3);}
if(RPM >= 3000){
PORTA &= ~(1<<PA4);
} else {PORTA |= (1<<PA4);}
if(RPM >= 3500){
PORTA &= ~(1<<PA5);
} else {PORTA |= (1<<PA5);}
if(loop_per_sec > 5500){
PORTB &= ~(1<<PB7); //digitalWrite(13,LOW);//lit idle lamp, common GND
} else {
PORTB |= (1<<PB7); //digitalWrite(13,HIGH); //common GND
}
//delay(100); //no delay in the loop for now
//long currentMillis = millis();
loops++;
/* By doing complex math, reading sensors, using the "delay" function,
* etc you will increase the time required to finish the loop,
* which will decrease the number of loops per second.
*/
if(millis() >= time_now + period){
time_now += period;
s++;
loop_per_sec = loops - last_loops;
last_loops = loops;
}
} //main loop
//Sets only PA0 to HIGH
//PORTA |= (1<<PA0);
//PORTA |= B00000001;
//set only PA1 to LOW
//PORTA &= ~(1<<PA1);
//PORTA &= B11111110;