ШИМ
- Войдите на сайт для отправки комментариев
Здравствуйте, подскажите как изменить код:
#include <dht11.h>
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#define DHT11_PIN 7
#define REQ_BUF_SZ 40
dht11 DHT;
File webFile;
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 3, 181);
EthernetServer server(80);
bool pin1;
bool pin2;
bool pin3;
bool pin4;
int pin5;
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
SD.begin(4);
Ethernet.begin(mac, ip);
server.begin();
pin1 = pin2 = pin3 = pin4 = 0;
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
if (c == '\n' && currentLineIsBlank) {
if (StrContains(HTTP_req, "GET /")) {
if (StrContains(HTTP_req, "/ ")
|| StrContains(HTTP_req, "/index.htm")) {
sendHtmlFile(client, "index.htm");
} else if (StrContains(HTTP_req, "/favicon.ico")) {
sendFile(client, "favicon.ico");
} else if (StrContains(HTTP_req, "/temp.png")) {
sendFile(client, "temp.png");
} else if (StrContains(HTTP_req, "/humid.png")) {
sendFile(client, "humid.png");
} else if (StrContains(HTTP_req, "/flame.png")) {
sendFile(client, "flame.png");
} else if (StrContains(HTTP_req, "/my.css")) {
sendFile(client, "my.css");
} else if (StrContains(HTTP_req, "ajax_flame")) {
sendBaseAnswer(client);
int smoke_gas = 0; //пин на котором подключен MQ-2
int sensorReading = analogRead(smoke_gas);
int chk;
chk = DHT.read(DHT11_PIN);
char buff[32];
sprintf(buff, "%d:%d:%d:%d:%d:%d:%d:%d:",
sensorReading, DHT.temperature, DHT.humidity,
digitalRead(2), digitalRead(3), digitalRead(5), digitalRead(6),
pin5);
client.println(buff);
} else if (StrContains(HTTP_req, "setpin?")) {
if (StrContains(HTTP_req, "pin=1")) {
pin1 = !pin1;
digitalWrite(2, pin1);
} else if (StrContains(HTTP_req, "pin=2")) {
pin2 = !pin2;
digitalWrite(3, pin2);
} else if (StrContains(HTTP_req, "pin=3")) {
pin3 = !pin3;
digitalWrite(5, pin3);
} else if (StrContains(HTTP_req, "pin=4")) {
pin4 = !pin4;
digitalWrite(6, pin4);
} else if (StrContains(HTTP_req, "pin=5")) {
String input = HTTP_req;
int posStart = input.indexOf("value=");
int posEnd = input.indexOf(' ', posStart);
String param = input.substring(posStart + 6, posEnd + 1);
pin5 = param.toInt();
analogWrite(9, pin5);
}
sendBaseAnswer(client);
}
}
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}
bool sendHtmlFile(EthernetClient client, char *fileName) {
webFile = SD.open(fileName);
sendBaseAnswer(client);
return sendFile(client, webFile);
}
bool sendFile(EthernetClient client, char *fileName) {
webFile = SD.open(fileName);
sendHttpOkAnswer(client);
client.println();
return sendFile(client, webFile);
}
bool sendFile(EthernetClient client, File &webFile) {
if (webFile) {
while (webFile.available())
client.write(webFile.read()); // send web page to client
webFile.close();
return 1;
}
return 0;
}
void sendBaseAnswer(EthernetClient client) {
sendHttpOkAnswer(client);
client.println(F("Content-Type: text/html"));
client.println(F("Connection: close"));
client.println();
}
void sendHttpOkAnswer(EthernetClient client) {
client.println(F("HTTP/1.1 200 OK"));
}
void StrClear(char *str, char length) {
for (int i = 0; i < length; i++) {
str[i] = 0;
}
}
char StrContains(char *str, char *sfind) {
char found = 0;
char index = 0;
char len;
len = strlen(str);
if (strlen(sfind) > len) {
return 0;
}
while (index < len) {
if (str[index] == sfind[found]) {
found++;
if (strlen(sfind) == found) {
return 1;
}
}
else {
found = 0;
}
index++;
}
return 0;
}
HTML:
<html>
<head>
<meta http-equiv='content-type' content='text/html; charset=UTF-8'>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<title>Arduino WEB контроль</title>
<link type="text/css" rel="StyleSheet" href="/my.css" />
<script>
function GetFlameState() {
nocache = "&nocache=" + Math.random() * 1000000;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText != null) {
var arrayOfStrings = this.responseText.split(":");
document.getElementById("flame_txt").innerHTML = arrayOfStrings[0];
document.getElementById("temp_txt").innerHTML = arrayOfStrings[1];
document.getElementById("humid_txt").innerHTML = arrayOfStrings[2];
for(var i = 1 ; i < 5 ; i++)
if(arrayOfStrings[2+i] == "1")
document.getElementById("led_"+i).setAttribute("class","button_enabled");
else
document.getElementById("led_"+i).setAttribute("class","button_disabled");
}
}
}
}
request.open("GET", "ajax_flame" + nocache, true);
request.send(null);
setTimeout('GetFlameState()', 1000);
}
function onClick(pin){
var request = new XMLHttpRequest();
request.open("GET", "\setpin?pin=" + pin , false);
request.send(null);
}
function PWM(){
value = document.getElementById("led_PWM").value;
var request = new XMLHttpRequest();
request.open("GET", "\setpin?pin=5?value=" + value, false);
request.send(null);
}
</script>
</head>
<body onload="GetFlameState()">
<div class="form">
<h2>Arduino WEB контроль</h2>
<hr noshade size="1px" color="white">
<h3>Метеостанция</h3>
<table align="center">
<tr>
<td><img src='flame.png' /></td>
<td valign="center">Датчик дыма</td>
<td><span id="flame_txt"> 0</span></td>
</tr>
<tr>
<td><img src='temp.png' /></td>
<td valign="center">Температура</td>
<td><span id="temp_txt">0</span> °C</td>
</tr>
<tr>
<td><img src='humid.png' /></td>
<td valign="center">Влажность</td>
<td><span id="humid_txt">0</span> %</td>
</tr>
</table>
<center>
<hr noshade size="1px" color="white">
<h3>Управление нагрузками</h3>
<button type="button" id="led_1" class="button_disabled" onClick="onClick(1)">Реле №1</button>
<button type="button" id="led_2" class="button_disabled" onClick="onClick(2)">Реле №2</button>
<button type="button" id="led_3" class="button_disabled" onClick="onClick(3)">Реле №3</button>
<button type="button" id="led_4" class="button_disabled" onClick="onClick(4)">Реле №4</button><br>
<hr noshade size="1px" color="white">
<h3>Регулировка мощности</h3><input type="range" min="0" max="255" id="led_PWM" step="2.55" oninput="PWM()" value="0" class="rangeP">
<hr noshade size="1px" color="white">
</center>
</div>
</Body>
</html>
В общем ШИМ на пине 9 управляется из HTML ползунком, всё работает, но,
когда ползунок с левой стороны - светодиод светится ярче
когда с правой - тусклеет
Как поменять местами?
Чтобы когда ползунок слева светодиод светился тусклее, когда справа ярче.
Заранее благодарю за ответ.
Вычесть значение pwm из 255.
С 255 уже что только не пробовал делать, с большинстве вариантов ползунок вообще не двигается.
А ты в 95 строке из 255 pin5 вычитаешь?
Спасибо всем, заработало!
Здраствуйте,
Скажите, какая ето функция с каторая я могу сохранит последние данни ШИМ-а, после изключение напрежение? Вероятно ето функция в сохранения в Епром памяти, и потом когда влючаeтся кантролeр в меню voit setup пропишем ето последное значение память?