I'm not proffessional in arduino coding,and i have an question about sketch. I need code to control relay with push buttons. I have 3 push buttons 3 leds and 2 relays. When 1 button push then select first led if twice push then select second led. When push second button once then select first relay,if twice push then select second relay,and in the end start button to start all this commands an then lights third led.
Pls Help!
int button1=2;
int button2=3;
int button3=4;
int relay1=8;
int relay2=9;
int led=5;
int led2=6;
int led3=7;
int button1State=0;
int button2State=0;
int button3State=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(button1, INPUT_PULLUP);
pinMode(button2,INPUT_PULLUP);
pinMode(button3,INPUT_PULLUP):
pinMode(relay1, OUTPUT);
digitalWrite(relay1, HIGH);
pinMode(relay2,OUTPUT);
digitalWrite(relay2, HIGH);
pinMode(led,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
button1State = digitalRead(button1);//when once click turn led
if(button1State==HIGH){
digitalWrite(led,HIGH);
delay(wait);
if(button1State==HIGH){ //when clicked twice then turn on led 2, but i dont know how to do it
digitalWrite(led2,HIGH);
delay(wait)
}
}
if (button2State== HIGH){
digitalWrite(relay1,HIGH);
delay(wait);
if(button2State==HIGH){ //when clicked twice turn on second relay but i dont know how to do this
digitalWrite(relay2,HIGH);
delay(wait);
}
if(button2State==HIGH){
digitalWrite(relay1&&relay2,HIGH);
delay(wait);
}
}
//and click start i dont kknow how to do this :((
//when select led then circuits run for 10 sec,if led 2 select then circuit runs for 20 sec
}
Related
I am attempting to photograph water drop collisions using two solenoids. I have successfully done this using one solenoid. This used setting delays in the code to time the camera shutter, water drops and flash. I would like to trigger two solenoids at the same time with identical delays but not sure how to do this.
The order of operation should be
Open the camera shutter (camera is in bulb mode)
Drop two water drops from two solenoids at the same time.
Fire the flash
Close the camera shutter
My platform is an Atmega 2560. Here is the code I have so far,
int relayCam=A0; //pin to relay to trigger camera
int relayFlash=A7 ; //pin to relay to fire flash
int button1Read;
//button read delays (settling time)
int button1DT=250;
int button1Pin=8; //pin for 2 drop button read
int DelayCamOpen=100; //wait time to trigger camera
int DelayCamClose=1000; //wait time to close camera shutter
//Drop start
int DropStart=500; //time to start first drop
//Flash timing
int DelayFlashFire1=350;
int DelayFlashOff=50; //turn flash off
bool runOnce = false;
class Solenoid
{
int SolenoidPin;
long OnTime;
long OffTime;
int SolenoidState;
unsigned long previousMillis;
public:
Solenoid(int pin, long on, long off)
{
SolenoidPin = pin;
pinMode(SolenoidPin, OUTPUT);
OnTime = on;
OffTime = off;
SolenoidState = HIGH;
previousMillis = 0;
}
void update()
{
unsigned long currentMillis = millis();
if((SolenoidState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
SolenoidState = HIGH;
previousMillis = currentMillis;
digitalWrite(SolenoidPin, SolenoidState);
}
else if ((SolenoidState == LOW) && (currentMillis - previousMillis >= OffTime))
{
SolenoidState = LOW;
previousMillis = currentMillis;
digitalWrite(SolenoidPin, SolenoidState);
}
}
};
Solenoid drop1(A1, 55, 55);
Solenoid drop2(A2, 55, 55);
void setup()
{
pinMode(relayCam, OUTPUT);
pinMode(relayFlash, OUTPUT);
pinMode(button1Pin, INPUT);
}
void loop()
{
//This part works
button1Read=digitalRead(button1Pin);
delay (button1DT);
if(button1Read==0) {
delay (DelayCamOpen); //wait time after button is pushed to start sequence
digitalWrite (relayCam,LOW); //opens the camera shutter
delay (DropStart); //wait time after shutter opens ro start first drop
//This part does not execute
if (runOnce == false)
{runOnce = true;
drop1.update();
drop2.update();
}
//This part works
delay (DelayFlashFire1); //wait time to fire flash
digitalWrite (relayFlash,LOW); //fire flash
delay (DelayFlashOff); //time to keep flash open (very short time)
digitalWrite (relayFlash,HIGH); //close flash relay
delay (DelayCamClose); //wait time to close camera shutter
digitalWrite (relayCam,HIGH); //close camera shutter
}
else if(button1Read==1)
{
digitalWrite (drop1,HIGH); //no signal to waterdrop
digitalWrite (drop2,HIGH); //no signal to waterdrop
digitalWrite (relayCam,HIGH); //no signal to camera
digitalWrite (relayFlash,HIGH); //no signal to flash
}
}
The camera and flash portion is working but the solenoids do not. I'm not sure if I'm even going about this correctly. I do know that having delays to run two solenoids is not giving me the desired results, hence trying to use the state machine method. Any help/suggestions is greatly appreciated. I am new to writing code and all things Arduino.
This is the code that works with one solenoid.
//Pin assignments
int relayCam=A0; //pin to relay to trigger camera
int relayFlash=A7 ; //pin to relay to fire flash
int relayWD1=A1; //pin to relay to trigger water drop 1
int button1Read;
//button read delays (settling time)
int button1DT=250;
int button1Pin=8; //pin for 2 drop button read
//drop sizes
int dropSize1=55; //time to hold relay for 1st drop
int dropSize2=45; //time to hold relay for 2nd drop
//delays betwween drops
int Delay1=45; //time between 1st and 2nd drops
//camera shutter timings
int DelayCamOpen=100; //wait time to trigger camera
int DelayCamClose=1000; //wait time to close camera shutter
//Drop start
int DropStart=500; //time to start first drop
//Flash timing
int DelayFlashFire1=350;
int DelayFlashOff=50; //turn flash off
void setup() {
pinMode(relayCam, OUTPUT);
pinMode(relayWD1, OUTPUT);
pinMode(relayFlash, OUTPUT);
pinMode(button1Pin, INPUT);
}
void loop() {
button1Read=digitalRead(button1Pin);
delay (button1DT);
if(button1Read==0) {
delay (DelayCamOpen); //wait time after button is pushed to start sequence
digitalWrite (relayCam,LOW); //opens the camera shutter
delay (DropStart); //wait time after shutter opens ro start first drop
digitalWrite (relayWD1,LOW); //starts first drop
delay (dropSize1); //duration of water flow
digitalWrite (relayWD1,HIGH); //stops first drop
delay (Delay1); //wait time for second drop to start
digitalWrite (relayWD1,LOW); //start of second drop
delay (dropSize2); //duration of waterflow
digitalWrite (relayWD1,HIGH); //stops second drop
delay (DelayFlashFire1); //wait time to fire flash
digitalWrite (relayFlash,LOW); //fire flash
delay (DelayFlashOff); //time to keep flash open (very short time)
digitalWrite (relayFlash,HIGH); //close flash relay
delay (DelayCamClose); //wait time to close camera shutter
digitalWrite (relayCam,HIGH); //close camera shutter
}
else if(button1Read==1)
{digitalWrite (relayWD1,HIGH); //no signal to waterdrop
digitalWrite (relayCam,HIGH); //no signal to camera
digitalWrite (relayFlash,HIGH); //no signal to flash
}
}
The abstraction represented by your update() method is tortured and confusing to me. My suggestion is to replace update() with two methods startDrop() and stopDrop(). startDrop() records a startTimestamp (like you've done with currentMillis but a class member variable) and opens the solenoid. stopDrop() compares the elapsed time and returns false if not enough time has passed and the solenoid remains open, or returns true if enough time has passed and the solenoid is closed.
class Solenoid
{
...
private unsigned long startTimestamp;
void startDrop()
{
startTimestamp = millis();
digitalWrite(SolenoidPin, LOW);
}
bool stopDrop()
{
if ((millis() - startTimestamp) >= OffTime)
{
digitalWrite(SolenoidPin, HIGH);
return true;
}
return false;
}
...
}
void loop()
{
...
drop1.startDrop();
drop2.startDrop();
while (drop1.stopDrop() == false);
while (drop2.stopDrop() == false);
...
}
That's just a rough draft. You might want to add some error checking to ensure that, for example, the solenoid was closed before startDrop() or that startDrop() was called before stopDrop().
With an Arduino I'm taking analog input from a potentiometer. With the input I regulate how much my lamp blink. The problem is when I try to have a button that turn the lamp on and off so that it blinks or not blinks. I can make the button turn on the lamp but I cannot get it to turn the lamp off.
My loop that makes the lamp blink has a variable that has to be 1 for it to run. However when i change the variable to 0 with an if statement the blink loop does not stop and the lamp keeps blinking.
Here is my code:
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int buttonPin = 11; //select the pin for the button
int buttonState = 0; //variable to start and stop the led
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
if(digitalRead(buttonPin)==HIGH){
buttonState = 1;
delay(1000) //So that buttonState does not instantly change back
}
if(digitalRead(buttonPin)==HIGH && buttonState == 1;){
buttonState = 0;
delay(1000) //So that buttonState does not instantly change back
}
while(buttonState == 1){
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}
}
Thanks for helping!
A while inside loop is always suspicious, and you provide a good example.
Inside the while loop buttonState will never change, thus you have a while forever
Simply change it to an if and your sketch will behave better.
Showing that delay(1000); is not optimal for button handling. You rather want to handle state changes (and consider bouncing buttons). But that's an advanced question. :)
I am using Arduino Nano, Bluetooth module HC-05 and one push button.
I want to rotate the motor when it receives signal from the Bluetooth and stop the rotation when the push button is pressed but here the Bluetooth signal should not be disconnected the motor should stop its rotation when BLuetooth is connected and the button is pressed.
The problem is when signal is passed through Bluetooth or serial , the motor rotates but when we push the button to stop the rotation the motor doesn't stops.
Below is what I tried.
#include <SoftwareSerial.h>
SoftwareSerial Bluetooth(10,6);
const int buttonPin = 5; // the pin that the pushbutton is attached to
const int motorPin = 9;
const int ledPin = LED_BUILTIN; // the pin that the LED is attached to
int buttonState = 0; // current state of the button
int Data;
void setup() {
Serial.begin(9600);
Bluetooth.begin(9600);
Bluetooth.println("Send 1 to open LOCK. Send 0 to close LOCK");
Serial.println("Send 1 to open LOCK. Send 0 to close LOCK");
delay(1000);
Bluetooth.println("Waiting for command...");
Serial.println("Waiting for command...");
pinMode(buttonPin, INPUT);
pinMode(motorPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
if (Bluetooth.available())
{
Data=Bluetooth.read();
if(Data=='1'){
Serial.println("Motor rotating");
Serial.println(buttonState);
digitalWrite(motorPin, HIGH);
}
if (Data=='1' and buttonState == 1){
Serial.println("Motor stop");
Serial.println(buttonState);
digitalWrite(motorPin, LOW);
digitalWrite(ledPin, HIGH);
}
else{;}
}
}
I tried only to control with the push button only which works well. When the button is pushed it motor stops and when unpushed it rotates.
I found the following problems with your code
The variable Data you defined as integer but you are comparing it with string '1'
In your first if condition you are not checking button status before rotating motor, make it
if(Data==1 && buttonState == 0)
Otherwise both the condition will execute when buttonState =1, which will give unexpected result of starting and stopping the motor intermittently.
When I push the button it turns the KY008 off but when I click it again it won't turn it off, but if I jiggle the Laser Diode a little bit the KY008 turns back on.
Code:
int LED = 12;
int BUTTON = 4;
void setup(){
pinMode(LED,OUTPUT);
pinMode(BUTTON,INPUT);
}
void loop(){
if(digitalRead(BUTTON) == HIGH){
digitalWrite(LED,HIGH);
}else{
digitalWrite(LED,LOW);
}
}
If you use INPUT you need to have a physical pullup (or pulldown) resistor (typically 10k).
Otherwise use INPUT_PULLUP to use the Arduino internal pullup resistors
pinMode(BUTTON, INPUT_PULLUP);
Make sure that your button closes the circuit to ground when pressed.
Also when reading a button you will have a lot of bouncing.
The easiest way to prevent the bouncing is to add a delay between reads.
void loop(){
if(digitalRead(BUTTON) == HIGH){
digitalWrite(LED,HIGH);
}else{
digitalWrite(LED,LOW);
}
delay(100);
}
I've just started tinkering with the arduino and i'm getting my head around the basics. I have a push button hooked up so so i get a serial print when it's pushed.
int button = 3;
void setup() {
Serial.begin(9600);
pinMode(button, INPUT_PULLUP);
}
void loop() {
if (digitalRead(button) == LOW) {
Serial.print("pressed\n");
}
}
Now when the button is pressed it'll print pressed a bunch until released. Now my next step is hook up an LED and I want to use the button as a toggle. Press it the first time, it'll come on, press it a second, it'll turn off. But this will run hundreds of times while the button is pressed. How do I get around this? Thanks
int led = 5;
int button = 3;
void setup() {
Serial.begin(9600);
pinMode(button, INPUT_PULLUP);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
void loop() {
if (digitalRead(button) == LOW) {
Serial.print("pressed\n");
toggleLed(led);
}
}
int toggleLed(int led){
if (digitalRead(led) == LOW) {
Serial.print("set on");
digitalWrite(led, HIGH);
} else {
Serial.print("set off");
digitalWrite(led, LOW);
}
}
There is an example code that comes with the Arduino IDE called the State Change Example. Study it. Basically you need to have a variable to remember the state of the button the last time you pressed it and you only react to changes in the button state. Instead of running your code anytime the button pin IS low, you run your code anytime the button pin changes from high to low.