Sketch works 99% although curious response - any assistance?

HI,

Not sure why that would happen, but I only wrote and compiled, you are on the spot with the hardware, good fix.

A picture of your project would help to, so we can see your component layout.

So you eventually want 4 inputs.
If you see in my code I give each input a decimal/binary value.
The first two are 1 , 2 the next inputs would be 4 and 8.
This way each switch combination will have its own unique value, then you can use switch.. case to do what you want with the outputs.

Glad to see you have made some progress.

Thanks.. Tom... :grinning: :coffee: :australia:
PS, Can you please post a copy of my code with your edits, so we can both be on the same page?

Sketch is the same as your except I deleted the DEFAULT case and it worked.

I haven't yet reviewed your sketch to fully understand your inputs and bins etc.

Attached is the wiring diagram and the your sketch minus the DEFAULT case which works.

Ideally, what this project is to do is for the arduino to be on with cyan LED. When amplifier is on, it supplies 12v signal in to pin 9. Arduino sees this, and activates the linear actuator to open the centre speaker door making the LED green whilst doing so. Once the door is open, the amp is on, so the arduino LED will be magenta. If amp turns off, arduino will close door making LED red whilst doing so, then return to cyan colour to show arduino power on but amp off. Rocker switch is for manual open/close of door hence neutral position, position 1 open/green and position 2 closed/red.
I have built the cabinet, designed and 3d printed the clevisis for the actuator which allow adjustment of door closed position, and it works perfectly, Now just to have it working automatically on amplifier switching...... Oh and I am using an Uno instead of a Nano as couldn't get nano working.....for now

// Prior to Setup
int green_light_pin = 5; ;// Green pin connected to digital pin 5
int blue_light_pin = 6; // Blue pin connected to digital pin 6
int red_light_pin = 7; // Red pin connected to digital pin 7


int switch_on_pin = 11; // switch on pin connected to digital pin 11
int switch_off_pin = 10; // switch off pin connected to digital pin 10


int switch_on_val = 0; // value used to store the state of the input pin
int switch_off_val = 0; // value used to store the state of the input pin

int switchon_valbin = 0; // value used to store the state of the input pin
int switchoff_valbin = 0; // value used to store the state of the input pin
int LEDVal = 0;

// Setup
void setup() {
  pinMode(green_light_pin, OUTPUT); // sets the greenPin as an output
  pinMode(blue_light_pin, OUTPUT); // sets the bluePin as an output
  pinMode(red_light_pin, OUTPUT); // sets the redPin as an output


  pinMode(switch_on_pin, INPUT_PULLUP);    // declare on switch as input + activate internal pull-up resistor
  pinMode(switch_off_pin, INPUT_PULLUP);    // declare off switch as input + activate internal pull-up resistor


  // turn LED cyan
  RGB_color(0, 255, 255);

  // initialize serial communication:
  Serial.begin(9600);

} // close void setup

// Loop
void loop() {

  switch_on_val = digitalRead(switch_on_pin); // read the state
  if (switch_on_val)  // Assign a value to switch position
  {
    switchon_valbin = 1;
  }
  else
  {
    switchon_valbin = 0;
  }

  switch_off_val = digitalRead(switch_off_pin); // read the state
  if (switch_off_val)  // Assign a value to switch position
  {
    switchoff_valbin = 2;
  }
  else
  {
    switchoff_valbin = 0;
  }
  LEDVal = switchon_valbin + switchoff_valbin;  // Calculate the LED display Value

  switch (LEDVal)    // Select the RGB colour according to switch value
  {
    case 1:  // Red
      RGB_color(255, 0, 0);
      Serial.println("Switch in RED position");
      break;
    case 2:  // Green
      RGB_color(0, 255, 0);
      Serial.println("Switch in GREEN position");
      break;
    case 3:  // Blue
      RGB_color(0, 0, 255);
      Serial.println("Switch in CENTRE BLUE position");
    default: //had to delete this DEFAULT section  to make it work properly SF
      RGB_color(0, 0, 0);
      Serial.println("Switch in UNDEFINED position");
      break;
  }
delay(250);  // delay to slow the monitor update down
}

// this following void command is like a DEFINE FUNCTION command in that it defines rgb color as a command consisting of 3 integers and then as specified in the curly brackets, analogwrites to each of these as specified


void RGB_color(int red_light_value, int green_light_value, int blue_light_value)
{
  analogWrite(red_light_pin, red_light_value);
  analogWrite(green_light_pin, green_light_value);
  analogWrite(blue_light_pin, blue_light_value);

}


  void switch_on()  //switch on subroutine function
  {
  RGB_color(0, 255, 0); // turn green

  Serial.println("LED is green for switch on ");

  }

  void switch_off()  //switch off subroutine function
  {
  RGB_color(255, 0, 0); // turn red

  Serial.println("LED is red for switch on ");

  }
1 Like

Hi,
Well all you need to do is to integrate the actuator instructions, open and close into the RED and GREEN cases and a STOP command in the BLUE case. I would think.

Tom... :grinning: :+1: :coffee: :australia:

Hi TomGeorge, I need new cases I think per my table above as the switch is now sorted but need cases for amplifier on/off, door open/closed

Hi,
Are you using a door open limit switch AND a door closed limit switch, not just one?
If so we need to see how you have them in your circuit.

Tom.. :grinning: :+1: :coffee: :australia:

Not using any limit switches, with the linear actuator it takes slightly less than 15 seconds, so I was going to use a timer during the open/close routines. if you supply a voltage to the actuator, once it reaches the extent of its travel, it just stops until it receives the opposite direction voltage.

So you don't have a closed or open input. If your actuator has internal limits, then you don't need the closed/open check.

If it is already open, then commanding it open, will do nothing for its 15 second duration, and that is not a problem is it?
Same when closed, the closed command when already closed will not do anything to the actuator motor.
So you don't need the open/closed column.

Tom... :grinning: :+1: :coffee: :australia:

You're getting ahead of me now! Yes, I expect so - commanding it to open if it is already open would delay for 15 seconds. However if the flag for open was checked and if already open, then the open command wouldn't do anything might be the way to go?

Hi,

How would you initialise the flag when you booted your code.
How would it know if the door was open or not?

Tom... :grinning: :+1: :coffee: :australia:

thats a good question. Thats where the manual operation via rocker switch comes in or perhaps the code just opens it if the amp is on, and if it's already open then well, it just tries for 15 seconds, then returns to normal?

Hi,
Yes. allsorts of things to check just using just the relay operation to prove your code.

Keep adding Serial.print to keep checking your code status.

Tom... :grinning: :+1: :coffee: :australia:

The Wokwi Arduino simulator is still in development. I noticed that the first time it might not run well, and the second time runs okay. It is constantly improving and new things are added.

Thanks for all your assistance. I'm a bit indisposed at the moment, but will return to sketch tomorrow. Perhaps I can reach out re the best way to address all the cases per my table (if that is the best way) ie per your earlier message re binary inputs 1, 2, 4 8 etc?

1 Like

Ok, so no need to debounce.

I don't know what the best solution is, that depends on the usage.
You could use a 'enum' to assign labels to numbers, or a few variables, or one variable with bit settings.

A few 'bool' variables is also an option. It might not be the best solution for you. I only show how it looks:

bool amplifierOn = false;  // true for 'on', false for 'off'.
bool doorOpen = false; // true for 'open', false for 'closed'.
enum { IDLE, OPEN, CLOSE } switch;

if( amplifierOn && doorOpen && switch == CLOSE)
{
  ...
}

// To check if a 'bool' is false, add a 'not' in front of it.
if( !amplifierOn && !doorOpen && switch == OPEN)
{
  ...
}

// When using aliases.
if( not amplifierOn and not doorOpen and switch == OPEN)
{
  ...
}

For the aliases, see this paragraph at wikipedia.

Hi Paul - quick question if I may. if 12v signal from Amp is received, will activate relays for 15 seconds. SO I digitalwrite it high, delay 15000 MS, digitalwrite it LOW. However with the pressing of the rocker switch, I only want relay to activate while switch is closed. How might I do this?

This might be a great opportunity to see if you can work this out for yourself. It will help consolidate all the learning from the thread so far. Obviously you'll still get help if it doesn't work, so post your attempt. :grinning:

Easy enough, but why? Won't it be more convenient to have the door open or close fully when you use the toggle switch? Is there a reason you want to half-open the door? If so, you could have a quick push of the toggle to fully open or close, or a longer push to open or close until you release the toggle.

Ha ha, now that is getting really smart! In all likelihood the switch wouldn't be used much if at all but it's foreseeable that it might need a 'jog' function although remote. To do what you propose would be ideal

Hey @PaulRB / @SteveThackery, @Koepel, @TomGeorge etc, thanks for all your help. Thought you might want to see finished project. Designed a case in AutoCad and as it turned out, sketched the wiring diagram to help me wire it all up, this was the hardest part fitting dozens of wires in the minute box.

Modelled the case using Fusion360 and had it 3D printed. Houses the Arduino, two relays and switch/led/resent button. Used RCA plugs for the 12v in, amp signal in and linear actuator wires, and the switch to manually open/close the linear actuator.

Bought a 100mm linear actuator, designed clevis mounts for it, the main mount has a screw-adjustable base to allow +/- 5 mm adjustment to ensure door closes perfectly as well as the main clevis screw coming in from an adjacent door in case actuator fails, I can remove it and release the actuator.

Anyway, great little project, not sure how my soldering will last as it was very challenging soldering such small wires, hopefully the heat shrink helps

thanks again for all your help