• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Jak połączyć dwa kody
#1
Witam
Jestem tu nowy i chciałbym podłączyć do Arduino nano klawiaturę membranową oraz pasek led po przez mosfet.
Jedyny problem jaki napotkałem to połączenie dwóch gotowych kodów.
Czy jest mi ktoś w stanie z tym pomóc ?

Pierwszy kod do klawiatury:

Kod:
#define MY_DEBUG
#define MY_RADIO_NRF24

#include <SPI.h>
#include <MySensors.h>
#include <Keypad.h>

#define MY_NODE_ID 123 // or set to AUTO if you want gw to assign a NODE_ID for you.
#define SN "Scene Controller"
#define SV "3.0"
#define KEYPAD_CHILD_ID 95

MyMessage scene(KEYPAD_CHILD_ID, V_SCENE_ON);
MyMessage scene2(KEYPAD_CHILD_ID, V_SCENE_OFF);

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
 {'1','2','3'},
 {'4','5','6'},
 {'7','8','9'},
 {'*','0','#'}
};
byte rowPins[ROWS] = {8, 7, 6, 5};  //Piny, do których podłączamy wyprowadzenia od rzędów
byte colPins[COLS] = {4, 3, 2}; //Piny, do których kolumn wyprowadzenia od rzędów

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte lastState;

void setup() {
 
 sendSketchInfo(SN, SV);
 present(KEYPAD_CHILD_ID, S_SCENE_CONTROLLER);
 keypad.addEventListener(keypadEvent);
}

void loop() {
 char key = keypad.getKey();
}

void keypadEvent(KeypadEvent key) {
 switch (keypad.getState()) {

   case PRESSED:
     lastState = 1;
     break;

   case HOLD:
     lastState = 2;
     break;

   case RELEASED:
     int keyInt = key - '0';
     if (lastState == 2) {
       send(scene2.set(keyInt));
     } else {
     send(scene.set(keyInt));
     }
     //break;
}
}
Drugi kod do ściemniacza Led

Kod:
// Enable debug prints to serial monitor
#define MY_DEBUG

// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69

#include <MySensors.h>

#define SN "DimmableLED"
#define SV "1.1"

#define LED_PIN 3      // Arduino pin attached to MOSFET Gate pin
#define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)

static int16_t currentLevel = 0;  // Current dim level...
MyMessage dimmerMsg(0, V_DIMMER);
MyMessage lightMsg(0, V_LIGHT);


/***
* Dimmable LED initialization method
*/
void setup()
{
   // Pull the gateway's current dim level - restore light level upon sendor node power-up
   request( 0, V_DIMMER );
}

void presentation()
{
   // Register the LED Dimmable Light with the gateway
   present( 0, S_DIMMER );

   sendSketchInfo(SN, SV);
}

/***
*  Dimmable LED main processing loop
*/
void loop()
{
}



void receive(const MyMessage &message)
{
   if (message.type == V_LIGHT || message.type == V_DIMMER) {

       //  Retrieve the power or dim level from the incoming request message
       int requestedLevel = atoi( message.data );

       // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
       requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 );

       // Clip incoming level to valid range of 0 to 100
       requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
       requestedLevel = requestedLevel < 0   ? 0   : requestedLevel;

       Serial.print( "Changing level to " );
       Serial.print( requestedLevel );
       Serial.print( ", from " );
       Serial.println( currentLevel );

       fadeToLevel( requestedLevel );

       // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
       send(lightMsg.set(currentLevel > 0));

       // hek comment: Is this really nessesary?
       send( dimmerMsg.set(currentLevel) );


   }
}

/***
*  This method provides a graceful fade up/down effect
*/
void fadeToLevel( int toLevel )
{

   int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;

   while ( currentLevel != toLevel ) {
       currentLevel += delta;
       analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
       delay( FADE_DELAY );
   }
}
 
Odpowiedź
#2
Również chciałabym zacząć i podlączyć się. Bardzo fajne i przydatne info. Dzięki
 
Odpowiedź
#3
Proponuje powrócić do migania diodą i zacząć od najprostszych programów .
Mamy diodę na 12 pinie do tego dajemy włącznik nawet dołożymy wyświetlacz . Pewnie robiliście programy z każdym z tych elementów. Teraz nauka jak działa i co to warunek (if)
Nie muszę opisywać że na początku pin12 ustawić jak wyjście plus przycisk i wyświetlacz i biblioteka do niego.
Sam program główny po starcie dioda nie świeci

zaczynamy od warunku
if
jeżeli dioda nie świeci a przycisk jest wciśnięty
to zaświeć diodę wyczyść ekran i wyświetl dioda świeci i odczekaj 2s.

i drugi warunek
if
jeżeli dioda świeci a przycisk jest wciśnięty
to zgaś diodę wyczyść ekran i wyświetl dioda zgaszona i odczekaj 2s

Program składa się z 2 if jeżeli pierwszy zostanie spełniony to zaświeci diodę .
Dioda będzie świecić tak długo aż nie naciśniemy przycisku.
poczytać jak to się używa jak wstawiać nawiasy .
 
Odpowiedź
  


Skocz do:


Przeglądający: 1 gości