Looking for help finishing off this project please.

Moderator edit : This topic has been moved here at the request of the OP. Later in the topic he/she says that they are prepared to pay for help

Hello all, I am doing a project that uses 4 arm switches and I need this beacon program to run when one or more of the arm switches are activated but im having a hard time figuring out the code. Im sure its easy but its just beyond my skill. If anyone can help it would be amazing.. Thank you! Ill be running a resistor between the pins and ground.... switch is between the pins and 5V

Rocket_RX_Beacon.ino (348 Bytes)

Do you know how to read the state of an input ?
Have you looked at the Button example in the IDE ?

Im very new to arduino, I can use a switch or button to make an LED turn on or off but I dont know how to run that beacon program with my switch.

If you can use a button to make an LED turn on/off, then apply that same logic to all 4 of your buttons.

  1. read all 4 buttons [digitalRead()]
  2. if any of the buttons are HIGH, run your beacon code [if() statement, 'or' keyword]

Give it a try.

its the "run beacon code" that i dont know how to write

put that code inside your if() statement

GRVDIGR:
I need this beacon program to run when one or more of the arm switches are activated!

What's actually supposed to happen?

GRVDIGR:
Ill be running a resistor between the pins and ground.... switch is between the pins and 5V

Make your life easier and lose the resistor, pinMode(pin, INPUT_PULLUP), wire the switch from the pin to ground, and invert the logic to look for a low as a press.

blh64:
but that code inside your if() statement

Ah I guess so haha, i suppose Id have to write it out a statement for each of the 4 buttons, im sure there is an easy way but ill start with that. Thank you

fishboneDiagram:
What's actually supposed to happen?

Make your life easier and lose the resistor, pinMode(pin, INPUT_PULLUP), wire the switch from the pin to ground, and invert the logic to look for a low as a press.

Simple really.... flip a switch and runs the LED code.
Im using the internal pullup resistors, no external ones used. Without any form of resistor on a button.... wont it float?

GRVDIGR:
Simple really.... flip a switch and runs the LED code.

But all that code does is increase the value of brightness for ever....

void loop() {  
analogWrite(output_pin9,brightness);
delay(4);
brightness++;
}

Is that what you want? (Not that it can analogWrite() beyond 255 anyway.) What's supposed to happen when the led gets to full brightness?

And is there only one led, or one per switch?

So pretend we know nothing, and rather than saying "runs the LED code" explain what should actually happen.

GRVDIGR:
Im using the internal pullup resistors, no external ones used.

Not according to the sketch you posted, and what you said earlier

running a resistor between the pins and ground

pinMode(A7, INPUT);// Arm 1
pinMode(A6, INPUT);// Arm 2
pinMode(A5, INPUT);// Arm 3
pinMode(A4, INPUT);// Arm 4

GRVDIGR:
Without any form of resistor on a button.... wont it float?

Yes, but if you don't use INPUT_PULLUP you need an external one.

Which Arduino is used?

pinMode(A7, INPUT);// Arm 1
pinMode(A6, INPUT);// Arm 2

For UNO, Mini, Nano classic A6 and A7 are analog input only and don´t provide a pullup.

My apologies it was early when I posted and coffee didn’t hit me yet! Ok so let’s ignore the internal pull-up side of things, that’s for a while other part of the project that is working fine. The 4 analog “arm” input pins are on a switch and will need an external resistor so it doesn’t float.... correct me if I’m wrong. I’m using a Nano. Each one of these switches will control 4 different LEDs on PWM pins.

As for my LED code, it starts in the off position, gradually increases brightness, blinks twice fast at full brightness, dims to off, then loops.

This is what I’m using for the LED code

If that is what you are using for your LED code, then why did you attach different code in your original post?

You should start with that code [and include it properly, not a link] and then begin adding your switches

The original code

byte led = 9;                   // which pin LED is connected to MUST USE PWM PIN
byte intensity = 0;             // value for PWM setting on led pin, LED brightness
byte rampUp = 1;                // amount the brightness steps for each iteration of the loop
byte stepTimer = 40;            // duration between steps of the loop
float flareTimer;               // duration the flare effect stays bright
byte flares = 1;                // number of "strobe" effects to perform

void setup() {
  pinMode(led, OUTPUT);
  analogWrite(led, 0);
  flareTimer = (stepTimer * 0.9);
}

void loop () {
  intensity = intensity + rampUp;
  analogWrite(led, intensity);
  if (intensity >= 30) {
    flareEffect(flares, flareTimer, led);
  }
  if (intensity <= 0) {
    delay(350);
    rampUp = -rampUp;
  } if (intensity >= 30) {
    rampUp = -rampUp;
  }
  delay(stepTimer);
}

void flareEffect (byte flares, float flareTimer, byte led) {
  for (int flareCount = 0; flareCount < flares; flareCount++) {
    analogWrite(led, 30);
    delay(40);
    analogWrite(led, 255);
    delay(flareTimer);
  }
}

Start there. Rename your loop() function to be beacon(). Add a new loop() function that checks your switches and if they are HIGH/LOW [depending on your wiring...] then call the beacon() function.

byte led = 9;                   // which pin LED is connected to MUST USE PWM PIN
byte intensity = 0;             // value for PWM setting on led pin, LED brightness
byte rampUp = 1;                // amount the brightness steps for each iteration of the loop
byte stepTimer = 40;            // duration between steps of the loop
float flareTimer;               // duration the flare effect stays bright
byte flares = 1;                // number of "strobe" effects to perform


void setup() {
  pinMode(led, OUTPUT);
  analogWrite(led, 0);
  flareTimer = (stepTimer * 0.9);

  pinMode(A5, INPUT);// Arm 1
  pinMode(A4, INPUT);// Arm 2
  pinMode(A3, INPUT);// Arm 3
  pinMode(A2, INPUT);// Arm 4
}

void loop() {
  // put your code here to check the state of the switches
}


void beacon() {
  intensity = intensity + rampUp;
  analogWrite(led, intensity);
  if (intensity >= 30) {
    flareEffect(flares, flareTimer, led);
  }
  if (intensity <= 0) {
    delay(350);
    rampUp = -rampUp;
  } if (intensity >= 30) {
    rampUp = -rampUp;
  }
  delay(stepTimer);
}

void flareEffect(byte flares, float flareTimer, byte led) {
  for (int flareCount = 0; flareCount < flares; flareCount++) {
    analogWrite(led, 30);
    delay(40);
    analogWrite(led, 255);
    delay(flareTimer);
  }
}

The actual code is left as an exercise.

FYI... the "beacon" code is not very robust. If your ramp variable every changes it may break since the intensity variable is of type byte (0-255) and the code checks to see if it is less than 0 (which is never will be) and if your ramp is such that it doesn't actually reach 0, it will never change ramp direction.

Oh dang I did send the wrong vid/code, I believe that was the one I was going to go with but settled on this one. Ill see if I can put it together and re post what I come up with.

Hmmm.... How do you post code in the box like that?

Rocket_RX_Beacon.ino (348 Bytes)

GRVDIGR:
Hmmm.... How do you post code in the box like that?

By reading the sticky posts at the top of the forum like this one and look at item #6.

int output_pin9 = 9;    // LED connected to digital pin 9
int brightness = 0;

#define buttonPin4 A4
#define buttonPin5 A5
#define buttonPin6 A6
#define buttonPin7 A7

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;

void setup() {

  pinMode(output_pin9, OUTPUT);

  pinMode(buttonPin4, INPUT); //Arm 1
  pinMode(buttonPin5, INPUT); //Arm 2
  pinMode(buttonPin6, INPUT); //Arm 3
  pinMode(buttonPin7, INPUT); //Arm 4
}

void loop() {
  // put your code here to check the state of the switches
}


void beacon() {
  analogWrite(output_pin9,brightness);
  delay(3);
  brightness++;
}

Thank you!

I'm really confused about what you're trying to do and which code is which.

The last post (#16) has a beacon function that's never called, that will only increase the brightness on pin 9 when it is called. You have 4 INPUTs (still not with _PULLUP so not sure of the situation with resistors) and 1 output. Do all the inputs control the same output, or is each supposed to have its own led?

Earlier you spoke of flipping a switch, yet the pins are named buttonPin so it's not clear if you have toggle switches or momentary buttons.

I think, but by no means sure :slight_smile: that you want 4 leds and 4 toggle switches, and when any switch is closed it strobes its associated led like in the video (fade in, blink twice, fade off, repeat); and when a switch is open, its led is off.

But I'm not sure what this means:

The 4 analog "arm" input pins are on a switch

Is that 4 pins on 1 switch, or 4 switches? Why analog? (Although of course the analog pins can be used as digital pins, but it seems there's some significance attached to the word analog as you use it?)

So can we back up, and ask that you explain what you want, from the top so to speak, including a circuit diagram.

edit, having looked at the youtube guy's code: If you do want 4x independent strobing leds, the code's going to need a total makeover to get rid of all the delay()s

Meantime, I wrote a sketch which "might" do what you want. Sort of. I say "might" since I don't know what you want :wink:

It has a bunch of leds' pins in an array, with associated switch pins in another array.

Then it has some arrays (could / should probably be in a struct) to hold the beacon characteristics (timing etc) for each led.

In loop() it uses a for() to zoom through the array of leds (to all intents and purposes instantaneously). If an led's switch is open, the led is set off. If the led's switch is closed, it "beacons" that led, that is if fades it up or down (delay()-lessly) and gives the rotating effect by jumping to full brightness at some threshold similarly to the youtube guy's.

The beacon effect may or may not be what you want. Nor is my understanding of what the switches are supposed to do by any means certain, nor am I certain you have the leds and switches in pairs.

So confirm that all first, then if my code is anything like what you want, I'll post it.

Oops, a mistake, I was just dumb to post this.
I'm sorry....