Arduino programing code problem

Hello everyone i make a simple code
i make that led turns on by push buttom
and i wanna make more like this in one program for another channels
There is a code please help me :frowning:
**int J1 = A0; **
**int O1 = 13; **
int val = 0;
int lightON = 0;
int pushed = 0;


void setup() {
** Serial.begin(9600);**
** pinMode(J1, INPUT_PULLUP); **
** pinMode(O1, OUTPUT);**
** digitalWrite(O1, HIGH);**
}

void loop() {
** val = digitalRead(J1);**
** if(val == HIGH && lightON == LOW){**
** pushed = 1-pushed;**
** delay(100);**
** } **

** lightON = val;**

** if(pushed == HIGH){**
** Serial.println("Light ON");**
** digitalWrite(O1, LOW); **


** }else{**
** Serial.println("Light OFF");**
** digitalWrite(O1, HIGH);**


** } **
}

What are all the asterisks ?

Your post was MOVED to it's current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Here is how you should post a sketch:

const int J1 = A0;
const int O1 = 13;

int val = 0;
int lightON = 0;
int pushed = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(J1, INPUT_PULLUP);
  pinMode(O1, OUTPUT);
  digitalWrite(O1, HIGH);
}

void loop()
{
  val = digitalRead(J1);
  if (val == HIGH && lightON == LOW)
  {
    pushed = 1 - pushed;
    delay(100);
  }

  lightON = val;

  if (pushed == HIGH)
  {
    Serial.println("Light ON");
    digitalWrite(O1, LOW);
  }
  else
  {
    Serial.println("Light OFF");
    digitalWrite(O1, HIGH);
  }
}

So 'J1' is a button and you want to toggle the LED (O1) when the button is pressed? And you watn to have that happen for multiple buttons/LEDs?

Here is a sketch I wrote for detecting state changes and debouncing buttons. Perhaps it will be handy as an example.

const byte ButtonCount = 3;
const byte ButtonPins[ButtonCount] = {2, 3, 4};
const unsigned long DebounceTime = 30;

boolean ButtonWasPressed[ButtonCount];  // Defaults to 'false'
boolean ButtonChangedState[ButtonCount];  // Defaults to 'false'

unsigned long ButtonStateChangeTime = 0; // Debounce timer common to all buttons

void setup()
{
  Serial.begin(115200);

  pinMode(LED_BUILTIN, OUTPUT);
  
  for (byte i = 0; i < ButtonCount; i++)
  {
    pinMode (ButtonPins[i], INPUT_PULLUP);  // Button between Pin and Ground
  }
}

void loop()
{
  checkButtons();
}

void checkButtons()
{
  unsigned long currentTime = millis();

  // Update the buttons
  for (byte i = 0; i < ButtonCount; i++)
  {
    boolean buttonIsPressed = digitalRead(ButtonPins[i]) == LOW;  // Active LOW
    ButtonChangedState[i] = false;

    // Check for button state change and do debounce
    if (buttonIsPressed != ButtonWasPressed[i] &&
        currentTime  -  ButtonStateChangeTime > DebounceTime)
    {
      // Button state has changed
      ButtonStateChangeTime = currentTime;
      ButtonChangedState[i] = true;
      ButtonWasPressed[i] = buttonIsPressed;
    }
  }

  // Act on the the buttons
  for (byte i = 0; i < ButtonCount; i++)
  {
    if (ButtonChangedState[i])
    {
      if (ButtonWasPressed[i])
      {
        // Button was just pressed
      }
      else
      {
        // Button was just released
      }
    }
  }
}

Check out my tutorial on Debouncing Switches in Arduino

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.