Clavier 5 boutons Agrandir

Clavier 5 boutons

Compatible Arduino

Plus de détails

2 pièces disponibles

5,42 € HT

(6,50 € TTC)

» Ajouter à ma liste

Aucun point de fidélité pour ce produit.


Clavier 5 boutons <p>Ce clavier utilise une seule entrée analogique pour lire l'état des 5 boutons. Livré avec un câble de connexion.</p> <p>Exemple de code Arduino:</p> <div class="container"> <div class="line number1 index0 alt2"><code class="cpp comments">//ADKeyboard Module</code></div> <div class="line number5 index4 alt2"><code class="cpp color1 bold">int</code> <code class="cpp plain">adc_key_val[5] ={600,650, 700,  800, 900 };</code></div> <div class="line number6 index5 alt1"><code class="cpp color1 bold">int</code> <code class="cpp plain">NUM_KEYS = 5;</code></div> <div class="line number7 index6 alt2"><code class="cpp color1 bold">int</code> <code class="cpp plain">adc_key_in;</code></div> <div class="line number8 index7 alt1"><code class="cpp color1 bold">int</code> <code class="cpp plain">key=-1;</code></div> <div class="line number9 index8 alt2"><code class="cpp color1 bold">int</code> <code class="cpp plain">oldkey=-1;</code></div> <div class="line number10 index9 alt1"><code class="cpp keyword bold">void</code> <code class="cpp plain">setup()</code></div> <div class="line number11 index10 alt2"><code class="cpp plain">{</code></div> <div class="line number12 index11 alt1"><code class="cpp spaces">  </code><code class="cpp plain">pinMode(13, OUTPUT);  </code><code class="cpp comments">//we'll use the debug LED to output a heartbeat</code></div> <div class="line number13 index12 alt2"><code class="cpp spaces">  </code><code class="cpp plain">Serial.begin(9600); </code><code class="cpp comments">// 9600 bps</code></div> <div class="line number14 index13 alt1"><code class="cpp plain">}</code></div> <div class="line number15 index14 alt2"> </div> <div class="line number16 index15 alt1"><code class="cpp keyword bold">void</code> <code class="cpp plain">loop()</code></div> <div class="line number17 index16 alt2"><code class="cpp plain">{</code></div> <div class="line number18 index17 alt1"><code class="cpp spaces">  </code><code class="cpp plain">adc_key_in = analogRead(0);    </code><code class="cpp comments">// read the value from the sensor </code></div> <div class="line number19 index18 alt2"><code class="cpp spaces">  </code><code class="cpp plain">digitalWrite(13,LOW); </code></div> <div class="line number20 index19 alt1"><code class="cpp spaces">  </code><code class="cpp plain">key = get_key(adc_key_in);  </code><code class="cpp comments">// convert into key press</code></div> <div class="line number21 index20 alt2"><code class="cpp spaces"> </code> </div> <div class="line number22 index21 alt1"><code class="cpp spaces">  </code><code class="cpp keyword bold">if</code> <code class="cpp plain">(key != oldkey)   </code><code class="cpp comments">// if keypress is detected</code></div> <div class="line number23 index22 alt2"><code class="cpp spaces">   </code><code class="cpp plain">{</code></div> <div class="line number24 index23 alt1"><code class="cpp spaces">    </code><code class="cpp plain">delay(50);  </code><code class="cpp comments">// wait for debounce time</code></div> <div class="line number25 index24 alt2"><code class="cpp spaces">    </code><code class="cpp plain">adc_key_in = analogRead(0);    </code><code class="cpp comments">// read the value from the sensor </code></div> <div class="line number26 index25 alt1"><code class="cpp spaces">    </code><code class="cpp plain">key = get_key(adc_key_in);    </code><code class="cpp comments">// convert into key press</code></div> <div class="line number27 index26 alt2"><code class="cpp spaces">    </code><code class="cpp keyword bold">if</code> <code class="cpp plain">(key != oldkey)    </code></div> <div class="line number28 index27 alt1"><code class="cpp spaces">    </code><code class="cpp plain">{   </code></div> <div class="line number29 index28 alt2"><code class="cpp spaces">      </code><code class="cpp plain">oldkey = key;</code></div> <div class="line number30 index29 alt1"><code class="cpp spaces">      </code><code class="cpp keyword bold">if</code> <code class="cpp plain">(key &gt;=0){</code></div> <div class="line number31 index30 alt2"><code class="cpp spaces">        </code><code class="cpp plain">digitalWrite(13,HIGH);</code></div> <div class="line number32 index31 alt1"><code class="cpp spaces">        </code><code class="cpp keyword bold">switch</code><code class="cpp plain">(key)</code></div> <div class="line number33 index32 alt2"><code class="cpp spaces">        </code><code class="cpp plain">{</code></div> <div class="line number34 index33 alt1"><code class="cpp spaces">           </code><code class="cpp keyword bold">case</code> <code class="cpp plain">0:Serial.println(</code><code class="cpp string">"S1 OK"</code><code class="cpp plain">);   </code></div> <div class="line number35 index34 alt2"><code class="cpp spaces">                  </code><code class="cpp keyword bold">break</code><code class="cpp plain">;</code></div> <div class="line number36 index35 alt1"><code class="cpp spaces">           </code><code class="cpp keyword bold">case</code> <code class="cpp plain">1:Serial.println(</code><code class="cpp string">"S2 OK"</code><code class="cpp plain">);   </code></div> <div class="line number37 index36 alt2"><code class="cpp spaces">                  </code><code class="cpp keyword bold">break</code><code class="cpp plain">;</code></div> <div class="line number38 index37 alt1"><code class="cpp spaces">           </code><code class="cpp keyword bold">case</code> <code class="cpp plain">2:Serial.println(</code><code class="cpp string">"S3 OK"</code><code class="cpp plain">);   </code></div> <div class="line number39 index38 alt2"><code class="cpp spaces">                  </code><code class="cpp keyword bold">break</code><code class="cpp plain">;</code></div> <div class="line number40 index39 alt1"><code class="cpp spaces">           </code><code class="cpp keyword bold">case</code> <code class="cpp plain">3:Serial.println(</code><code class="cpp string">"S4 OK"</code><code class="cpp plain">);   </code></div> <div class="line number41 index40 alt2"><code class="cpp spaces">                  </code><code class="cpp keyword bold">break</code><code class="cpp plain">;      </code></div> <div class="line number42 index41 alt1"><code class="cpp spaces">           </code><code class="cpp keyword bold">case</code> <code class="cpp plain">4:Serial.println(</code><code class="cpp string">"S5 OK"</code><code class="cpp plain">);   </code></div> <div class="line number43 index42 alt2"><code class="cpp spaces">                  </code><code class="cpp keyword bold">break</code><code class="cpp plain">;  </code></div> <div class="line number44 index43 alt1"><code class="cpp spaces">        </code><code class="cpp plain">}                </code></div> <div class="line number45 index44 alt2"><code class="cpp spaces">      </code><code class="cpp plain">}</code></div> <div class="line number46 index45 alt1"><code class="cpp spaces">    </code><code class="cpp plain">}</code></div> <div class="line number47 index46 alt2"><code class="cpp spaces">  </code><code class="cpp plain">}</code></div> <div class="line number48 index47 alt1"><code class="cpp spaces"> </code><code class="cpp plain">delay(100);</code></div> <div class="line number49 index48 alt2"><code class="cpp plain">}</code></div> <div class="line number50 index49 alt1"><code class="cpp comments">// Convert ADC value to key number</code></div> <div class="line number51 index50 alt2"><code class="cpp color1 bold">int</code> <code class="cpp plain">get_key(unsigned </code><code class="cpp color1 bold">int</code> <code class="cpp plain">input)</code></div> <div class="line number52 index51 alt1"><code class="cpp plain">{</code></div> <div class="line number53 index52 alt2"><code class="cpp spaces">    </code><code class="cpp color1 bold">int</code> <code class="cpp plain">k;</code></div> <div class="line number54 index53 alt1"><code class="cpp spaces">    </code><code class="cpp keyword bold">for</code> <code class="cpp plain">(k = 0; k &lt; NUM_KEYS; k++)</code></div> <div class="line number55 index54 alt2"><code class="cpp spaces">    </code><code class="cpp plain">{</code></div> <div class="line number56 index55 alt1"><code class="cpp spaces">      </code><code class="cpp keyword bold">if</code> <code class="cpp plain">(input &lt; adc_key_val[k])</code></div> <div class="line number57 index56 alt2"><code class="cpp spaces">     </code><code class="cpp plain">{</code></div> <div class="line number58 index57 alt1"><code class="cpp spaces">            </code><code class="cpp keyword bold">return</code> <code class="cpp plain">k;</code></div> <div class="line number59 index58 alt2"><code class="cpp spaces">        </code><code class="cpp plain">}</code></div> <div class="line number60 index59 alt1"><code class="cpp spaces">   </code><code class="cpp plain">}</code></div> <div class="line number61 index60 alt2"><code class="cpp spaces">       </code><code class="cpp keyword bold">if</code> <code class="cpp plain">(k &gt;= NUM_KEYS)k = -1;  </code><code class="cpp comments">// No valid key pressed</code></div> <div class="line number62 index61 alt1"><code class="cpp spaces">       </code><code class="cpp keyword bold">return</code> <code class="cpp plain">k;</code></div> </div> <div class="line number63 index62 alt2"><code class="cpp plain">}</code></div> <div class="line number63 index62 alt2"> </div>
5,42 €

Ce clavier utilise une seule entrée analogique pour lire l'état des 5 boutons. Livré avec un câble de connexion.

Exemple de code Arduino:

//ADKeyboard Module
int adc_key_val[5] ={600,650, 700,  800, 900 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
void setup()
{
  pinMode(13, OUTPUT);  //we'll use the debug LED to output a heartbeat
  Serial.begin(9600); // 9600 bps
}
 
void loop()
{
  adc_key_in = analogRead(0);    // read the value from the sensor
  digitalWrite(13,LOW);
  key = get_key(adc_key_in);  // convert into key press
  
  if (key != oldkey)   // if keypress is detected
   {
    delay(50);  // wait for debounce time
    adc_key_in = analogRead(0);    // read the value from the sensor
    key = get_key(adc_key_in);    // convert into key press
    if (key != oldkey)   
    {  
      oldkey = key;
      if (key >=0){
        digitalWrite(13,HIGH);
        switch(key)
        {
           case 0:Serial.println("S1 OK");  
                  break;
           case 1:Serial.println("S2 OK");  
                  break;
           case 2:Serial.println("S3 OK");  
                  break;
           case 3:Serial.println("S4 OK");  
                  break;     
           case 4:Serial.println("S5 OK");  
                  break
        }               
      }
    }
  }
 delay(100);
}
// Convert ADC value to key number
int get_key(unsigned int input)
{
    int k;
    for (k = 0; k < NUM_KEYS; k++)
    {
      if (input < adc_key_val[k])
     {
            return k;
        }
   }
       if (k >= NUM_KEYS)k = -1;  // No valid key pressed
       return k;
}
 

Newsletter

PayPal