Thank you. Now the code works very well.
Probably best you post the code to make easier to check what you mean.
I have nothing concrete yet, this question was one of principle. I have not written any code yet. Being the beginning of this activity are many things unknown to me, in conclusion I have many questions. Some of these are relevant, while others may seem foolish to start. For the second category to display little tolerance please.
To be more explicit on the question asked earlier to take the following examples:
const// These constants won't change:
int sensorPin = A0; // pin that the sensor is attached to
const int ledPin1 = A1; // pin that the LED1 is attached to
const int ledPin2 = A2; // pin that the LED2 is attached to
int lastState;
int currentState;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// initialize the sensor pin as an input:
pinMode(sensorPin, INPUT);
}
void loop() {
// read the value of the sensor:
int sensorValue = digitalRead(sensorPin);
int currentState=sensorValue;//store current value of A0 pin
//check if state has changed since last run though loop
if(currentState != lastState){
// turn on the LED1:
if (sensorValue == HIGH) {
digitalWrite(ledPin1, HIGH);
delay(2000);
digitalWrite(ledPin1, LOW);
}
// turn on the LED2:
if (sensorValue == LOW) {
digitalWrite(ledPin2, HIGH);
delay(2000);
digitalWrite(ledPin2, LOW);
}
lastState=currentState;//set lastState equal to currentState to prevent loop
//running until pin A0 value changes
}
}
Code that we discussed, and the following code examples taken from the Arduino, which will put the LED on pin 13, when the button is actuated to pin 2.
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
After combining them I got this code that is functional:
// These constants won't change:
const int sensorPin = A0; // pin that the sensor is attached to
const int ledPin1 = A1; // pin that the LED1 is attached to
const int ledPin2 = A2; // pin that the LED2 is attached to
int lastState=10;
int currentState;
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// initialize the sensor pin as an input:
pinMode(sensorPin, INPUT);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the value of the sensor:
int sensorValue = digitalRead(sensorPin);
int currentState=sensorValue;//store current value of A0 pin
//check if state has changed since last run though loop
if(currentState != lastState){
// turn on the LED1:
if (sensorValue == HIGH) {
digitalWrite(ledPin1, HIGH);
delay(2000);
digitalWrite(ledPin1, LOW);
}
// turn on the LED2:
if (sensorValue == LOW) {
digitalWrite(ledPin2, HIGH);
delay(2000);
digitalWrite(ledPin2, LOW);
}
lastState=currentState;//set lastState equal to currentState to prevent loop
//running until pin A0 value changes
}
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
I wanted to know if this kind of combination of codes is normal or whether it should do otherwise.
I ask this question because the sketch that I intend to do for railway turntable contains two sensors, keyboard control, a stepper motor, an LCD, and all that, I want to make one code to test it separately, then to unite all these codes.
If my way of approaching this problem is unusual, I request someone to correct me.