Go Down

Topic: Combining two sketches, help! (Read 1 time) previous topic - next topic

PaulS

The code snippet I posted should (maybe) be:
Code: [Select]
    if (buttonUpState == HIGH && prevBtnUp == LOW)
    {
      if (millis() - lastBtnUp > transInt)
      {
        channel++;

        lastBtnUp = millis();
        Serial.println(channel);
      }
    }


I'm not positive, because I don't know what you want to have happen when.

If that placement of { and } causes the compiler grief, it is because of something that went wrong earlier. You'll need to post modified code and exact error messages.

APieceOfToast

your correction made the program compile without errors, however now the counting is just not occurring according to the serial monitor. this might be a problem on the physical side since I'm in the process of moving the circuit into its "permanent" home.

To clarify what I want to happen when, I'm using the blink program to run a motor  which in turn moves an arm up and down. I'm using the count program to count how many times the arm breaks the photo gates beam. So I need to blink program to run with the count program so that it is counting continuously while the motor runs.

Here is the current code:
Code: [Select]
//blink without delay
const int ledPin = 9;

int ledState = LOW;
long previousMillis = 0;

long interval = 1000;

// photogate
const int inPinUp = 6;
const int inPinDown = 7;
int channel = 1;
int buttonUpState = 0;
int buttonDownState = 0;
int prevBtnUp = LOW;
int prevBtnDwn = LOW;
unsigned long lastBtnUp = 0;
unsigned long lastBtnDwn = 0;
int transInt = 50;

void setup ()
{
  pinMode (ledPin, OUTPUT);
  Serial.begin(9600);
  pinMode(inPinUp, INPUT);
  pinMode(inPinDown, INPUT);
}

void loop ()
{
  buttonUpState = digitalRead(inPinUp); //begin photogate
  buttonDownState = digitalRead(inPinDown);

 
    if (buttonUpState == HIGH && prevBtnUp == LOW)
    {
      if (millis() - lastBtnUp > transInt)
      {
      channel++;

    lastBtnUp = millis();
    Serial.println(channel);
  }
}
  prevBtnUp = buttonUpState;


  //blink without delay
  unsigned long currentMillis = millis ();

  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;

    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    digitalWrite(ledPin, ledState);
  }
}


(and yes I know indenting doesn't do anything for the compiler but it made it easier for me to read)

PaulS

Quote
but it made it easier for me to read)

Using Tools + Auto Format before posting code would make it easier for ME to read.

Consistently placing the { (I prefer on a new line) makes it easier to read the code.

Using names like lastBtnUp and prevBtnUp in the same code REALLY makes it hard to follow. If a variable contains a state, the name should reflect that. If a variable contains a time, the name should reflect that.

Quote
I'm using the blink program to run a motor

A motor connected to the ledPin? Being turned on and off by the ledState variable? Why? You don't have a damned LED connected.

Anyway, that code does something. You want it to do something. Presumably, those two somethings are not the same thing, or you wouldn't still be looking for help. What either of those two somethings is, though, is not clear.

APieceOfToast

I did use tools > auto format as per your earlier request, and that is exactly what I posted and you read.

I'm using the blink program on the motor because with the setup I have, it allows me to tune the motor to the desired rpm. do you have a different suggestion? I would love a better solution as how its set up now is giving me tons of trouble (see previous experience - none)

Like I said though, I believe the problem is now on the physical side of things and I will continue on with testing until I get it.

Thank you very much I appreciate your help greatly and I will use the within advice for all my future posts.

PaulS

Quote
I'm using the blink program on the motor because with the setup I have, it allows me to tune the motor to the desired rpm. do you have a different suggestion?

Yes. I suggest that you call the variable containing the pin the motor is connect to motorPin, not ledPin. I suggest that you call the variable containing the motor state motorState, not ledState.


Go Up