momentary push-buttons functioning to run a if statement

Hi guys;

My code has successfully run when I run when i have written the code for each pin, however adding them together as one does not seem to work. Any ideas?
I am curious about the timing?

senarios i am trying to run;

  1. temp sensor turns a pin, on/off
    2.'go’Button press runs if statement with timing
    3.‘return’ button runs if statement.
    4.potentiometer postition in relation turns on/off motor.

Thats it:)

ideas are appreciated.
Also I am now receiving an error i think i removed a { or (.

code below, or linked .ino:

//button
#include <ButtonDebounce.h>
ButtonDebounce button2(2, 50); //return button
ButtonDebounce button1(1, 50); //go button

//photosensor
#define Sensor2 A2 //Photoresistor is connected to pin A2 top laser trip *not sure if A2 or 0
int val = 0; //val is used to store state of input pin for photoresitor or the other one?
//time elapsed
#include <elapsedMillis.h>
elapsedMillis sinceTest1;
elapsedMillis sinceTest2;
elapsedMillis sinceTest3;

//temp
#include <OneWire.h>
#include <DallasTemperature.h>
int greenLedPin = 3; // Pin Green LED is connected to
int temp_sensor = A4; // Pin DS18B20 Sensor is connected to
float temperature = 0; //Variable to store the temperature in
int lowerLimit = 2; //define the lower threshold for the temperature
int upperLimit = 3; //define the upper threshold for the temperature
OneWire oneWirePin(temp_sensor);
DallasTemperature sensors(&oneWirePin);

#include <Servo.h>
Servo flapper; //servo

void setup() {

pinMode(Sensor2, INPUT); //photoresitor

// motor shield info: makerfabs OAS0000HB
// Bottom motor controll pin initiate;
pinMode(A1, INPUT); //looking at it LEFT POTENTIOMITER
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(9, OUTPUT); // Speed control

// Push motor controll pin initiate;
pinMode(A0, INPUT); //looking at it right POTENTIOMITER
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(10, OUTPUT); // Speed control

//Enable the Motor Shield output;
pinMode(6, OUTPUT);
digitalWrite(6, HIGH);

flapper.attach (3); //servo

//cooler
pinMode(A5, OUTPUT);

//potentiometer
pinMode(A3, INPUT);

sensors.begin();

}

void loop() {

//***temp
sensors.requestTemperatures();
temperature = sensors.getTempCByIndex(0);

if (temperature <= lowerLimit) { //lower limit
digitalWrite(A5, HIGH);
}
else if (temperature >= upperLimit) { //upperlimit
digitalWrite(A5, LOW);
//*****would like to add a function where it only reads once per second, no delay.

}

//***go button
// go button sequence:
//flapper open
//‘push motor’ down - speed is via. potentiometer, runs non stop till photoresistor is tripped then runs for 2s.,
//when the photoresistor is tripped at a reading of 100,
//‘bottom motor’ then turns on
// after 1.75 seconds ‘push motor’ goes up @full speed, and flapper close
//.25 second later or 2 seconds total time after photoresitor tripped push motor turns off
//at 2.2 seconds total after trip ‘bottom motor’ turns off.
//Everything ends and turned off. Wait for next button push.

button1.update();
if (button1.state() == HIGH) {

flapper.write (40); //40 degrees flapper open

//push motor down
digitalWrite(6, HIGH);
analogWrite(10, analogRead(A0));
int blue = map(A0, 0, 1023, 0, 255); //convert from analog to digital
digitalWrite(7, HIGH);
digitalWrite(8, LOW);

val = analogRead(Sensor2); // bottom photoresistor trigger for sequence
if (val < 100) { // if the sensor goes below 100…

analogWrite(9, analogRead(A1)); //bottom motor on
int blue = map(A0, 0, 1023, 0, 255); //convert from analog to digital
analogWrite(9, 0);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);

}
// time past ms

if (sinceTest1 >= 1750)
{
sinceTest1 = sinceTest1 - 1750; //1.75sec
digitalWrite(6, HIGH);
analogWrite(10, 255); //push motor up @ full speed
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
flapper.write (60); //60 degrees flapper close

}
if (sinceTest2 >= 2000)
{
sinceTest2 = sinceTest2 - 2000; //2sec.
analogWrite(10, 0); //push motor off
digitalWrite(7, LOW);
digitalWrite(8, LOW);

}

if (sinceTest3 >= 2200)
{
sinceTest3 = sinceTest3 - 2200; //2.2sec
analogWrite(9, 0); //Bottom motor off
digitalWrite(5, LOW);
digitalWrite(4, LOW);

}
else {
digitalWrite(6, LOW); //shield off
analogWrite(10, 0); //push motor off
digitalWrite(7, LOW);
digitalWrite(8, LOW);
analogWrite(9, 0); //bottom motor off
digitalWrite(5, LOW);
digitalWrite(4, LOW);
flapper.write (70); //70 degrees flapper close

}
}

//***Return button
// push motor up @ full speed (255).

button2.update();
if (button2.state() == HIGH) {

digitalWrite(6, HIGH);
analogWrite(10, 255); //push motor up full speed
digitalWrite(7, HIGH);
digitalWrite(8, LOW);

}
else {
digitalWrite(6, LOW); //motor shield off
analogWrite(10, 0); //push motor off
digitalWrite(7, LOW);
digitalWrite(8, LOW);
analogWrite(9, 0); //bottom motor off
digitalWrite(5, LOW);
digitalWrite(4, LOW);
}
}

//***Start potentiometer postition, run this function

//potentiometer sensor
if (analogRead(A3) > 1000) // if the petentiometer goes above 1000 (Top)…
{
digitalWrite(6, LOW); //motor shield off
analogWrite(10, 0); //push motor off
digitalWrite(7, LOW);
digitalWrite(8, LOW);
analogWrite(9, 0); //bottom motor off
digitalWrite(5, LOW);
digitalWrite(4, LOW);
}
else {

}

if (analogRead(A3) < 20) // if the potentiometer goes below 10 (BOTTOM THEN RETURN TO TOP)…
{
digitalWrite(6, HIGH);
analogWrite(10, 255); //push motor up full speed
digitalWrite(7, HIGH);
digitalWrite(8, LOW);

}
else {

}

//END of potentometer function

motorcode2ndtrynewbutton.ino (5.43 KB)

Please use code tags when posting code, most ppl do not want to download and save your sketch in order to view it :)

“runs if statement”??? What do you mean?

It sounds like you need a State Machine

to clarify: 'runs if statement',

I am trying to say that a momentary push-button makes the code do a set of things.

push a button a bunch of stuff happens

Thank you for the input, I read State Machine, that looks beyond my basic coding ability.

For the 'go' button the idea of the if, if, if, if, if, else. A motor turns on it can keep going until the senario makes it turn off, of either photoresistor, time or potentiometer.

You just described a state machine. If it's in the running state, it keeps running until one of the inputs tells it to leave that state. (Time can be an input for a state machine. eg. "run for 8 seconds or stop button is pushed")

Hello Morgan thank you so much for the input, how are you suggesting the state machine would be added to the code. I am having a hard time deciphering State Machine to transfer to into this code. Do you have any more ideas on what i should delete and add to it to make it function?

I have only started learning this 2 months ago, forgive me if I am asking the wrong questions.

Thanks

It looks like button1 is on Arduino pin 1. That's the TX pin and should not have a button on it.

It might be easier to spot pin conflicts if you defined constants at the top of the file for all the pins you are using instead of directly using numeric values throughout the code. You should also delete the ones you are not using, like greenLedPin.

This is difficult to explain in text only. It really needs a diagram. But here goes...

Start your state machine with a "start" state. Write the word "start" in the middle of a piece of paper and draw a circle around it. "Start" holds all the motors off with digitalWrite() or whatever it needs to do to be ready and waiting. This is the state that you want to be in when setup() is finished.

Now you already know about debouncing and you can use a library, but for a simple tutorial on state machines, I'm going to do all of the debouncing in the main state diagram.

What causes it to leave that "start" state and do something different? Maybe the motor-start button is pushed. So draw an arrow out of that circle with "motor-start button goes down". You definitely want to debounce this button, so we need another state that waits 20 milliseconds to be sure that the person really wanted to push that button and it wasn't just an electrical glitch.

So that arrow goes to a state that we'll call "wait 20ms". What causes it to leave that state? If the button is released (bounces) in less than 20ms then just go back to "start". If the 20ms timer expires, we want another arrow leading out of this state to the "motor running" state. Note that with switch bouncing, it will cycle between "start" and "wait 20ms" many times. When it actually leaves "wait 20ms" the bouncing has stopped and the user's finger is still leaning on the button.

While we're in the "motor running" state, we continually write the run command to the motor - digitalWrite() the pins or whatever we need.

So what causes it to leave the "motor running" state? Well, we don't care if the user is still holding the motor-start button, so we don't even need to look at that button to find out when they take their finger off the button. But maybe there's an emergency-stop button. Maybe we want emergency-stop to be really sensitive. No debouncing at all. If I'm getting dragged into the machine by my loose sleeve then I want the emergency-stop to be sensitive to the lightest touch with my free hand.

So draw an arrow going out of "motor running" back to "start" and label it "emergency stop touched". "Start" doesn't care if emergency-stop bounces or even if it's held down forever. So there's no arrow leading out of the "start" that responds to changes to the emergency-stop button.

Now you have a complete state diagram and you can start and stop the motor with nicely debounced buttons. But look at it again. What happens if I hold down emergency-stop and push the motor-start button at the same time? It will go through the "wait20ms" state and then "motor running" but it will leave "motor running" almost instantly. For a motor, you will never see it move in the few microseconds that it spent in that state. But for some other processes, this might be important.

That last step of saying "What happens if?" is where you solve all the real problems. That's where you cover all the possible alternatives of accidentally-on-purpose holding down all the buttons or unplugging some vital sensor in the middle of a critical operation. It's also where you add a lot more states that you didn't think of at first. If you decide the emergency-stop will really stop it and it will stay stopped until a reset button is pressed [u]for 1 second[/u] and released then there's a lot more states to add.

Morgan thank you for some explanation.

Yes the button should have some 'states' if the button is held down for a long time or if a second button is pushed whilst in another. hmm still quite clear as mud :) but closer.

Draw the diagram for yourself. That will clarify the mud.

  int blue = map(A0, 0, 1023, 0, 255);  //convert from analog to digital

Here you're mapping the number 14 not any reading from the pin. This will always give the same answer. I think you want to use the reading from the pin not the pin number.