/***************************************************
Project R2D2 Sound Generator
To all fans of StarWars and Arduino!
Written by Marcelo Larios
BSD license, all text above must be included in any redistribution
Participated in the Instructable Arduino Contest 2019
https://www.instructables.com/R2D2-Sound-Generator/
****************************************************/
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
#define speakerPin 11
#define led1Pin 7
#define led2Pin 8
#define led3Pin 9
#define led4Pin 10
#define modepin1a 4
#define modepin2a 5
int potpin1 = A1; // analog pin used to connect the potentiometer
int potpin2 = A2; // analog pin used to connect the potentiometer
int potpin3 = A3; // analog pin used to connect the potentiometer
int potpin4 = A4; // analog pin used to connect the potentiometer
int potpin5 = A5; // analog pin used to connect the potentiometer
int k;
int p1;
int d1;
int p2;
int d2;
int modepin1;
int modepin2;
//int k=1000; //random number for calc (1-2000)
//int p1=500; //random length high (100-3000)
//int d1=2; //random mhz (.9-2)
//int p2=500; //random length Low (100-3000)
//int d2=2; //random mhz (.9-2)
void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
pinMode(modepin1a, INPUT_PULLUP);
pinMode(modepin2a, INPUT_PULLUP);
randomSeed(analogRead(0));
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("K");
lcd.setCursor(0,1);
lcd.print("P1");
lcd.setCursor(8,1);
lcd.print("D1");
}
void loop() {
k = analogRead(potpin1); // reads the value of the potentiometer (value between 0 and 1023)
k = map(k, 0, 1023, 1, 2000); // scale it to use it with the servo (value between 0 and 180)
p1 = analogRead(potpin2);
p1 = map(p1, 0, 1023, 100, 3000);
d1 = analogRead(potpin3);
d1 = map(d1, 0, 1023, 0, 2);
p2 = analogRead(potpin4);
p2 = map(p2, 0, 1023, 100, 3000);
d2 = analogRead(potpin5);
d2 = map(d2, 0, 1023, 0, 2);
modepin1=digitalRead(modepin1a);
modepin2=digitalRead(modepin2a);
lcd.setCursor(3,0);
lcd.print(k);
lcd.setCursor(3,1);
lcd.print(p1);
lcd.setCursor(11,1);
lcd.print(d1);
lcd.setCursor(13,0);
lcd.print(modepin1);
lcd.setCursor(15,0);
lcd.print(modepin2);
if(modepin1 == HIGH && modepin2 == HIGH){
digitalWrite(led2Pin, HIGH);
for (int i = 0; i <= p1; i++){
tone(speakerPin, k+(i*2));
delay(d1);
}
digitalWrite(led2Pin, LOW);
}
else if (modepin1 == LOW && modepin2 == HIGH){
digitalWrite(led1Pin, HIGH);
for (int i = 0; i <= p1; i++){
tone(speakerPin, k +(-i*2));
delay(d1);
}
digitalWrite(led1Pin, LOW);
}
else if (modepin1 == LOW && modepin2 == LOW){
digitalWrite(led3Pin, HIGH);
for (int i = 0; i <= p1; i++){
tone(speakerPin, k+(i/2));
delay(d1);
}
digitalWrite(led3Pin, LOW);
}
else if (modepin1 == HIGH && modepin2 == LOW){
digitalWrite(led4Pin, HIGH);
for (int i = 0; i <= p1; i++){
tone(speakerPin, k);
delay(d1);
}
digitalWrite(led4Pin, LOW);
}
}