Recent Posts

Pages: [1] 2 3 ... 10
1
Microcontrollers / Attiny85 Plant Watering system...
Last post by Anton3006 - Today at 03:39 pm
hey, I build my own plant watering system with an attiny85.
The problem: the code works perfectly on an arduino uno but not on the attiny85.

It don't turn on the pumps, maybe because it can't read the values well...


Attiny85 code:
Code: [Select]

//                  +-\/-+
// Ain0 (D 5) PB5  1|    |8  Vcc
// Ain3 (D 3) PB3  2|    |7  PB2 (D 2) Ain1
// Ain2 (D 4) PB4  3|    |6  PB1 (D 1) pwm1
//            GND  4|    |5  PB0 (D 0) pwm0
//                  +----+

#include <avr/sleep.h>    // Sleep Modes
#include <avr/power.h>    // Power management
#include <avr/wdt.h>      // Watchdog timer




#define SENSOR1 3 //Attiny2
#define SENSOR2 2 //Attiny3 = P2
#define PUMP1 0   //Attiny5
#define PUMP2 1   //Attiny6
#define RELAY 2   //Attiny 7

int SensorValue1; //Read Value
int SensorValue2; //
int ValueP1;      //Value percent
int ValueP2;      //



ISR (PCINT0_vect)
 {
 // do something interesting here
 }  // end of PCINT0_vect
 
// watchdog interrupt
ISR (WDT_vect)
{
   wdt_disable();  // disable watchdog
}  // end of WDT_vect

void resetWatchdog ()
{
  // clear various "reset" flags
  MCUSR = 0;     
  // allow changes, disable reset, clear existing interrupt
  WDTCR = bit (WDCE) | bit (WDE) | bit (WDIF);
  // set interrupt mode and an interval (WDE must be changed from 1 to 0 here)
  WDTCR = bit (WDIE) | bit (WDP3) | bit (WDP0);    // set WDIE, and 8 seconds delay
  // pat the dog
  wdt_reset(); 
}  // end of resetWatchdog
 

void setup()
{
  resetWatchdog ();  // do this first in case WDT fires
  pinMode(PUMP1, OUTPUT);
  pinMode(SENSOR1, INPUT);
  pinMode(PUMP2, OUTPUT);
  pinMode(SENSOR2, INPUT);
  pinMode(RELAY, OUTPUT);
}

void loop()
{
  digitalWrite(RELAY, HIGH);    //turn on sensors
  delay(1000);
  for(int x=0; x<10; x++)       //read 10 times and add
  {
    SensorValue1 = analogRead(SENSOR1);
    SensorValue2 = analogRead(SENSOR2);
    ValueP1 = ValueP1 + map(SensorValue1, 0, 400, 100, 0);
    ValueP2 = ValueP2 + map(SensorValue2, 0, 400 , 100, 0);
    delay(100);
  }
  ValueP1 = ValueP1/10;        //divide by 10 to get average value
  ValueP2 = ValueP2/10;
  while((ValueP1<60) || (ValueP2<60))  //if one of the plants need water do this
  {
    watering();
  }
  ValueP1 = 0;        //reset Values
  ValueP2 = 0;
  digitalWrite(RELAY, LOW);   // turn off sensors
  for(int x=0; x<1; x++)      //set to sleep for x times 10 sec
  {
    goToSleep ();
  }
}


void watering()
{
  SensorValue1 = analogRead(SENSOR1);   //read sensor values
  SensorValue2 = analogRead(SENSOR2);
  ValueP1 = map(SensorValue1, 0, 400, 100, 0);  //map to percent // 0 to 400 because this is the range for the perfect soil moist
  ValueP2 = map(SensorValue2, 0, 400 , 100, 0);
 
  if(ValueP1<60)
  {
    digitalWrite(PUMP1, HIGH);   //turn on pump for first plant
  }
  else
  {
    digitalWrite(PUMP1, LOW);   //turn of if enough water
  }

  if(ValueP2<60)
  {
    digitalWrite(PUMP2, HIGH);
  }
  else
  {
    digitalWrite(PUMP2, LOW);
  }
  delay(1000);
}


void goToSleep ()
  {
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);
  ADCSRA = 0;            // turn off ADC
  power_all_disable ();  // power off ADC, Timer 0 and 1, serial interface
  noInterrupts ();       // timed sequence coming up
  resetWatchdog ();      // get watchdog ready
  sleep_enable ();       // ready to sleep
  interrupts ();         // interrupts are required now
  sleep_cpu ();          // sleep               
  sleep_disable ();      // precaution
  power_all_enable ();   // power everything back on
  }  // end of goToSleep



Arduino Uno code:
Code: [Select]

#define SENSOR1 0
#define SENSOR2 1
#define PUMP1 9
#define PUMP2 10
#define RELAY 8

int SensorValue1; //
int SensorValue2; //
int ValueP1;      //
int ValueP2;      //


void setup()
{
  Serial.begin(9600);
  pinMode(PUMP1, OUTPUT);
  pinMode(SENSOR1, INPUT);
  pinMode(PUMP2, OUTPUT);
  pinMode(SENSOR2, INPUT);
  pinMode(RELAY, OUTPUT);
}

void loop()
{
  digitalWrite(RELAY, HIGH);
  Serial.println("RELAY=ON");
  delay(1000);
  for(int x=0; x<10; x++)
  {
    SensorValue1 = analogRead(SENSOR1);
    SensorValue2 = analogRead(SENSOR2);
    ValueP1 = ValueP1 + map(SensorValue1, 0, 400, 100, 0);
    ValueP2 = ValueP2 + map(SensorValue2, 0, 400 , 100, 0);
    delay(100);
  }
  ValueP1 = ValueP1/10;
  ValueP2 = ValueP2/10;
  Serial.print(SensorValue1);
  Serial.print(" | ");
  Serial.println(ValueP1);
  Serial.print(SensorValue2);
  Serial.print(" | ");
  Serial.println(ValueP2);
  while((ValueP1<60) || (ValueP2<60))
  {
    Serial.println("WATERING");
    watering();
  }
  digitalWrite(RELAY, LOW);
  Serial.println("RELAY=OFF");
  for(int x=0; x<3; x++)
  {
    delay(1000);
    Serial.println(x);
  }
}


void watering()
{
  SensorValue1 = analogRead(SENSOR1);
  SensorValue2 = analogRead(SENSOR2);
  ValueP1 = map(SensorValue1, 0, 400, 100, 0);
  ValueP2 = map(SensorValue2, 0, 400 , 100, 0);
  Serial.print(ValueP1);
  Serial.print(" | ");
  Serial.println(ValueP2);
  if(ValueP1<60)
  {
    digitalWrite(PUMP1, HIGH);
    Serial.println("PUMP1=ON");
  }
  else
  {
    digitalWrite(PUMP1, LOW);
    Serial.println("PUMP1=OFF");
  }

  if(ValueP2<60)
  {
    digitalWrite(PUMP2, HIGH);
    Serial.println("PUMP2=ON");
  }
  else
  {
    digitalWrite(PUMP2, LOW);
    Serial.println("PUMP2=OFF");
  }
  delay(1000);
}


The sleepmode is from here(but I don't think this is the problem):http://gammon.com.au/forum/?id=11497&reply=6#reply6

I'm using this type of sensor:https://www.amazon.com/Kuman-Moisture-Compatible-Raspberry-Automatic/dp/B071F4RDHY/ref=sr_1_4?ie=UTF8&qid=1544797339&sr=8-4&keywords=soil+moisture+sensor

I attached my cuircitdiagramm(LED should be pumps)

please help me and excuse my bad english, I'm from germany


2
The Diagram isn't exactly the model 1:1, its just a diagram i found that is extremely similar to my circuit, just ignore the buttons,LCD screen and the pot and reverse the ground and vcc pins. I was trying to mess with the code to see if a certain number can represent a LED on the matrix but it didn't really work out as you can tell. Im not sure what kind of code to use correctly to get the correct output from both 595's. For instance, i wanted to see if I can light the very 1st LED, at the top right and then light the very bottom right of the matrix. I only know how to set the output's of the ardunio pins to match the data lines of the 595 (like connecting Clock, latch and data pins and setting them to output on arduino) but from there , i dont know how to use the correct multiplexing code. Is there any other code for controlling the outputs of the 595's?
3
Sensors / Re: Using Multiple ina219 sens...
Last post by Sprotty - Today at 03:38 pm
Looking at the data sheet you may be able to set a bit address
http://www.ti.com/lit/ds/symlink/ina219.pdf

A1 A0 SLAVE ADDRESS
GND GND 1000000
GND VS+ 1000001
GND SDA 1000010
GND SCL 1000011
VS+ GND 1000100
VS+ VS+ 1000101
VS+ SDA 1000110
VS+ SCL 1000111
SDA GND 1001000
SDA VS+ 1001001
SDA SDA 1001010
SDA SCL 1001011
SCL GND 1001100
SCL VS+ 1001101
SCL SDA 1001110
SCL SCL 1001111
4
Bar Sport / Re: What do you look for when ...
Last post by wildbill - Today at 03:37 pm
And then compile it with max warning level. That's found the problem on any number of occasions.
5
Show your test code you are trying.

Use CTRL T to format your code.
Attach your sketch between code tags
[code]Paste your sketch here[/code]


6
Hardware / Re: Solar panel + Ni-MH
Last post by Triko93 - Today at 03:35 pm
Non penso che se la tensione e' sotto la solgia succeda qualcosa, al massimo non si accende e non succede nulla!
7
Deutsch / Re: Briefkasten Meldeanlage LO...
Last post by Doc_Arduino - Today at 03:32 pm
Hallo,

combie, es geht um Programmcode in der loop. Es geht nicht um Hardwareeinheiten die nebenbei alleine arbeiten können. Am Timer Einstellungen ändern und die USART auslesen geht  auch nicht gleichzeitig sondern nur nacheinander.

Die Diskussion von Micha und Gregor ist eine Frage der Definition "Gleichzeitig". Laut meiner Definition ist das "zur gleichen Zeit mehrere Dinge tun", hier nicht möglich. Ein Nachtwächter kann auf seiner Runde (loop) tausende Dinge tun. Aber diese nicht zur gleichen Zeit. Mit jeder zusätzlichen Aufgabe verlängert sich seine Rundenzeit.

Gleichzeitig arbeiten können nur die Hardwareinheiten wenn sie mit sich selbst zu tun haben. Und selbst da gibts Abhängigkeiten durch Interrupts. Wenn man das ganz genau betrachtet.
8
Microcontrollers / Re: usbasp cannot set sck peri...
Last post by firatdk33 - Today at 03:32 pm
I was having same problem. How did I solve ? I put 100nF bypass cap to AREF pin. Worked !
9
Deutsch / Re: Datum aus NTP-Datenpaket b...
Last post by Tommy56 - Today at 03:30 pm
Du kannst ja mal beim Autor nachfragen, ob er ihn für Dich noch kommentiert ;)

Der Code ist auch ohne zusätzliche Kommentare verständlich, wenn man ein paar Grundlagen beherrscht.

Du brauchst auch nicht zu schreiben, dass Du die Antwort als nutzlos empfindest. Du bekommst keine andere von mir.

Gruß Tommy
10
Français / Re: Anti-Rebonds
Last post by 68tjs - Today at 03:27 pm
Le rebond est un effet ressort sur les contacts, il est d'origine uniquement mécanique et ne peut se manifester qu'à la suite d'un appui sur le bouton qui va faire entrer les extrémités des lames en contact.
L'amortissement faisant son effet les rebonds cessent assez vite.
Mais c'est vrai tant qu'il y a des rebond, l'entrée du microcontrôleur les interprète comme un appui volontaire sur le bouton, comment pourrait-elle faire autrement ?

Pour un anti rebond matériel le couple qui va bien avec des composants passe partout que normalement nous avons tous au fond du tiroir :
Résistance en série avec le contact : 10 kohms (à la place on peut aussi utiliser la pull-up de l'entrée du micro)
Condensateur en parallèle sur le contact : 100 nF.
Pages: [1] 2 3 ... 10