Need help with checking code and circuit diagram!

Hey! I am a newbie to arduino, but am currently creating a project where once an ir sensor is triggered, a dc motor goes on for 4 seconds, and an mp3 decoder goes on for 20. i also need it so that once a microswitch is not pressed, a motor turns on, and vice versa. here is my circuit diagram (fritzing doesnt have the option for a microswitch or mp3 decoder so the button would actually be a microswitch, and the 2 wires going to nowhere on the left would be going to my mp3 decoder.


This code i mostly got from someone else as i am not an expert at millis, so if you could try and read through it and tell me if something looks off that would be great. currently my first motor is just staying on. `#define SENSOR_PIN 6
#define MOTOR_PIN 11
#define MP3 4
#define Switch 8
#define Motortwo 13
int SwitchValue = 0;
void setup() {
pinMode(SENSOR_PIN, INPUT);
pinMode(MOTOR_PIN, OUTPUT);
pinMode(MP3, OUTPUT);
digitalWrite(MOTOR_PIN, LOW);

digitalWrite(MP3, LOW);

}

struct event_t {
int pin;
int on;
int duration;
unsigned long timer;
event_t(int pin, int duration) :
pin(pin),
duration(duration)
{ on = false; timer = 0; }
} schedule[2] = {
{ MOTOR_PIN, 4000 },
{ MP3, 20000 }
};

void item_state(int which, bool bOn) {
schedule[which].on = bOn;
if (bOn) {
digitalWrite(schedule[which].pin, HIGH);
schedule[which].timer = millis() + schedule[which].duration;
} else {
digitalWrite(schedule[which].pin, LOW);
schedule[which].timer = 0;
}
}

void sequence(int &which, bool &active) {
if (!schedule[which].on) { // if not started
active = true;
item_state(which, active);
} else {
if (millis() >= schedule[which].timer) {
item_state(which, false);
if (++which != sizeof(schedule)) {
item_state(which, true);
} else {
which = 0;
active = false;
}
}
}
}

void loop() {
SwitchValue = digitalRead(Switch);
if(SwitchValue !=0){
digitalWrite(Motortwo, LOW);
}
else{(Motortwo, HIGH); }

static bool bActive = false;
static int activeEvent = 0;
if (digitalRead(SENSOR_PIN) == HIGH) {
    // don't start if already active
    if (!bActive) {
        bActive = true;
        sequence(activeEvent, bActive);
    }
}

// The sequence(...) function does not block.
// Continue to do other stuff here asynchronously.
// bActive will be set to false when sequence has completed.

}` Thank you all so much!

Your diagram is fine for wiring on the bench but nearly impossible to convert to an understandable circuit without redrawing it completely.

So I suggest you write small programs to test each function separately to show the hardware is even controllable in the method you want. You can add Print statements to areas of your code which will print to the IDE serial monitor.

For instance:

Write a program that:

Prints "starting motor'
Start motor
delay(10000) ten second delay
Print "stopping motor"
delay(2 seconds)
Loop to the beginning to repeat forever.

Then look at the example Blind without delay in the arduino IDE examples (under File).

Learn to do the above with millis.

Now do the same with each other device (using millis).

Note a 9V battery has very little energy. It is a poor choice for what you are doing, espceially operating motors. You need to get a plug in supply (like a battery charger but ( or 12V)

Hi, @halloweencentral
Please read the post at the start of any forum , entitled "How to use this Forum".

This will help with advice on how to present your code and problems.

A hand drawn circuit will be a lot better, include all your components and label them as well as pins.

Tom... :smiley: :+1: :coffee: :australia:

Your circuit is for N-channel MOSFETs, which transistors are you using?

Hi,
You do not have the gnd of the UNO connected to the gnd of the motor power supply.

Tom.... :smiley: :+1: :coffee: :australia:

the posted code seems unnecessarily complex
consider

#undef MyHW
#ifdef MyHW
# define PinSw       A1
# define PinIR       A2

# define PinMotor1   11
# define PinMotor2   12
# define PinMP3      13

#else
# define PinSw       8
# define PinIR       6

# define PinMotor1   11
# define PinMotor2   13
# define PinMP3      4
#endif

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void setup ()
{
    pinMode (PinSw, INPUT_PULLUP);
    pinMode (PinIR, INPUT_PULLUP);

    pinMode (PinMotor1, OUTPUT);
    pinMode (PinMotor2, OUTPUT);
    pinMode (PinMP3,    OUTPUT);

    digitalWrite (PinMotor1, Off);
    digitalWrite (PinMP3,    Off);
}

// -----------------------------------------------------------------------------
struct Timer {
    byte           pin;
    unsigned long  duration;
    bool           on;
};

Timer timers [] = {
    { PinMotor1, 4000 },
    { PinMP3,    20000 }
};

#define N_TIMERS    (sizeof(timers)/sizeof(Timer))

// -----------------------------------------------------------------------------
void loop ()
{
    static unsigned long msecLst;
           unsigned long msec = millis ();

    // enable timed outputs
    if (digitalRead (PinIR) == LOW)  {
        msecLst = msec;
        for (unsigned n = 0; n < N_TIMERS; n++)  {
            timers [n].on = true;
            digitalWrite (timers [n].pin, On);
        }
    }

    // turn off timed outputs when duration expired
    Timer *t = timers;
    for (unsigned n = 0; n < N_TIMERS; n++, t++)  {
        if (t->on && (msec - msecLst) > t->duration)  {
            digitalWrite (t->pin, Off);
            t->on = false;
        }
    }

    // drive motor from switch
    digitalWrite (PinMotor2, digitalRead (PinSw));
}

Sorry I'm dumb, but my mosfet says IRF520NPbF on it, is that n channel?

oops lol, long night, forgot to put that on my diagram. I do have it connected on the real thing

my motor came from an animatronic from spirit, which are 6v. whats wrong with my circuit?

ayyyyy thanks so much man, I'll try this later and let you know how it goes!

Hi,

Your "circuit diagram" is what is called a Fritzy?
Sorry but it basically does not display any major useful info about your project.
If you look at your diagram, it is a mass of crossed coloured wires.

Please draw a proper schematic, a hand drawn circuit will be fine, showing your power supply, component labels and pin names.

Thanks... Tom.... :smiley: :+1: :coffee: :australia:

do you mean a schematic? or just a diagram

Let's not split hairs. We don't need a schematic of the entire UNO, or the other modules. Just a complete wiring diagram. When modules are shown in addition to, or instead of bare components, it should probably be called a wiring diagram not a schematic, but really the difference is academic in this case.

The necessary attributes are spelled out for you, " showing your power supply, component labels and pin names". Your question sounds like you are trying to do the minimum work.

Hi,
Like this example;


Where multi-pin components are represented by boxes with side/top connections.

Hand drawn will be fine.
Tom.... :smiley: :+1: :coffee: :australia:

God you people are nitpicky. I started arduino in july and am 14, but apparently clarification is doing the minimum work.

Crao, i have absolutely no idea how those work. I'll try to learn and make one.

Then post both a schematic and a diagram just to be sure. Or, tell us what you think the difference between a schematic and a diagram is. Because, we can't be really sure what you think the difference is.

I have absolutely no idea how to make one or how they work and am currently trying to learn. I'm entirely self taught and it sucks that i constantly get crap for not knowing every bit of arduino. I came here to learn, not have someone yell at me but not even try to help. Please try and have some compassion.

Have look at this YouTube, very simple how to by a guy who know how to.

Google;

how to draw electronic schematic diagram

Will also provide some insight.
You do not need a CAD package, a pen(cil) and paper will get you by for your project.

Tom... :smiley: :+1: :coffee: :australia:
PS. When I was your age, I would copy diagrams from magazines and library books, any kit instructions I copied, just to get the hang of it. I also kept me out of trouble and my supportive parents when they were busy.

Ayyyyy thanks so much, I'll have a look