Brand New and need Help

i am trying to use an Arduino to control 3/4 different things and im having a hell of a time controlling anything more than the one LED. i am able to change which output pin the LED is in but after that i can figure out how to successfully add a different/second input/output.

I want to control 2 5v fans independently, a 5v amp and a small voice changer. i want to use a 1x4 keypad. (Membrane 1x4 Keypad + Extras : ID 1332 : $2.95 : Adafruit Industries, Unique & fun DIY electronics and kits)

here is my code thus far.

one question is if i use LEDs as stand-ins with this code should i not be able to mimic how i have the wires for the 13/7 setup on the 9/4 setup? and cant i share the ground?

i'm such a nubie and I'm feeling lost and condused. there really isnt much infor out there about the 1x4 keypad.

thankd

axis.ino (1.47 KB)

Yeah, you could use LED's in place of your other things for now. It makes visualizing whats happening MUCH easier.

I'm too lazy to download the code ahaha, if you could, put it in the message using the code blocks (Top left of the toolbar).

I assume you may be using delay();, which obviously makes 4 systems run independently difficult. One thing you can do is take the time and store it in a variable, then the time again and store it in another variable then subtract the first from the last. That would give you elapsed time, so rather than delaying say one second, it can continue running and check when one second has passed. Here's an example:

long timeA = 0;  //Create variable to store time event happened
long timeB = 0;  //Create variable to store the current time
long elapsed;     //Create variable to store how much time has passed

void loop() {
   timeB = millis();  //Store the current time passed since program started in milliseconds

   if(//Event, such as button press) {  //If the event happens
       timeA = millis();                     //Store the time it happened
   }
    
   if(timeA != 0) {                          //If the event has a time
      elapsed = timeB - timeA;          //Find the elapsed time
   }

   if(elapsed >= 1000) {                  //If the elapsed time is greater than or equal to one second
      //Do something else
   }
}

Not sure if that helps. Just a trick I learned making my Sonic Screwdriver (Universal Remote!)

I would really not recommend running the fans and the amp directly from the pins. The pins can only provide 20mA continuous current and 30-40mA peak. An led can pull 20mA usually, and IR leds can get up to 100mA. I made the mistake of using one without limiting the current pull down to 20 (resistor) and it burnt out my pin 3.

For motors, or anything that needs higher current, use a transistor controlled from the pin. I haven't quite figured it out yet myself, but you can get much higher current than just pulling from the pins.
See this page: https://www.arduino.cc/en/Tutorial/TransistorMotorControl

Hope this helps!

one question is if i use LEDs as stand-ins with this code should i not be able to mimic how i have the wires for the 13/7 setup on the 9/4 setup?

Yes you should.

cant i share the ground?

Not only can you share grounds, you have to have a common ground.

Where you are going wrong is you are using the same variables to cope with two LEDs. The variables should be renamed so a new variable is used for all dealings with the second LED.
So

int val = 0;     // val will be used to store the state
                 // of the input pin
int old_val = 0; // this variable stores the previous
                 // value of "val"
int state = 0;   // 0 = LED off and 1 = LED on

should be

int val1 = 0;     // val will be used to store the state of the input pin to control LED 1
int val2 = 0;     // val will be used to store the state of the input pin to control LED 2
int old_val1 = 0; // this variable stores the previous state of the input pin LED 1
int old_val2 = 0; // this variable stores the previous state of the input pin LED 2                 
int state1 = 0;   // state of LED1 -  0 = LED off and 1 = LED on
int state2 = 0;   // state of LED2 -  0 = LED off and 1 = LED on

This makes

val = digitalRead(BUTTON1); // read input value and store it
                            // yum, fresh
 val = digitalRead(BUTTON2); // read input value and store it
                            // yum, fresh

Should be

val1 = digitalRead(BUTTON1); // read input value of button 1 and store it
                            
 val2 = digitalRead(BUTTON2); // read input value button 2 and store it

And likewise whenever you encounter any of the other variables.

Posting a .ino file is a bit problematic because most mobile devices don't know how to deal with them and so won't download them.
Please read this:-
How to use this forum as it will tell you how to post code.