Switching between to programs with a 1+4 Matrix Keypad on Arduino

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);
}

}

int TIP120pin10 = 10; //for this project, I pick Arduino's PMW pin 10

The Arduino doesn't have any PMW pins. Perhaps you were thinking PMS? Or maybe PWM.

If it's PWM, then pins 12 and 13 are not PWM pins, unless you have a Mega.

  outputValue = map(sensorValue, 0, 1023, 0, 255);

The map function is overkill for dividing by 4.

I know I have to use a IF statement but I was sure on how to use it with the 1+4 matrix keypad.

You need to actually determine which key is pressed. Where are you doing that?

Thanks for the comments. I have did some updates and this is my modified code; however when I press either button nothing works. Any advice?

// 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 button1 = 7;
int button2 = 8;
int buttonstate1 = 0;
int buttonstate2 = 0;
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)

void setup() {
pinMode(TIP120pin10, OUTPUT);
pinMode(TIP120pin11, OUTPUT);
pinMode(TIP120pin12, OUTPUT);
pinMode(TIP120pin13, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);

}

void loop(){
if (buttonstate1 == HIGH){
loop1();
}
else if (buttonstate2 == HIGH){
loop2();
}
}
void loop1() {
// 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);
delay(10);
}

void loop2(){
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);
}
}

You never update buttonstate2 and buttonstate1.

Add something like

buttonstate1 = digitalRead(button1_pin);
buttonstate2 = digitalRead(button2_pin);


Rob

Which Arduino are you using? This is the 2nd time I've asked.

If you are not using a Mega, some of your "PMW" pins do not support PWM.

You've done nothing about the wasteful call to map.

If you run loop2, you'll notice that the sketch becomes unresponsive for nearly five and a half seconds.
This may or may nor be an issue for you.

Sorry about that. I have a UNO controller. Also, could you give me some specific advice on improving the map portion of my code?

I have a UNO controller.

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

The PWM pins are 3, 5, 6, 9, 10, and 11.

Also, could you give me some specific advice on improving the map portion of my code?

outputValue = map(sensorValue, 0, 1023, 0, 255) / 4;

Hi everyone,

With the help I have made modifications to my code; however I still can't get it to switch when I press the button. Can I get some additional support?

// 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 TIP120pin9 = 9; //for this project, I pick Arduino's PMW pin 9
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 TIP120pin6 = 6; //for this project, I pick Arduino's PMW pin 6
int button1 = 7;
//int button2 = 8;
int buttonstate1 = 0;
//int buttonstate2 = 0;
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)

long lastDebounceTime =0; //the last time the output ping toggled
long debounceDelay = 50; //the debounce time; increase if the output flickers
int buttonState; //the current reading from the input pin
int lastButtonState = LOW; //the previous reading from the input pin
int currLoop=0;

void setup()
{
pinMode(TIP120pin9, OUTPUT);
pinMode(TIP120pin10, OUTPUT);
pinMode(TIP120pin11, OUTPUT);
pinMode(TIP120pin6, OUTPUT);
pinMode(button1, INPUT);
//pinMode(button2, INPUT);

}

void loop()
{
int reading = digitalRead(button1);
//buttonstate2 = digitalRead(button2);
if (reading != lastButtonState)
lastDebounceTime = millis();
if ((millis() - lastDebounceTime) > debounceDelay)
{
buttonstate1 = reading;
if (buttonstate1 == HIGH)
currLoop=1-currLoop;
}
if (currLoop)
loop2();
else
loop1();
lastButtonState = reading;

}
void loop1() {
// 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(TIP120pin9, outputValue);
analogWrite(TIP120pin10, outputValue);
analogWrite(TIP120pin11, outputValue);
analogWrite(TIP120pin6, outputValue);
//delay(10);
}

void loop2(){
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(TIP120pin9, sinOut);
analogWrite(TIP120pin10, sinOut);
analogWrite(TIP120pin11, sinOut);
analogWrite(TIP120pin6, sinOut);
//delay(15);
}
}

You still have this

outputValue = map(sensorValue, 0, 1023, 0, 255);


Rob

int TIP120pin9 = 9; //for this project, I pick Arduino's PMW pin 9
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 TIP120pin6 = 6; //for this project, I pick Arduino's PMW pin 6

Using the pin number in the name is silly. The whole reason for defining names for the pin numbers is so that you can easily change the pin number being used without having to change any other code. With these names, that is impossible.

Suppose that you needed to change the pin used for the 4th TIP120 to pin 5. Determine how much of your code needs to change. More than one line, unless you want something like:

int TIP120pin6 = 5;

which isn't silly. It's stupid.

You are not using the internal pullup resistor for button1. So, you must have an external pullup or pulldown resistor. How IS the switch wired?

What purpose does copying reading to buttonState1 serve?

Your poor indenting and poor use of white space makes it difficult to follow your code. Even though if statements and else statements that have only one line of code in the block do not NEED curly braces, it is much easier to see that you have not made invalid assumptions, based on indenting, about what really will be executed if you ALWAYS use curly braces to define the body of all if/else/for/while statements.

There are no Serial.print() statements that define exactly what is happening. Why not? How ARE you debugging this code?

Finally, "this doesn't work" gives us nothing to go on. "I expected such and such; instead this and that happened" gives us a LOT more to go on. We don't have your hardware, wired your way, so we can't exercise the code the same way.