Go Down

Topic: using pullstime  (Read 1 time) previous topic - next topic

Matyk

I tried both methods both compile, serial monitor looks fine but the still don't do anything. Is it still in the program?


Code: [Select]
#include <Servo.h>
Servo m1;
const byte buttonPin = A2;
const int ledPin = 9;
int writeValues[5] = {1000, 1250,1500,1750,2000};
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT_PULLUP);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);

}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println(buttonPushCounter);
      Serial.print("buttonPushCounter % 5 = ");
      Serial.println(buttonPushCounter % 5);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

  lastButtonState = buttonState;

  {
  if (buttonState == LOW);
  m1.writeMicroseconds(writeValues[5]);
  }
}





Code: [Select]
#include <Servo.h>
Servo m1;
const byte buttonPin = A2;
const int ledPin = 9;

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT_PULLUP);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);

}

void loop() {
  buttonState = digitalRead(buttonPin);

   if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
     Serial.println(buttonPushCounter);
      Serial.print("buttonPushCounter % 5 = ");
      Serial.println(buttonPushCounter % 5);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
 
  {
  if (buttonState == HIGH);
  byte index = buttonPushCounter % 5;
  m1.writeMicroseconds(1000 + (index * 250));

    }
  }








UKHeliBob

Code: [Select]
    if (buttonState == LOW);
    m1.writeMicroseconds(writeValues[5]);

Three things wrong here

1 - the if is terminated by a semicolon.  The semicolon is the only code whose execution depends on whether the if test returns true.  Remove it

2 - the array entry 5 does not exist.  The array values are numbered 0 to 4 as I warned you in an earlier post

3 - you are meant to use the value of buttonPushCounter % 5 as the array index to select the correct value for the value to write to m1.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Matyk

oh wait so would I do it like this, for every value...

{
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[0]);
  }

  {
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[1]);
  }

  {
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[2]);
  }

  {
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[3]);
  }

  {
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[4]);
  }
}

Matyk

sorry wrong thing...


Code: [Select]
{
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[0]);
  }

  {
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[1]);
  }

  {
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[2]);
  }

  {
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[3]);
  }

  {
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[4]);
  }
}

UKHeliBob

NO, NO, NO NO !

You have an array of 5 values numbered from 0 to 4
Code: [Select]

int writeValues[5] = {1000, 1250,1500,1750,2000};


You have a number between 0 and 4 derived like this
Code: [Select]
buttonPushCounter % 5)

You do not need to test its value 5 times.  Instead you use its value to select the associated value from the array like this
Code: [Select]
m1.writeMicroseconds(writeValues[buttonPushCounter % 5)]);
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Matyk

Code: [Select]
#include <Servo.h>
Servo m1;
const byte buttonPin = A2;
const int ledPin = 9;
int writeValues[5] = {1000, 1250,1500,1750,2000};
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT_PULLUP);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);

}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println(buttonPushCounter);
      Serial.print("buttonPushCounter % 5 = ");
      Serial.println(buttonPushCounter % 5);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

  lastButtonState = buttonState;

  {
  m1.writeMicroseconds(writeValues[buttonPushCounter % 5]);
  }
}

UKHeliBob

#81
Mar 25, 2019, 11:17 am Last Edit: Mar 25, 2019, 11:18 am by UKHeliBob
Does it work ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Matyk

I can't test it right now...

Matyk

Is it the f***in code again?

Code: [Select]
#include <Servo.h>
Servo m1;
const byte buttonPin = A2;
const int ledPin = 9;
int writeValues[5] = {1000, 1250,1500,1750,2000};
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT_PULLUP);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);

}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println(buttonPushCounter);
      Serial.print("buttonPushCounter % 5 = ");
      Serial.println(buttonPushCounter % 5);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

  lastButtonState = buttonState;

  {
  m1.writeMicroseconds(writeValues[buttonPushCounter % 5]);
  }
}

UKHeliBob

Is what in the code ?

You forgot to do the m1.attach() in setup()
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Matyk

oh the message didn't send itself, well basically I now can't upload the code to the Arduino.

Code: [Select]
Sketch uses 3268 bytes (10%) of program storage space. Maximum is 32256 bytes.
Global variables use 273 bytes (13%) of dynamic memory, leaving 1775 bytes for local variables. Maximum is 2048 bytes.
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00


UKHeliBob

Quote
I now can't upload the code to the Arduino.
That is not a problem with the code.  Close down the IDE, unplug the Arduino then restart the IDE and plug the Arduino back in and check that the COM port that it is using is correct.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Matyk

Quite a problem here, I did what you said and it didn't work, then I completely removed the Arduino software and installed the Arduino software again; it is still not working or I can't upload it. I think that something happened to the Arduino itself even though I didn't do anything abusive. What do you think?
PS: I spent like 4 hours on this, trying programmers and other things.


Code: [Select]
Sketch uses 3268 bytes (10%) of program storage space. Maximum is 32256 bytes.
Global variables use 273 bytes (13%) of dynamic memory, leaving 1775 bytes for local variables. Maximum is 2048 bytes.
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00

UKHeliBob

It is possible that you have a dead board but that would not be caused by the program

Personally I would try another Arduino, another USB cable, another USB port, another PC or all four.  I assume that you have rebooted the PC.

Do any COM ports show up in Device Manager (assuming that you are using Windows) ?  Do the COM ports change in Device Manager when you plug or unplug the Arduino ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Matyk

Sorry for the long hile, I had to get a hold of a new computer...

Go Up