The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Arduino Yun with AC dimmer
Kristijan1392
post Sep 22 2016, 02:12 PM
Post #1


Member
***

Group: Members
Posts: 32
Joined: 10-November 15
Member No.: 23,730



This is the program that works for changing pin values over web:
CODE
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <dht11.h>
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
int potPin = A1; //Potentiometer input pin
int potValue1 = 0;
int potValue2 = 0; // final display variable
int greenPin = A0;
int pin9 = 9;
int pin12 = 12;
dht11 DHT11;
int dac = 0;
int DigitalPin[] = {2, 12, 13};
int DacPin = 3;
YunServer server;
#define I2C_ADDR 0x27 // <<- Add your address here.
#define Rs_pin 0
#define Rw_pin 1
#define En_pin 2
#define BACKLIGHT_PIN 3
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
void setup() {
pinMode(2,INPUT);
pinMode(4,INPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
digitalWrite(13, LOW);
Bridge.begin();
digitalWrite(13, HIGH);
server.listenOnLocalhost();
server.begin();
lcd.begin (16,2); // <<-- our LCD is a 20x4, change for your LCD if needed

// LCD Backlight ON
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);

lcd.home (); // go home on LCD
lcd.print("HC-SR04");
  Serial.begin (9600);
}
void loop() {
YunClient client = server.accept();
if (client) {
process(client);
client.stop();
}
  DHT11.read(greenPin);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Trenutno = ");
  lcd.setCursor(12,0);
  lcd.print(DHT11.temperature);
  lcd.setCursor(14,0);
  lcd.print((char)223);
  lcd.print("C");
  delay(100);
  lcd.setCursor(0,1);
  lcd.print("Odabrano = ");
  lcd.setCursor(12,1);
  lcd.print(potValue2);
  lcd.setCursor(14,1);
  lcd.print((char)223);
  lcd.print("C");
  delay(100);
  if(DHT11.temperature >= potValue2){
  analogWrite(pin12,255);
  }
  else if(DHT11.temperature <= potValue2){
analogWrite(pin12,0);
}
delay(50);
}
void process(YunClient client) {
String command = client.readStringUntil('/');
if (command == "digital") {
digitalCommand(client);
}
if (command == "dac") {
dacCommand(client);
}
if (command == "status") {
statusCommand(client);
}
}
void digitalCommand(YunClient client) {
int pin, value;
pin = client.parseInt();
if (client.read() == '/') {
value = client.parseInt();
digitalWrite(pin, value);
}
else {
value = digitalRead(pin);
}
client.print(F("digital,"));
client.print(pin);
client.print(F(","));
client.println(value);
}
void dacCommand(YunClient client) {
int pin, value;
pin = client.parseInt();
if (client.read() == '/') {
value = client.parseInt();
dac = value;
analogWrite(pin, value);
}
else {
value = dac;
}
client.print(F("dac,"));
client.print(pin);
client.print(F(","));
client.println(value);
}
void statusCommand(YunClient client) {
int pin, value;
client.print(F("status"));
for (int thisPin = 0; thisPin < 3; thisPin++) {
pin = DigitalPin[thisPin];
value = digitalRead(pin);
client.print(F("#"));
client.print(pin);
client.print(F("="));
client.print(value);
}
{
pin = DacPin;
value = dac;
client.print(F("#"));
client.print(pin);
client.print(F("="));
client.print(value);
}
{
// read then divide the input(max 1020 in this case) by 10
    potValue1 = analogRead(potPin) / 10;
// divide by 1.02 to get percentage
    potValue2 = potValue1 / 2.2;
float temp = (potValue2);
client.print(F("#A1"));
client.print(F("="));
client.print(temp);
float temperature = (DHT11.temperature);
client.print(F("#A0"));
client.print(F("="));
client.print(temperature);
}
client.println("");
}

And html:
CODE
<html><head>

<title>Arduino Yun I/O Demo</title>

<script type="text/javascript">

window.onload=Pinstatus;

function Pinstatus(){

morestatus();

}

function morestatus(){

setTimeout(morestatus, 2000);

document.getElementById("description").innerHTML = "Processing Status";

server = "/arduino/status/99";

request = new XMLHttpRequest();

request.onreadystatechange = updateasyncstatus;

request.open("GET", server, true);

request.send(null);

}

function updateasyncstatus(){

if ((request.readyState == 4) && (request.status == 200))

{

result = request.responseText;

document.getElementById("description").innerHTML = result;

fullset = result.split("#");

document.getElementById("description").innerHTML = fullset;

for(i = 1; i < fullset.length; i++){

PinPair = fullset[i];

singleset = PinPair.split("=");

PN = singleset[0];

Pinstatus = singleset[1];

if (PN > 11)

{

ActNum = "action" + PN;

ImgNum = "image" + PN;

if (Pinstatus == 0)

{

PinAct = "1";

image = "off.jpg";

}

else

{

PinAct = "0";

image = "on.jpg";

}

document.getElementById(ActNum).value = PinAct;

document.getElementById(ImgNum).src = image;

}

if (PN == 2)

{

ImgNum = "image" + PN;

if (Pinstatus == 1)

{

image = "led_on.jpg";

}

else

{

image = "led_off.jpg";

}

document.getElementById(ImgNum).src = image;

}

if (PN == 3 )

{

PinVal = parseInt(singleset[1]);

DacNum = "dac" + PN;

ValNum = "valueDac" + PN;

document.getElementById(DacNum).value = PinVal;

document.getElementById(ValNum).innerHTML = PinVal;

}

if (PN.substr(0,1) == "A")

{

PinVal = parseFloat(singleset[1]);

AnalogNum = "analog" + PN.substr(1,2);

document.getElementById(AnalogNum).value = PinVal;

}

}

}

}

function sendbutton(Pin,action){

document.getElementById("description").innerHTML = "Processing Button Click";

server = "/arduino/digital/" + Pin + "/" + action;

request = new XMLHttpRequest();

request.onreadystatechange = updateasyncbutton;

request.open("GET", server, true);

request.send(null);

}

function updateasyncbutton(){

if ((request.readyState == 4) && (request.status == 200))

{

result = request.responseText;

document.getElementById("description").innerHTML = result;

singleset = result.split(",");

PinType = singleset[0];

PinNum = singleset[1];

Pinstatus = singleset[2];

ActNum = "action" + PinNum;

ImgNum = "image" + PinNum;

if (Pinstatus == 0)

{

PinAct = "1";

image = "off.jpg";

}

else

{

PinAct = "0";

image = "on.jpg";

}

document.getElementById(ActNum).value = PinAct;

document.getElementById(ImgNum).src = image;

document.getElementById("description").innerHTML = result;

}

}

function sendDac(Pin,value){

ValNum = "valueDac" + Pin;

document.getElementById(ValNum).innerHTML=value;

document.getElementById("description").innerHTML = "Processing Slider";

server = "/arduino/dac/" + Pin + "/" + value;

request = new XMLHttpRequest();

request.onreadystatechange = updateasyncDac;

request.open("GET", server, true);

request.send(null);

}

function updateasyncDac(){

if ((request.readyState == 4) && (request.status == 200))

{

result = request.responseText;

singleset = result.split(",");

PinType = singleset[0];

PinNum = singleset[1];

PinVal = parseInt(singleset[2]);

DacNum = "dac" + PinNum;

ValNum = "valueDac" + PinNum;

document.getElementById(DacNum).value = PinVal;

document.getElementById(ValNum).innerHTML = PinVal;

document.getElementById("description").innerHTML = result;

}

}

</script>

</head>

<body><font face="Arial">

<img src="led_off.jpg" width="50" id="image2" style="display:none"><table name="Table" width="400" border="1" cellpadding="6">

<tbody><tr> <th align="center" colspan="6">Temperatura</th></tr>

<tr>

<td align="center">

Trenutno

<br>

<input type="text" style="text-align: center;" name="analogA0" id="analog0" value="0" size="6" readonly="">

</td><td align="center">

Odabrano

<br>

<input type="text" style="text-align: center;" name="analogA1" id="analog1" value="0" size="6" readonly="">

</td>

</tr><tr> <th align="center" colspan="6">PWM</th></tr>

<tr>

<td colspan="6" align="center">

3

<br>

<input type="hidden" name="pin" value="3" id="pin3">

<input type="range" style="width: 100%; height: 30px;" id="dac3" min="0" max="100" value="0" step="1" onchange="sendDac(document.getElementById('pin3').value,

this.value);">

<br>

<span id="valueDac3">0</span>

</td>

</tr>



<tr> <th align="center" colspan="6">Prekidaci</th></tr>

<tr>

<td align="center">

12

<br>

<input type="hidden" name="pin" value="12" id="pin12">

<input type="hidden" name="action" value="0" id="action12">

<img src="off.jpg" width="50" id="image12" onclick="sendbutton(document.getElementById('pin12').value,document.getElementById('action12').value);">

</td>

<td align="center">

13

<br>

<input type="hidden" name="pin" value="13" id="pin13">

<input type="hidden" name="action" value="0" id="action13">

<img src="off.jpg" width="50" id="image13" onclick="sendbutton(document.getElementById('pin13').value,document.getElementById('action13').value);">

</td>

</tr>

</tbody></table>

<br><br>

<br><br>

<p id="description">Processing Status</p>

</font>



</body></html>

this is program that works for AC dimmer:
CODE
// Testing sketch for 50Hz !!!

#include <TimerOne.h>

unsigned char channel_3 = 6; // Output to Opto Triac pin, channel 3
unsigned char CH3;
unsigned char i=0;
unsigned int delay_time=2500; // delay ms or SPEED
unsigned char clock_tick; // variable for Timer1


void setup() {

pinMode(channel_3, OUTPUT);// Set AC Load pin as output
attachInterrupt(1, zero_crosss_int, RISING);
Timer1.initialize(100); // set a timer of length 100 microseconds for 50Hz or 83 microseconds for 60Hz;
Timer1.attachInterrupt( timerIsr ); // attach the service routine here

}

void timerIsr()
{
clock_tick++;

if (CH3==clock_tick)
{
digitalWrite(channel_3, HIGH); // triac firing
delayMicroseconds(10); // triac On propogation delay (for 60Hz use 8.33)
digitalWrite(channel_3, LOW); // triac Off
}
}



void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
// Every zerocrossing interrupt: For 50Hz (1/2 Cycle) => 10ms; For 60Hz (1/2 Cycle) => 8.33ms
// 10ms=10000us , 8.33ms=8330us

clock_tick=0;
}



void loop() {
CH3=95;
delay(delay_time);

CH3=90;
delay(delay_time);

CH3=85;
delay(delay_time);

CH3=80;
delay(delay_time);

CH3=75;
delay(delay_time);

}

I would like to fuse them in one program,i don't need lcd or dht11,i just need to control my ac dimmer with slider,thanks in advance
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Kristijan1392
post Sep 23 2016, 07:06 PM
Post #2


Member
***

Group: Members
Posts: 32
Joined: 10-November 15
Member No.: 23,730



Solved smile.gif
If anybody can use this as help feel free to try my code at least to start your project.
Arduino Yun code:
CODE
#include <TimerOne.h>
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
unsigned char channel_3 = 10; // Output to Opto Triac pin, channel 3
unsigned int CH3;
unsigned int delay_time=2500; // delay ms or SPEED
unsigned int clock_tick; // variable for Timer1
int pin12 = 12;
int dac = 0;
int DigitalPin[] = {2, 12, 13};
int DacPin = 3;
YunServer server;
void setup() {
pinMode(channel_3, OUTPUT);// Set AC Load pin as output
attachInterrupt(1, zero_crosss_int, RISING);
Timer1.initialize(100); // set a timer of length 100 microseconds for 50Hz or 83 microseconds for 60Hz;
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
digitalWrite(13, LOW);
Bridge.begin();
digitalWrite(13, HIGH);
server.listenOnLocalhost();
server.begin();
}
void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
// Every zerocrossing interrupt: For 50Hz (1/2 Cycle) => 10ms; For 60Hz (1/2 Cycle) => 8.33ms
// 10ms=10000us , 8.33ms=8330us

clock_tick=0;
}
void loop() {
YunClient client = server.accept();
if (client) {
process(client);
client.stop();
}
delay(50);
}
void process(YunClient client) {
String command = client.readStringUntil('/');
if (command == "digital") {
digitalCommand(client);
}
if (command == "dac") {
dacCommand(client);
}
if (command == "status") {
statusCommand(client);
}
}
void digitalCommand(YunClient client) {
int pin, value;
pin = client.parseInt();
if (client.read() == '/') {
value = client.parseInt();
digitalWrite(pin, value);
}
else {
value = digitalRead(pin);
}
client.print(F("digital,"));
client.print(pin);
client.print(F(","));
client.println(value);
}
void dacCommand(YunClient client) {
int pin, value;
pin = client.parseInt();
if (client.read() == '/') {
value = client.parseInt();
dac = value;
analogWrite(pin, value);
}
else {
value = dac;
}
client.print(F("dac,"));
client.print(pin);
client.print(F(","));
client.println(value);
}
void statusCommand(YunClient client) {
int pin, value;
client.print(F("status"));
for (int thisPin = 0; thisPin < 3; thisPin++) {
pin = DigitalPin[thisPin];
value = digitalRead(pin);
client.print(F("#"));
client.print(pin);
client.print(F("="));
client.print(value);
}
{
pin = DacPin;
value = dac;
client.print(F("#"));
client.print(pin);
client.print(F("="));
client.print(value);
}
client.println("");
}

And html:
CODE
<html>

<head>

<title>Arduino Yun Dimmer</title>

<script type="text/javascript">

window.onload=Pinstatus;

function Pinstatus(){

morestatus();

}

function morestatus(){

setTimeout(morestatus, 2000);

document.getElementById("description").innerHTML = "Processing Status";

server = "/arduino/status/99";

request = new XMLHttpRequest();

request.onreadystatechange = updateasyncstatus;

request.open("GET", server, true);

request.send(null);

}

function updateasyncstatus(){

if ((request.readyState == 4) && (request.status == 200))

{

result = request.responseText;

document.getElementById("description").innerHTML = result;

fullset = result.split("#");

document.getElementById("description").innerHTML = fullset;

for(i = 1; i < fullset.length; i++){

PinPair = fullset[i];

singleset = PinPair.split("=");

PN = singleset[0];

Pinstatus = singleset[1];

if (PN > 11)

{

ActNum = "action" + PN;

ImgNum = "image" + PN;

if (Pinstatus == 0)

{

PinAct = "1";

image = "off.jpg";

}

else

{

PinAct = "0";

image = "on.jpg";

}

document.getElementById(ActNum).value = PinAct;

document.getElementById(ImgNum).src = image;

}

if (PN == 10 )

{

PinVal = parseInt(singleset[1]);

DacNum = "dac" + PN;

ValNum = "valueDac" + PN;

document.getElementById(DacNum).value = PinVal;

document.getElementById(ValNum).innerHTML = PinVal;

}

}

}

}

function sendbutton(Pin,action){

document.getElementById("description").innerHTML = "Processing Button Click";

server = "/arduino/digital/" + Pin + "/" + action;

request = new XMLHttpRequest();

request.onreadystatechange = updateasyncbutton;

request.open("GET", server, true);

request.send(null);

}

function updateasyncbutton(){

if ((request.readyState == 4) && (request.status == 200))

{

result = request.responseText;

document.getElementById("description").innerHTML = result;

singleset = result.split(",");

PinType = singleset[0];

PinNum = singleset[1];

Pinstatus = singleset[2];

ActNum = "action" + PinNum;

ImgNum = "image" + PinNum;

if (Pinstatus == 0)

{

PinAct = "1";

image = "off.jpg";

}

else

{

PinAct = "0";

image = "on.jpg";

}

document.getElementById(ActNum).value = PinAct;

document.getElementById(ImgNum).src = image;

document.getElementById("description").innerHTML = result;

}

}

function sendDac(Pin,value){

ValNum = "valueDac" + Pin;

document.getElementById(ValNum).innerHTML=value;

document.getElementById("description").innerHTML = "Processing Slider";

server = "/arduino/dac/" + Pin + "/" + value;

request = new XMLHttpRequest();

request.onreadystatechange = updateasyncDac;

request.open("GET", server, true);

request.send(null);

}

function updateasyncDac(){

if ((request.readyState == 4) && (request.status == 200))

{

result = request.responseText;

singleset = result.split(",");

PinType = singleset[0];

PinNum = singleset[1];

PinVal = parseInt(singleset[2]);

DacNum = "dac" + PinNum;

ValNum = "valueDac" + PinNum;

document.getElementById(DacNum).value = PinVal;

document.getElementById(ValNum).innerHTML = PinVal;

document.getElementById("description").innerHTML = result;

}

}

</script>

</head>

<font face="Arial">

<table name="Table" width="400" border="1" cellpadding="6">

<tr> <th align="center" colspan="6" >PWM</th></tr>

<tr>

<td colspan="6" align="center" >

10

<br>

<input type="hidden" name="pin" value="10" id="pin10" />

<input type="range" style="width: 100%; height: 30px;" id="dac10" min="18" max="32" value="18" step="1" onchange="sendDac(document.getElementById('pin10').value,

this.value);"/>

<br>

<span id="valueDac10">18</span>

</td>

</tr>

<img src="led_off.jpg" width="50" id="image2" style="display:none" />

<tr> <th align="center" colspan="6" >Prekidaci</th></tr>

<tr>

<td align="center">

12

<br>

<input type="hidden" name="pin" value="12" id="pin12" />

<input type="hidden" name="action" value="0" id="action12" />

<img src="off.jpg" width="50" id="image12" onclick="sendbutton(document.getElementById('pin12').value,document.getElementById('action12').value);"/>

</td>

<td align="center">

13

<br>

<input type="hidden" name="pin" value="13" id="pin13" />

<input type="hidden" name="action" value="0" id="action13" />

<img src="off.jpg" width="50" id="image13" onclick="sendbutton(document.getElementById('pin13').value,document.getElementById('action13').value);"/>

</td>

</tr>

</table>

<br><br>

<br><br>

<p id="description"> - </p>

</font>

</html>

there are few pictures and how it looks in attachments.


Attached thumbnail(s)
Attached Image Attached Image Attached Image

Attached image(s)
Attached Image Attached Image
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 23rd April 2024 - 05:05 AM