I made this to workk like if I plug the brown cable, a transistor will give me 12V for 5 seconds even if i keep my cable plugged however when i remove back for some reason the transistor runs again I think it has something to do with the code but i cant figure out whats happening neither on google i find something similar.
The video about it:
The video corresponds to the 2nd IF statement
void setup() {
pinMode(5,INPUT);
pinMode(2,INPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
}
void loop() {
if (digitalRead(5)==HIGH)
{
digitalWrite(3,HIGH);
delay(5000);
do{
digitalWrite(3,LOW);
}while (digitalRead(5)==HIGH);
}
if(digitalRead(2)==HIGH)
{
digitalWrite(4,HIGH);
delay(5000);
do{
digitalWrite(4,LOW);
}while (digitalRead(2)==HIGH);
}
Circuit:
LarryD
May 24, 2021, 12:30am
2
You must add a resistor in series with the base of each transistor.
Try 220Ω
Wire your switches as S3 is wired in the image below then look for a LOW for a pushed switch.
Use:
pinMode(5,INPUT_PULLUP);
pinMode(2,INPUT_PULLUP);
I forgot to update that circuit, on video i was using 1K resistors of each base
LarryD
May 24, 2021, 12:37am
4
In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch .
Use the </> icon from the ‘reply menu’ to attach the copied sketch.
This is the code with the S3 way of switching, but still do the same
void setup() {
pinMode(5, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}
void loop() {
if (digitalRead(5) == LOW)
{
digitalWrite(3, HIGH);
delay(5000);
do {
digitalWrite(3, LOW);
} while (digitalRead(5) == LOW);
}
if (digitalRead(2) == LOW)
{
digitalWrite(4, HIGH);
delay(5000);
do {
digitalWrite(4, LOW);
} while (digitalRead(2) == LOW);
}
}
LarryD
May 24, 2021, 12:55am
6
ctronics:
I made this to workk like if I plug the brown cable, a transistor will give me 12V for 5 seconds even if i keep my cable plugged however when i remove back for some reason the transistor runs again I think it has something to do with the code but i cant figure out whats happening neither on google i find something similar.
Please explain this better.
Let’s see your new schematic with the switches wired as S3.
Is the Arduino GND connected to the 12v GND ?
LarryD
May 24, 2021, 12:58am
7
BTW, you should look for a change in the switch not the switches current level.
Yes the ground is all together.
So what is happening is: I "have" a switch like that (picture below), and I need to use it, and what he is going to do is control a dc Motor with time (5000ms in this case) if I put at I it will turn right, if I put II it will turn left, and as I need to use that button, I need to find a way to let him "pressed" at I or II but not turning.
Example:
OH i want to turn right so I put the button at I; then he will move during 5 seconds and stop even if the button is stuck at I
LarryD
May 24, 2021, 1:08am
10
Is the switch OFF in the center ?
LarryD
May 24, 2021, 1:20am
12
Are you wired like this ?
The only difference is the 220Ohms resistors, I am using 1K
LarryD
May 24, 2021, 2:09am
14
Try this:
// Version YY/MM/DD Description
// 1.0 21/05/23 Running sketch
//
//
#define OPEN HIGH
#define CLOSED LOW
#define MotorON HIGH
#define MotorOFF LOW
#define timingEnabled true
#define timingDisabled false
bool rightTimingFlag = timingDisabled;
bool leftTimingFlag = timingDisabled;
const byte leftSwitch = 2;
const byte rightSwitch = 5;
const byte leftMotor = 4;
const byte rightMotor = 3;
byte lastLeft = OPEN;
byte lastRight = OPEN;
unsigned long commonMillis;
//**********************************************************************
void setup()
{
pinMode(rightSwitch, INPUT_PULLUP);
pinMode(leftSwitch, INPUT_PULLUP);
pinMode(rightMotor, OUTPUT);
pinMode(leftMotor, OUTPUT);
} //END of setup()
//**********************************************************************
void loop()
{
byte switchStatus;
//****************************************
if (rightTimingFlag == timingEnabled && millis() - commonMillis >= 5000)
{
//timing disabled
rightTimingFlag = timingDisabled;
//stop the right motor
digitalWrite(rightMotor, MotorOFF);
}
//****************************************
if (leftTimingFlag == timingEnabled && millis() - commonMillis >= 5000)
{
//timing disabled
leftTimingFlag = timingDisabled;
//stop the left motor
digitalWrite(leftMotor, MotorOFF);
}
//****************************************
switchStatus = digitalRead(rightSwitch);
//did the switch change state ?
if (lastRight != switchStatus)
{
//update to the new state
lastRight = switchStatus;
//if we are not timing, is the right switch pushed ?
if (rightTimingFlag == timingDisabled && leftTimingFlag == timingDisabled && switchStatus == CLOSED)
{
//start the right motor
digitalWrite(rightMotor, MotorON);
//enable TIMER
rightTimingFlag = timingEnabled;
//start the TIMER
commonMillis = millis();
}
}
//****************************************
switchStatus = digitalRead(leftSwitch);
//did the switch change state ?
if (lastLeft != switchStatus)
{
//update to the new state
lastLeft = switchStatus;
//if we are not timing, is the left switch pushed ?
if (leftTimingFlag == timingDisabled && rightTimingFlag == timingDisabled && switchStatus == CLOSED)
{
//start the right motor
digitalWrite(leftMotor, MotorON);
//enable TIMER
leftTimingFlag = timingEnabled;
//start the TIMER
commonMillis = millis();
}
}
} //END of loop()
//**********************************************************************
It is working like one time ok on time not ok
LarryD
May 24, 2021, 2:20am
16
Please explain this better.
LarryD
May 24, 2021, 2:29am
17
Touch the switch wire 'momentarily' go to GND.
as you can see in this video sometimes work sometimes not
LarryD
May 24, 2021, 2:33am
19
Touch the switch wire momentarily go to GND.
Nah tho, doesnt work too can it be the chip?maybe it is damaged?