collapse

* Posts Recentes

Emulador NES em ESP32 por dropes
[13 de Março de 2024, 21:19]


Escolher Osciloscópio por jm_araujo
[06 de Fevereiro de 2024, 23:07]


TP4056 - Dúvida por dropes
[31 de Janeiro de 2024, 14:13]


Leitura de dados por Porta Serie por jm_araujo
[22 de Janeiro de 2024, 14:00]


Distancia Cabo por jm_araujo
[08 de Janeiro de 2024, 16:30]


Meu novo robô por josecarlos
[06 de Janeiro de 2024, 16:46]


Laser Engraver - Alguém tem? por almamater
[16 de Dezembro de 2023, 14:23]


Focos LED SMD por almamater
[16 de Dezembro de 2023, 14:12]


I Belive por dropes
[15 de Dezembro de 2023, 13:59]


Carga de corrente eletrónica ZPB30A1 60W por jm_araujo
[11 de Dezembro de 2023, 13:27]

Autor Tópico: ligação de varios leds  (Lida 4315 vezes)

0 Membros e 1 Visitante estão a ver este tópico.

Offline afonso henriques

  • Mini Robot
  • *
  • Mensagens: 78
ligação de varios leds
« em: 15 de Maio de 2012, 00:02 »
boas a todos
estou com um projeto simples
como temos aqui em guimarães a capital da cultura, estou a fazer um coração a leds
mas estou um pouco confuso numa questão
tenho cerca de  50 leds no coração mas o arduino só tem 14 saidas
eu queria controlar os leds independentemente uns dos outros
alguem sabe com posso controlar o sistema com as 14 saidas?

Offline Hugu

  • Mini Robot
  • *
  • Mensagens: 5.602
  • Keyboard not found. Press any key to continue.
    • [url=www.g7electronica.net]G7 Electrónica.net[/url]

Offline iyahdub

  • Mini Robot
  • *
  • Mensagens: 280
Re: ligação de varios leds
« Responder #2 em: 15 de Maio de 2012, 19:51 »
O meu amigo Nick fez este codigo para shift registers... 4, mas podes expandir um pouco( apesar de conseguires mais de 64 leds tal como o LOL shield, nao quer dizer que seja correcto, para alem do facto que teria muito noise digital, ligado a uma PSU pequena tal como as portateis de parede)
Código: [Seleccione]
#include <SPI.h>

const byte LATCH = 10;

const byte numberOfChips = 4;

byte LEDdata [numberOfChips];  // initial pattern

void refreshLEDs ()
  {
  digitalWrite (LATCH, LOW);
  for (byte i = 0; i < numberOfChips; i++)
    SPI.transfer (LEDdata [i]);
  digitalWrite (LATCH, HIGH);
  } // end of refreshLEDs
 

void setup ()
{
  SPI.begin ();
}  // end of setup


void showPattern (const unsigned int p1, const unsigned int p2)
  {
  LEDdata [0] = highByte (p1);
  LEDdata [1] = lowByte (p1);
  LEDdata [2] = highByte (p2);
  LEDdata [3] = lowByte (p2);
  refreshLEDs ();
  delay (30);
  } // end of showPattern
 
void loop ()
{
unsigned int pattern;
 
  pattern = 1;
  for (int i = 0; i < 16; i++)
    {
    showPattern (pattern, pattern);
    pattern <<= 1;
    }

  pattern = 0x8000;
  for (int i = 0; i < 16; i++)
    {
    showPattern (pattern, pattern);
    pattern >>= 1;
    }   

  pattern = 1;
  for (int i = 0; i < 16; i++)
    {
    showPattern (~pattern, ~pattern);
    pattern <<= 1;
    }

  pattern = 0x8000;
  for (int i = 0; i < 16; i++)
    {
    showPattern (~pattern, ~pattern);
    pattern >>= 1;
    }   
   
}  // end of loop

Tens aqui um muito mais detalhado que permite enviar comandos para controlar padroes, etc...

Código: [Seleccione]
// Example of setting, clearing, rotating LEDs connected to a 74HC595 shift register
// Author: Nick Gammon
// Date: 3rd May 2012

/*
 
 For more information see:
 
   http://www.gammon.com.au/forum/?id=11518
 
 Baud rate: 115200
 
 Commands you can type in the serial monitor (can be upper or lower case):
 
 nnnX   where nnn is a number (1 or more digits) and X is a command
 
 Spaces are ignored. Carriage-return or linefeed terminates the current command and resets ready for a new one.
 
 nnn C  --> Clear LED nnn. If nnn is omitted, or zero, all LEDs are cleared.
 nnn S  --> Set LED nnn. If nnn is omitted, or zero, all LEDs are set.
 nnn I  --> Invert LED nnn. If nnn is omitted, or zero, all LEDs are inverted.  (if on, turned off, or vice-versa)
 
 nnn R  --> Rotate all LEDs to the right nnn positions. If nnn is omitted, default to rotate one position right.
 nnn L  --> Rotate all LEDs to the left nnn positions. If nnn is omitted, default to rotate one position left.
 
 nnn D  --> Set delay amount. The delay (default 100 mS) is done after each command. Can be zero for doing things quickly.
 nnn P  --> Pause for nnn milliseconds. If nnn is zero, pause for the delay amount.
   
 Examples:
 
 5s  --> turn LED 5 on
 s   --> turn all LEDs on
 7s8s9s  --> turn on LEDs 7, 8, 9
 30c --> clear LED number 30
 c   --> clear (turn off) all LEDs
 44i --> invert LED number 44 (turn on if off, or off if on)
 i   --> invert all LEDs
 R   --> rotate pattern 1 to the right (if LED 1 was on, LED 2 will be on now and so on)
 10r --> rotate pattern 10 positions to the right
 L   --> rotate pattern 1 to the left (if LED 2 was on, LED 1 will be on now and so on)
 4L  --> rorate pattern 4 positions to the left
 
 1000D --> do a delay of 1000 mS (1 second) after every command
 P   --> pause the current delay amount
 50P --> pause for 50 mS
 
 
 Change "numberOfChips" below to reflect how many 74HC595 shift registers you have connected together.

 Wiring:
         D10 (SS) to ST_CP (pin 12) of all 74HC595 chips
         D11 (MOSI) to DS (pin 14) of the first 74HC595 chip
         D13 (SCK) to SH_CP (pin 11) of all 74HC595 chips
         
If you want to use a different SS (LATCH) pin on the processor change the LATCH constant below.       
 */
 
 

#include <SPI.h>

const byte LATCH = 10;

const byte numberOfChips = 4;
const byte maxLEDs = numberOfChips * 8;

byte LEDdata [numberOfChips] = { 0 };  // initial pattern

unsigned long delayAmount = 100;

void refreshLEDs ()
  {
  digitalWrite (LATCH, LOW);
  for (int i = numberOfChips - 1; i >= 0; i--)
    SPI.transfer (LEDdata [i]);
  digitalWrite (LATCH, HIGH);
  } // end of refreshLEDs
 
void setup ()
  {
  SPI.begin ();   
  Serial.begin (115200);
  Serial.println ("Starting ...");
  refreshLEDs ();  // clear all LEDs
  } // end of setup
 
// turn an LED number into the position in the array, and a bit mask
boolean getChipAndBit (unsigned int led, int & chip, byte & mask)
  {
  if (led > maxLEDs)
      {
      Serial.print ("LED ");
      Serial.print (led);
      Serial.println (" too high.");
      return true;  // error
      } // end of too high
   
   led--;  // make zero relative
   
   // divide by 8 to work out which chip
   chip = led / 8;  // which chip
   
   // remainder is bit number
   mask = 1 << (led % 8);
   
  return false;  // no error
  }  // end of getChipAndBit

// clear LED n (or all if zero)
void clearLED (const long n)
  {
  // zero means all
  if (n == 0)
    {
    for (int i = 0; i < numberOfChips; i++)
      LEDdata [i] = 0;
    return;
    }  // end of if zero
   
  int chip;
  byte mask;
  if (getChipAndBit (n, chip, mask))
    return;  // bad number
   
  LEDdata [chip] &= ~ mask;
  }  // end of clearLED

// set LED n (or all if zero)
void setLED (const long n)
  {
  // zero means all
  if (n == 0)
    {
    for (int i = 0; i < numberOfChips; i++)
      LEDdata [i] = 0xFF;
    return;
    }  // end of if zero
   
  int chip;
  byte mask;
  if (getChipAndBit (n, chip, mask))
    return;  // bad number
   
  LEDdata [chip] |= mask;
  }  // end of setLED

// invert LED n (or all if zero)
void invertLED (const long n)
  {
  // zero means all
  if (n == 0)
    {
    for (int i = 0; i < numberOfChips; i++)
      LEDdata [i] ^= 0xFF;
    return;
    } // end of if zero
   
  int chip;
  byte mask;
  if (getChipAndBit (n, chip, mask))
    return;  // bad number
   
  LEDdata [chip] ^= mask;
  }  // end of invertLED
 
// set the current delay amount to n
void setDelay (const long n)
  {
  delayAmount = n; 
  }  // end of setDelay
 
// rotate pattern right (up) n positions
void rotateRight (unsigned int n)
  {
  if (n >= maxLEDs)
    {
    Serial.print ("Rotation right amount ");
    Serial.print (n);
    Serial.println (" too high.");
    return;  // error
    }  // end of if too high an amount

  // assume rotate at least once
  if (n == 0)
    n = 1;
   
  // first do whole bytes
  while (n >= 8)
    {
    byte temp = LEDdata [numberOfChips - 1];
    memmove (&LEDdata [1], &LEDdata [0], numberOfChips - 1);
    LEDdata [0] = temp;
    n -= 8;
    }  // end of more than a byte 
   
   while (n-- > 0)
    {
    // each time we carry forwards the high order bit
    // the very first time we take that bit from the highest byte
    byte oldCarry = (LEDdata [numberOfChips - 1] & 0x80) ? 1 : 0;
   
    for (int thisChip = 0; thisChip < numberOfChips; thisChip++)
      {
      byte newCarry = (LEDdata [thisChip] & 0x80) ? 1 : 0;
      LEDdata [thisChip] <<= 1; 
      LEDdata [thisChip] |= oldCarry;
      oldCarry = newCarry;
      } // end of for
    }  // end of doing each bit
 
  }  // end of rotateRight

// rotate pattern left (down) n positions
void rotateLeft (unsigned int n)
  {
  if (n >= maxLEDs)
    {
    Serial.print ("Rotation left amount ");
    Serial.print (n);
    Serial.println (" too high.");
    return;  // error
    }  // end of if too high an amount

  // assume rotate at least once
  if (n == 0)
    n = 1;
   
  // first do whole bytes
  while (n >= 8)
    {
    byte temp = LEDdata [0];
    memmove (&LEDdata [0], &LEDdata [1], numberOfChips - 1);
    LEDdata [numberOfChips - 1] = temp;
    n -= 8;
    }  // end of more than a byte 
   
   while (n-- > 0)
    {
    // each time we carry forwards the low order bit
    // the very first time we take that bit from the lowest byte
    byte oldCarry = (LEDdata [0] & 1) ? 0x80 : 0;
   
    for (int thisChip = numberOfChips - 1; thisChip >= 0; thisChip--)
      {
      byte newCarry = (LEDdata [thisChip] & 1) ? 0x80 : 0;
      LEDdata [thisChip] >>= 1; 
      LEDdata [thisChip] |= oldCarry;
      oldCarry = newCarry;
      } // end of for
    }  // end of doing each bit
 
  }  // end of rotateLeft
 
// pause for supplied mS, or if zero, the current delay amount
void pause (const long mS)
  {
  if (mS == 0)
    {
    if (delayAmount)
      delay (delayAmount);
    return;     
    }

  delay (mS);     
  }  // end of pause
 
void processCommand (const byte command, const long n)
  {
  switch (command)
    {
    case 'C': clearLED    (n);   break;   // Clear LED(s)
    case 'S': setLED      (n);   break;   // Set LED(s)
    case 'I': invertLED   (n);   break;   // Invert LED(s)
    case 'D': setDelay    (n);   break;   // Delay amount (mS)
    case 'P': pause       (n);   break;   // Pause for some mS
    case 'R': rotateRight (n);   break;   // Rotate right n positions
    case 'L': rotateLeft  (n);   break;   // Rotate left n positions
   
    default:
      Serial.print ("Unknown command: ");   
      Serial.println (command);
      return;
   
    }  // end of switch on command
   
  // push the new pattern out to the chips
  refreshLEDs ();
 
  // do optional delay between commands
  if (delayAmount)
    delay (delayAmount);
 
  }  // end of processCommand
 
void processInput ()
  {
  static long receivedNumber = 0;
 
  byte c = Serial.read ();
 
  switch (c)
    {
     
    // accumulate digits
    case '0' ... '9':
      receivedNumber *= 10;
      receivedNumber += c - '0';
      break;

    default:
      Serial.print ("Unexpected input: ");   
      Serial.println (c);
     
      // fall through to start new number
     
    // carriage-return or line-feed starts new number
    case '\r':
    case '\n':
      receivedNumber = 0;
      break;
     
    case 'A' ... 'Z':
    case 'a' ... 'z':
      processCommand (toupper (c), receivedNumber);
      receivedNumber = 0;
      break;
     
    // ignore spaces
    case ' ':
      break;
    } // end of switch on received character
  }  // end of processInput
 
void loop ()
  {
 
  if (Serial.available ())
    processInput ();
   
  // do other stuff here
  } // end of loop

Tens aqui o Website dele com esquematicas, explicacoes, etc

http://www.gammon.com.au/forum/?id=11518
ps-Desculpen a falta de pontuacao, mas vivo no estrangeiro e os teclados sao xenofobos !!

Offline beirao

  • Mini Robot
  • *
  • Mensagens: 1.531
Re: ligação de varios leds
« Responder #3 em: 15 de Maio de 2012, 20:22 »
Usando shift registers de 8output, usas 6 = 48 leds, e metes 2 leds controlados por pin´s do arduino.

Pressupondo que cada shift precisa de 3bits para selecção de saída e um clock (podes precisar de mais um pin para reset ou assim) mas no minimo penso que fazes isso com 6+2+3+1 = 12pin

mas só fazendo um esquema e ver se nao ta a faltar nada
"O único lugar onde o sucesso vem antes do trabalho, é no dicionário" - Albert Einstein

Offline afonso henriques

  • Mini Robot
  • *
  • Mensagens: 78
Re: ligação de varios leds
« Responder #4 em: 16 de Maio de 2012, 00:02 »
olá
obrigado a todos pelas dicas
chegou me agora o resto do material (obrigado hugo)
no fim de semana tenho mais uma etapa soldar o resto dos leds
e debruçar-me um pouco sobre esses controladores os 74hc

Offline afonso henriques

  • Mini Robot
  • *
  • Mensagens: 78
Re: ligação de varios leds
« Responder #5 em: 25 de Maio de 2012, 00:42 »
boas a todos
já consegui fazer a ligação de um 74hc595
e consegui ligar os 8 leds mas queria por com outra sequencias
usei este:// Demo sketch to turn sequence up and down bits in 4 shift registers
// Author: Nick Gammon
// Date: 2 May 2012

#include <SPI.h>

const byte LATCH = 10;

const byte numberOfChips = 4;

byte LEDdata [numberOfChips];  // initial pattern

void refreshLEDs ()
  {
  digitalWrite (LATCH, LOW);
  for (byte i = 0; i < numberOfChips; i++)
    SPI.transfer (LEDdata );
  digitalWrite (LATCH, HIGH);
  } // end of refreshLEDs
 

void setup ()
{
  SPI.begin ();
}  // end of setup


void showPattern (const unsigned int p1, const unsigned int p2)
  {
  LEDdata
  • = highByte (p1);

  LEDdata [1] = lowByte (p1);
  LEDdata [2] = highByte (p2);
  LEDdata [3] = lowByte (p2);
  refreshLEDs ();
  delay (30);
  } // end of showPattern
 
void loop ()
{
unsigned int pattern;
 
  pattern = 1;
  for (int i = 0; i < 16; i++)
    {
    showPattern (pattern, pattern);
    pattern <<= 1;
    }

  pattern = 0x8000;
  for (int i = 0; i < 16; i++)
    {
    showPattern (pattern, pattern);
    pattern >>= 1;
    }   

  pattern = 1;
  for (int i = 0; i < 16; i++)
    {
    showPattern (~pattern, ~pattern);
    pattern <<= 1;
    }

  pattern = 0x8000;
  for (int i = 0; i < 16; i++)
    {
    showPattern (~pattern, ~pattern);
    pattern >>= 1;
    }   
   
}  // end of loop
será que está correto