顯示具有 arduino example 標籤的文章。 顯示所有文章
顯示具有 arduino example 標籤的文章。 顯示所有文章

2017年9月5日 星期二

Lesson 3. Digital Inputs


In this lesson, you will learn to use push buttons with digital inputs to turn an LED on and off.
Pressing the button will turn the LED on; pressing the other button will turn the LED off. 







Component Required:
(1) x Sintron Uno R3
(1) x 830 Tie-points Breadboard
(1) x 5mm red LED
(1) x 220 ohm resistor
(2) x push switches
(7) x M-M wires (Male to Male jumper wires) 




Component Introduction

PUSH SWITCHES:

Switches are really simple components. When you press a button or flip a lever, they connect two contacts together so that electricity can flow throughthem.
The little tactile switches that are used in this lesson have four connections, which can be a little confusing. 



Actually, there are only really two electrical connections. Inside the switch package, pins B and C are connected together, as are A and D. 



Schematic 



Although the bodies of the switches are square, the pins protrude from opposite sides of the switch. This means that the pins will only be far enough apart when they are placed correctly on thebreadboard.
Remember that the LED has to have the shorter negative lead to the right.


code : ( please copy and upload sketch to arduino board )
Everything between /* and */ or after // is a block comment; it explains what the sketch is for. 



//by Sintron 
//2017.03.20


int ledPin = 5;
int buttonApin = 9;
int buttonBpin = 8;

byte leds = 0;

void setup() 
{
  pinMode(ledPin, OUTPUT);
  pinMode(buttonApin, INPUT_PULLUP);  
  pinMode(buttonBpin, INPUT_PULLUP);  
}

void loop() 
{
  if (digitalRead(buttonApin) == LOW)
  {
    digitalWrite(ledPin, HIGH);
  }
  if (digitalRead(buttonBpin) == LOW)
  {
    digitalWrite(ledPin, LOW);
  }
} 



Load the sketch onto your UNO board. Pressing the left button will turn the LED on while pressing the right button will turn it off.
The first part of the sketch defines three variables for the three pins that are to be used. The 'ledPin' is the output pin and 'buttonApin' will refer to the switch nearer the top of the breadboard and 'buttonBpin' to the other switch.

The 'setup' function defines the ledPin as being an OUTPUT as normal, but now we have the two inputs to deal with. In this case, we use the set the pinMode to be 'INPUT_PULLUP' like this:
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
The pin mode of INPUT_PULLUP means that the pin is to be used as an input, but that if nothing else is connected to the input, it should be 'pulled up' to HIGH. In other words, the default value for the input is HIGH, unless it is pulled LOW by the action of pressing the button.
This is why the switches are connected to GND. When a switch is pressed, it connects the input pin to GND, so that it is no longer HIGH.
Since the input is normally HIGH and only goes LOW when the button is pressed, the logic is a little upside down. We will handle this in the 'loop'function.

void loop() {
if (digitalRead(buttonApin) == LOW)
    {
digitalWrite(ledPin, HIGH); 
    }
if (digitalRead(buttonBpin) == LOW)

    {
digitalWrite(ledPin, LOW);

    } 
                   }
In the 'loop' function there are two 'if' statements. One for each button. Each does an 'digitalRead' on the appropriate input.
Remember that if the button is pressed, the corresponding input will be LOW, if button A is low, then a 'digitalWrite' on the ledPin turns it on.

Similarly, if button B is pressed, a LOW is written to the ledPin.

Example picture 





Share:

2017年8月30日 星期三

Sintron arduino working with CH-926 coin acceptor to make money

with this arduino example ,you will learn how to read signals from CH-926 coin acceptor ,and then use LED to response .so alternatively .you can use coin acceptor to control any other device to charge fee for your service .


Wirings :


1. supply 12V power supply for both “coin acceptor” and “arduino uno”.


2. coin acceptor output pin ( white wire ) -- arduino pin D2 .
3. LED control pin :  pin D8 ( add 220 ohm resistance between D8 & LED )

code :


int Ledpin = 8;
int Coinpin = 2 ;


void setup( )
{
  pinMode(Ledpin,OUTPUT);
  pinMode( Coinpin,INPUT);
}
 void loop ( )

 {
   if (digitalRead( Coinpin) == HIGH)
   {
     digitalWrite(Ledpin,HIGH);
 
   }
   else if  (digitalRead( Coinpin) == LOW)
   {
     digitalWrite(Ledpin,LOW);
   
   }
 }






code 2: updated on 2019 june, using interrupt method:

// by Sintron ccfeng june 2019
const byte builtinLED = 13;
const byte ledPin = 8;
const byte coin_input = 2;
volatile byte state = LOW;
boolean coin_flag = false ;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(coin_input, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(coin_input), CH926, FALLING);
}

void loop() {

  if (coin_flag == true )
  {
    delay (15);
    state = !state;
    digitalWrite(ledPin, state);    // you can also use this for a relay
    digitalWrite(builtinLED, state);   // check if the bulit in LED changes

    coin_flag = false ;     // remove the flag
    Serial.println("got a coin in");  // you can also check in serial monitor

  }

 
}

void CH926() {
  coin_flag = true;

}











Material list :

1. Sintron arduino-compatible board
2. Sintron CH-926 coin acceptor
3. LED
4. 220 ohm resistor *1
5. 12V power supply *1


Share:

Sintron

Search This Blog

技術提供:Blogger.

Blog Archive

Arduino Super Mario tone with Buzzer

Labels