Help with code, will not work no matter what i try

Hey all! I am currently trying to make an animatronic using arduino. Essentially i need it so that once an obsticle avoidance sensor is triggered, a motor runs for 4 seconds, and something else runs for 20. i really have tried my best with coding for weeks, but nothing i have done works. my motor is connected to pin 13, my sensor 6, and the other thing is 2. if any of you could help me with code, that would be appriciated. thanks!

Some code would help. Make sure you use the code tags when posting (these things </>).

Show us your best attempt, tell us what’s wrong with it - output examples might help.

Remember, sometimes some Serial.prints sprinkled throughout your program will help you see things that shouldn’t be happening.

Hi, @halloweencentral
Welcome to the forum.

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.

We need your code and a circuit diagram of your project.
What is your obstacle avoidance sensor, link to spec/data?
What is your motor, link to specs/data?
What Arduino controller are you using?
How are you powering your project?

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

Ok, so here is the code that i have written, it doesn't have a single timer on it considering that i cannot really understand millis. i have also gotten some code from someone else that i will post. My code < int sensor = 6;
int motor = 13;
void setup()
{
pinMode (sensor, INPUT);
pinMode (motor, OUTPUT);
Serial.begin(9600);
}

void loop()
{
int sensorvalue = digitalRead (sensor);

if (sensorvalue == 1)
{
digitalWrite(motor,HIGH );

}

else
{
digitalWrite(Motor, LOW);
}
Serial.println(sensorvalue);

} >
Code i have gotten: < #define SENSOR_PIN 2
#define MOTOR_PIN 3
#define SOMETHING_ELSE 4

void setup() {
pinMode(SENSOR_PIN, INPUT);
pinMode(MOTOR_PIN, OUTPUT);
pinMode(SOMETHING_ELSE, OUTPUT);

digitalWrite(MOTOR_PIN, LOW);
digitalWrite(SOMETHING_ELSE, 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] = {
{ 2, 4000 },
{ 3, 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 * 1000;
} 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() {
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.

} >

Ok, so here is the code that i have written, it doesn't have a single timer on it considering that i cannot really understand millis. i have also gotten some code from someone else that i will post. i think the code i have gotten has a way better chance of working, but it still has not. My code is <int sensor = 6;
int motor = 13;
void setup()
{
pinMode (sensor, INPUT);
pinMode (motor, OUTPUT);
Serial.begin(9600);
}

void loop()
{
int sensorvalue = digitalRead (sensor);

if (sensorvalue == 1)
{
digitalWrite(motor,HIGH );

}

else
{
digitalWrite(Motor, LOW);
}
Serial.println(sensorvalue);

} >
Code i have gotten: <#define SENSOR_PIN 2
#define MOTOR_PIN 3
#define SOMETHING_ELSE 4

void setup() {
pinMode(SENSOR_PIN, INPUT);
pinMode(MOTOR_PIN, OUTPUT);
pinMode(SOMETHING_ELSE, OUTPUT);

digitalWrite(MOTOR_PIN, LOW);
digitalWrite(SOMETHING_ELSE, 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] = {
{ 2, 4000 },
{ 3, 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 * 1000;
} 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() {
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.

}>

I don't have fritzing so i will just show you the diagrams that i am using. I am using this for my motor, ignore the button. TransistorMotor_bb For controlling power to the other thing (soundboard) i am using this: https://circuits-diy.com/how-to-use-a-transistor-bc547-as-a-switch/. My motor is just one that was already on part of the animatronic that i scrapped, but it's just a hobby motor, like this one: $5.99 - Hobby Motor 6V DC 10000rpm - Tinkersphere.
. For my board i am using a genuine arduino uno. in terms of code i have code that i wrote myself, which has no timing in it, and also code that someone has given me. my code is <int sensor = 6;
int motor = 13;
void setup()
{
pinMode (sensor, INPUT);
pinMode (motor, OUTPUT);
Serial.begin(9600);
}

void loop()
{
int sensorvalue = digitalRead (sensor);

if (sensorvalue == 1)
{
digitalWrite(motor,HIGH );

}

else
{
digitalWrite(Motor, LOW);
}
Serial.println(sensorvalue);

} the other (and more promising code) is <#define SENSOR_PIN 2
#define MOTOR_PIN 3
#define SOMETHING_ELSE 4

void setup() {
pinMode(SENSOR_PIN, INPUT);
pinMode(MOTOR_PIN, OUTPUT);
pinMode(SOMETHING_ELSE, OUTPUT);

digitalWrite(MOTOR_PIN, LOW);
digitalWrite(SOMETHING_ELSE, 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] = {
{ 2, 4000 },
{ 3, 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 * 1000;
} 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() {
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.

}>

for my sensor i am using this: https://www.amazon.com/OSOYOO-Infrared-Obstacle-Avoidance-Arduino/dp/B01I57HIJ0

Your post was MOVED to its current location as it is more suitable.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination