Go Down

Topic: Start inpuls with digital input (Read 968 times) previous topic - next topic

zuercher

Hello everybody,

I'm really a newby in programing with C. I just startet to program and play with an LED bar.

Could you help me to write a program code to start the sequence with an impulse to Digital I/O connector 12 (12VDC)

The program is copied from the templates / samples,now I would integrate some topics.

The function later should be: When I press a button the sequence should be running until I release the button (Digital Input)


Thanks for your help!

regards

zuerchern

Attached is the actual programm, simple but it work! :-)

WonderTiger

Hi, welcome to the Arduino forum.

So if I understand it correctly you want to pulse digital pin 12 (do note it is a 5V digital output and not a 12V output). A good starting point will be the blink led example you can find HERE. This tutorial blinks the onboard LED which is connected to digital pin 13, take a look at this tutorial and see if you can implement code to let digital pin 12 pulse like the way you want.

A tip: If your code isn't that big just use the code tags (without the double quotes) ["code"]["/code"] to post your code. Then it will look like this:
Code: [Select]
/*

 */

int timer = 1200;         
void setup() {
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 2; thisPin < 7; thisPin++)  {
    pinMode(thisPin, OUTPUT);     
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 2; thisPin < 7; thisPin++) {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);   
    delay(timer);                 
    // turn the pin off:
    digitalWrite(thisPin, LOW);   
  }

   // loop from the lowest pin to the highest:
  for (int thisPin = 2; thisPin < 7; thisPin++) {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);   
    delay(timer);                 
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }
}
Universal LED cube library available at: https://forum.arduino.cc/index.php?topic=336469.0 :)
16x16x16 RGB LED cube: https://www.youtube.com/watch?v=JIGoYRGJOZk

zuercher

Hi WonderTiger,

Thanks for your fast response. I'm sorry but you have understood me wrong.

I have 6 LED on output (2-7) and I want to start the Loop when I give an input signal to input 12.
When I disconnect the input 12 the Loops should be stopped and all LED should be off.

MY application is later, when I turnover a switch to ON the LED should made a Loop, when I switch back to Off the LED should switches off.

I really tried alot but didn't can't find the correct code.

Thanks a second time for your help!

Regards

WonderTiger

#3
Jul 28, 2015, 10:50 pm Last Edit: Jul 28, 2015, 10:53 pm by WonderTiger
Ok, now I see what you want.

So I made a simple example of what you're trying to achieve, take a good look at it (see the comments) and see if you can figure out yourself how its working and maybe try to implement this to run your own LED sequence. If you got any problems, just post it here. Also take a look at this example provided by arduino.

Code: [Select]
byte ledPin = 13; // LED is internally connected to this pin
byte buttonPin = 8; // Connect a button to this pin

int ledPulseTime = 2000; // Pulse time for LED

bool buttonState = false;
bool oldPinState = false;
bool currentPinState;
bool validButtonState;

unsigned long timePinStateChanged = 0; // Time used to debounce button
unsigned long timeLedSequence = 0; // Time used for the LED sequence
void setup()
{
  pinMode(ledPin, OUTPUT); // LED pin as output
  pinMode(buttonPin, INPUT); // Button pin as input
}

void loop()
{
  validButtonState = readValidButtonState(); // Read debounced button state
  if(validButtonState == true) // Rather use if(validButtonState){...}, this is just for demo purposes
    runLedSequence(); // Run the LED sequence
  else
  {
    digitalWrite(ledPin, LOW); // LED should be off when button isn't pressed...
    timeLedSequence = millis(); // Update led sequence time
  }
}

void runLedSequence()
{
  if(millis() - timeLedSequence < ledPulseTime) // Turn LED on for ... ms
    digitalWrite(ledPin, HIGH);
  else if(millis() - timeLedSequence < (2 * ledPulseTime)) // Turn LED off for ... ms
    digitalWrite(ledPin, LOW);
  else
  {
    timeLedSequence = millis(); // LED has done one blink, update time to the variable that keeps track of the sequence time
    ledPulseTime -= 250; // Speed up the LED pulse time
    if(ledPulseTime <= 0) // Reset pulse time when it is 0 or below
      ledPulseTime = 2000;
  }
}
bool readValidButtonState()
{
  currentPinState = digitalRead(buttonPin); // Read digital pin
  if(currentPinState != oldPinState) 
  {
    oldPinState = currentPinState; // Update old pin state with current read pin state
    timePinStateChanged = millis(); // Update variable that keeps track of the time when the pin changed its state
  }
  if(millis() - timePinStateChanged > 100) // If pin has same state over 100ms we can safely say that it has debounced
    buttonState = currentPinState; // update buttonState
  return buttonState; // Return buttonState value
}
Universal LED cube library available at: https://forum.arduino.cc/index.php?topic=336469.0 :)
16x16x16 RGB LED cube: https://www.youtube.com/watch?v=JIGoYRGJOZk

Go Up