Skip to content

Attiny85 Plant Watering system error #8301

@anton3006

Description

@anton3006

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:

//                  +-\/-+
// 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:

#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):Sleepmode

I'm using this type of sensor:

I attached my cuircitdiagramm(LED should be pumps)
anmerkung 2018-12-14 153137

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


EDIT by @per1234:
Forum thread: http://forum.arduino.cc/index.php?topic=585142

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: InvalidOff topic for this repository, or a bug report determined to not actually represent a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions