I have two programs that I have tested separated and works well for my project; however I wanted to combined them in Ardunio and have a two buttons on 1 + 4 Matrix keypad switch between the codes. I have attached the codes that I am using and a schematic of the keypad. I know I have to use a IF statement but I was sure on how to use it with the 1+4 matrix keypad.
First code
// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
int TIP120pin10 = 10; //for this project, I pick Arduino's PMW pin 10
int TIP120pin11 = 11; //for this project, I pick Arduino's PMW pin 11
int TIP120pin12 = 12; //for this project, I pick Arduino's PMW pin 12
int TIP120pin13 = 13; //for this project, I pick Arduino's PMW pin 13
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(TIP120pin10, outputValue);
analogWrite(TIP120pin11, outputValue);
analogWrite(TIP120pin12, outputValue);
analogWrite(TIP120pin13, outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 10 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(10);
}
SECOND CODE
int TIP120pin10 = 10; //for this project, I pick Arduino's PMW pin 10
int TIP120pin11 = 11; //for this project, I pick Arduino's PMW pin 11
int TIP120pin12 = 12; //for this project, I pick Arduino's PMW pin 12
int TIP120pin13 = 13; //for this project, I pick Arduino's PMW pin 13
void setup(){
pinMode(TIP120pin10, OUTPUT);
pinMode(TIP120pin11, OUTPUT);
pinMode(TIP120pin12, OUTPUT);
pinMode(TIP120pin13, OUTPUT);
}
void loop(){
for(int i = 0; i<360; i++){
//convert 0-360 angle to radian (needed for sin function)
float rad = DEG_TO_RAD * i;
//calculate sin of angle as number between 0 and 255
int sinOut = constrain((sin(rad) * 128) + 128, 0, 255);
analogWrite(TIP120pin10, sinOut);
analogWrite(TIP120pin11, sinOut);
analogWrite(TIP120pin12, sinOut);
analogWrite(TIP120pin13, sinOut);
delay(15);
}
}
