美文网首页
【树莓派+arduino实验记录1】双色LED

【树莓派+arduino实验记录1】双色LED

作者: Geekero | 来源:发表于2020-06-17 08:48 被阅读0次

Arduino UNO R3

接线:

代码:

/**************************************
name: Dual-color Led
function: let the dual-color LED changes for red to green alternately, 
as well as flashing a mixed color during the alternation.

connection:
Dual-color LED     Arduino Uno R3
R                  11
GND                GND
G                  10
******************************************/

int redPin = 11;    //select the pin for the read LED
int greenPin = 10;    //select the pin for the blueLED
int val = 0;

void setup()
{  
 pinMode(redPin, OUTPUT);  //set redPin ad OUTPUT
 pinMode(greenPin, OUTPUT); //set greenPin as OUTPUT
 Serial.begin(9600);
}

/********************************************/
void loop()
{
     for(val=255; val>0; val--) 
      {   
      analogWrite(redPin, val); //red value decrease
      analogWrite(greenPin, 255-val); //green value increase
      Serial.println(val, DEC); //print the val on in the serial monitor
      delay(30); //delay 30ms;
      }
  
      for(val=0; val<255; val++)
      {
      analogWrite(redPin, val); //red value increase
      analogWrite(greenPin, 255-val); //green value decrease
      Serial.println(val, DEC); 
      delay(30); //delay 30 ms
     }   
}

树莓派4B

接线:

代码:

C语言版:
#include <wiringPi.h>
#include <softPwm.h>
#include <stdio.h>
#define uchar unsigned char
#define LedPinRed   0
#define LedPinGreen 1
void ledInit(void)
{
        softPwmCreate(LedPinRed, 0, 100);
        softPwmCreate(LedPinGreen, 0, 100);
}

void ledColorSet(uchar r_val, uchar g_val)
{
        softPwmWrite(LedPinRed, r_val);
        softPwmWrite(LedPinGreen, g_val);
}

int main(void)
{
        int i;
        if(wiringPiSetup() == -1){ //when initialize wiring failed, print message to screen
                printf("setup wiringPi failed !");
                return 1;
        }
        printf("linker LedPin: GPIO %d(wiringPi pin)\n", LedPinRed); //when initialize wiring successfully, print message to screen

        ledInit();
        while(1){
                ledColorSet(0xff,0x00); //red
                delay(500);
                ledColorSet(0x00,0xff); //green
                delay(500);
                ledColorSet(0xff,0x45);
                delay(500);
                ledColorSet(0xff,0xff);
                delay(500);
                ledColorSet(0x7c,0xfc);
                delay(500);
        }
        return 0;
}
Python版本
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

colors = [0xFF00, 0x00FF, 0x0FF0, 0xF00F]
pins = (11, 12) # pins is a set

GPIO.setmode(GPIO.BOARD) #Numbers GPIOs by physical location
GPIO.setup(pins, GPIO.OUT) #Set pins mode is output
GPIO.output(pins, GPIO.LOW) #Set pins to LOW(0V) to off led

p_R = GPIO.PWM(pins[0], 2000) #set Frequency to 2KHz
p_G = GPIO.PWM(pins[1], 2000) 

p_R.start(0)     #Initial duty Cycle = 0(leds off)
p_G.start(0)

def map_led(x, in_min, in_max, out_min, out_max):
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

def setColor(col): #For example: col = 0x1122
    R_val = col >> 8
    G_val = col & 0x00FF

    R_val = map_led(R_val, 0, 255, 0, 100)
    G_val = map_led(G_val, 0, 255, 0, 100)

    p_R.ChangeDutyCycle(R_val) #Change duty cycle
    p_G.ChangeDutyCycle(G_val)

def loop():
    while True:
        for col in colors:
            setColor(col)
            time.sleep(0.5)

def destroy():
    p_R.stop()
    p_G.stop()
    GPIO.output(pins, GPIO.LOW) #Turn off all leds
    GPIO.cleanup()


if __name__ == "__main__":
    try:
        loop()
    except KeyboardInterrupt:
        destroy()

扩展学习

树莓派基础实验1:双色LED灯实验
树莓派基础实验1:双色LED灯实验

相关文章

网友评论

      本文标题:【树莓派+arduino实验记录1】双色LED

      本文链接:https://www.haomeiwen.com/subject/rhrjxktx.html