Go Down

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

Grumpy_Mike


Code: [Select]
    blinking_millis -= DESIRED_TIME; //reset blinking millis
Minus?

Paul have you not met our dear Henry before? He has been giving out hardware advice of the same quality for some months now. There should be a sticky for newcomers to ignore him.

PeterH


My first thought, though is that you shouldn't be using delay() in your blink code and be using code from the Blink Without Delay example.


^^^ That.

Throw away the 'blink' code and replace it with 'blink without delay'. The code in 'blink without delay' uses a non-blocking technique which means that no part of your sketch needs to wait for something to happen. This technique enables you to combine many piece of functionality in a single sketch without them interfering with each other. Your inquiry is a classic illustration of why this approach is such a good idea. I'd go so far as to say the 'blink' example should be removed from the standard distribution, just because it encourages people to take an approach which is inherently not suitable for more complex projects.
I only provide help via the forum - please do not contact me for private consultancy.

APieceOfToast

ok, I went and redid the program to clean up the formatting and as many of the errors I could find. here's where it's at now:
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);
  }
}





It compiles and uploads without a problem but when I open the serial monitor to monitor the count it's counting the number 1 repeatedly extremely quickly. And I have no idea why. Any thoughts or suggestions?

PaulS

Code: [Select]
  {
    if (buttonUpState == HIGH && prevBtnUp == LOW)
      if (millis() - lastBtnUp > transInt)

        channel++;

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

Curly braces where you don't need them. No curly braces where you should have them. Indenting doesn't mean squat to the compiler. Curly braces do. Use them. Even when you don't think you need to.

APieceOfToast


Code: [Select]
  {
    if (buttonUpState == HIGH && prevBtnUp == LOW)
      if (millis() - lastBtnUp > transInt)

        channel++;

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

Curly braces where you don't need them. No curly braces where you should have them. Indenting doesn't mean squat to the compiler. Curly braces do. Use them. Even when you don't think you need to.


PaulS would you please explain what you mean by that? when I add in the { and } before and after the if statements the compiler fails citing the statement "expected primary-expression before '}' token"
I must be misunderstanding what you mean by that.

In response to the use them even when I don't need to, that just causes more problems with compiling as the error message then reads "too many left curly brackets" despite having double checked to make sure I've closed all the open brackets.

Go Up