JS代码+实现亮/关灯

本文介绍了如何通过JavaScript实现一个简单的灯泡开关效果,包括点击灯泡和代码按钮触发亮灯或关灯的功能。
摘要由CSDN通过智能技术生成

标题 灯泡的点击

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<img id="calss" src="img/p1.jpg" height="200" width="300" /><br />
		<div id="liss" height="100"  width="100"></div>
		<button id="push" onclick="reg()">按钮</button>
        
        <script type="text/javascript">
        	
			var time=new Date();
	


        	var img  =["img/p1.jpg","img/p2.jpg"];
        	var i=0;
        	function reg(){
        		document.getElementById("calss").src = img[i];
        		if(i == 0){
        	
        			i = 1;
        			var a = time.getFullYear()+"年"+(time.getMonth()+1)+"月"+time.getDate()+"号"+
		            time.getHours()+"时"+time.getMinutes()+"分"+time.getSeconds()+"秒";
        			document.getElementById("push").innerHTML="开";
        			document.getElementById("liss").innerHTML = a +":灯灭啦";
        			
        		}else{
        			i=0;
        			var a = time.getFullYear()+"年"+(time.getMonth()+1)+"月"+time.getDate()+"号"+
		            time.getHours()+"时"+time.getMinutes()+"分"+time.getSeconds()+"秒";
        			document.getElementById("push").innerHTML="关";
        			document.getElementById("liss").innerHTML = a +":灯亮啦";
        		}
        	}
        </script>
		
		
	</body>
</html>

标题 代码按钮点击实现效果

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
int main(void) { delay_init(); //延时函数初始化 NVIC_Configuration();//设置NVIC中断分组2:2位抢占优先级,2位响应优先级 LED_Init(); //初始化与LED连接的硬件接xBF uart_init(9600); TIM3_Int_Init(499,7199);//10Khz的计数频率,计数到500为50ms i=50; while(i--) delay_ms(100); printf("AT+CIPMUX=1\r\n"); //允许链接 i=10; while(i--) delay_ms(100); printf("AT+CIPSERVR=1,8080\r\n"); //创建端口号8080 while(1) { if(ReadFlag== 1) //读取串口数据标志 { Count=0; //传授接收变量清零 UartBusy=0; ReadFlag=0; //读取标识清零 if((strstr((const char *)BufTab,"OPEN1")!=NULL)||(strstr((const char * )BufTab,"LED1-3")!=NULL)) //接收到LPEN1 LED1-3 { PWML_LED1=10;rebackFalg=1;//设置pwm 发送标志置位 } else if(strstr((const char * )BufTab,(const char * )"CLOSE1")!=NULL) //接收到CLOSE1 { PWML_LED1=0;rebackFalg=1;//设置pwm 发送标志置位 } else if((strstr((const char *)BufTab,"OPEN2")!=NULL)||(strstr((const char *)BufTab,"LED2-3")!=NULL)) //接收到 OPEN2 { PWML_LED2=10;rebackFalg=1;//设置pwm 发送标志置位 } else if(strstr((const char *)BufTab,"LLOSE2")!=NULL) //接收到LLOSE2 { PWML_LED2=0;rebackFalg=1;//设置pwm 发送标志置位 } } else if(strstr((const char *)BufTab,"OPENALL")!=NULL) //接收OENALL { PWML_LED1=10;PWML_LED2=10;rebackFalg=1; //设置pwm 发送标志置位 } else if(strstr((const char *)BufTab,"CLOSEALL")!=NULL) //接收到CLOSEALL { PWML_LED1=0;PWML_LED2=0; rebackFalg=1; } for(i=0;i<10;i++) //清空wifi数据数组 { BufTab[i]='0'; } if((sendDataFlag == 1)&&(rebackFalg != 0))//接收到数后返回ok { if(MesCount == 0) //发送信息计数 { MesCount =1; printf("AT+CIPSEND=0,2\r\n"); //发送固定字节数据的at命令 } else { if(rebackFalg ==1) //返回标志置位 {printf("OK");} //发送ok MesCount = 0; //发送信息计数 rebackFalg = 0; } sendDataFlag = 0; //定时发送数据清空 } led1Count++; //led 对比pwm值计数 led2Count++; //led 对比pwm值计数 if(led1Count<PWML_LED1) //led1 PWM对比 { LED1=1; //开灯 } else if((led1Count>=PWML_LED1)&&(led1Count<=10)) //led1 PWM对比 { LED1=0; //关灯 } else { led1Count=0; //一个周期结束 } if(led2Count<PWML_LED2) //led2 PWM对比 { LED2=1; //开灯 } else if((led2Count>=PWML_LED2)&&(led2Count<=10)) //led2 PWM对比 { LED2=0; //关灯 } else { led2Count=0; //一个周期结束 } } } 请在此代码上,添加代码,使得LED1和LED2的闪烁频率能够实现三个等级的度变化,
06-12
可以在代码中添加一个变量来控制 LED度等级,然后根据不同的度等级设置不同的 PWM 值来实现 LED度变化。 具体代码实现如下: ```c int main(void) { int brightness_level = 1; // 度等级,初始值为1 delay_init(); NVIC_Configuration(); LED_Init(); uart_init(9600); TIM3_Int_Init(499,7199); i = 50; while(i--) delay_ms(100); printf("AT+CIPMUX=1\r\n"); i = 10; while(i--) delay_ms(100); printf("AT+CIPSERVR=1,8080\r\n"); while(1) { if(ReadFlag == 1) { Count = 0; UartBusy = 0; ReadFlag = 0; if((strstr((const char *)BufTab,"OPEN1")!=NULL)||(strstr((const char * )BufTab,"LED1-3")!=NULL)) { brightness_level = 1; // 设置度等级为1 rebackFalg=1; } else if(strstr((const char * )BufTab,(const char * )"CLOSE1")!=NULL) { brightness_level = 0; // 设置度等级为0 rebackFalg=1; } else if((strstr((const char *)BufTab,"OPEN2")!=NULL)||(strstr((const char *)BufTab,"LED2-3")!=NULL)) { brightness_level = 2; // 设置度等级为2 rebackFalg=1; } else if(strstr((const char *)BufTab,"LLOSE2")!=NULL) { brightness_level = 0; // 设置度等级为0 rebackFalg=1; } } else if(strstr((const char *)BufTab,"OPENALL")!=NULL) { brightness_level = 3; // 设置度等级为3 rebackFalg=1; } else if(strstr((const char *)BufTab,"CLOSEALL")!=NULL) { brightness_level = 0; // 设置度等级为0 rebackFalg=1; } for(i = 0; i < 10; i++) { BufTab[i] = '0'; } if((sendDataFlag == 1)&&(rebackFalg != 0)) { if(MesCount == 0) { MesCount = 1; printf("AT+CIPSEND=0,2\r\n"); } else { if(rebackFalg == 1) { printf("OK"); } MesCount = 0; rebackFalg = 0; } sendDataFlag = 0; } led1Count++; led2Count++; if(led1Count < (10 * brightness_level / 3)) { // 根据度等级设置 PWM 值 LED1 = 1; } else if((led1Count >= (10 * brightness_level / 3)) && (led1Count <= 10)) { LED1 = 0; } else { led1Count = 0; } if(led2Count < (10 * brightness_level / 3)) { LED2 = 1; } else if((led2Count >= (10 * brightness_level / 3)) && (led2Count <= 10)) { LED2 = 0; } else { led2Count = 0; } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值