#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
                                 InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.nxtdevices import EnergyMeter
from pybricks.iodevices import DCMotor                                
from pybricks.parameters import Port, Stop, Direction, Button, Color
from pybricks.tools import wait, StopWatch, DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile, ImageFile


# # This program requires LEGO EV3 MicroPython v2.0 or higher.
# # Click "Open user guide" on the EV3 extension tab for more information.


# # Create your objects here.
ev3 = EV3Brick()


# Write your program here.
ev3.speaker.beep()
us = UltrasonicSensor(Port.S4)
Light_1 = DCMotor(Port.C, positive_direction=Direction.CLOCKWISE)
us2 = UltrasonicSensor(Port.S3)
Light_2 = DCMotor(Port.D, positive_direction=Direction.CLOCKWISE)
energy_meter_s1 = EnergyMeter(Port.S1)
wait(3000)

global solnce
solnce = 0
peremennaya = 0

def voltage_check():
    voltage = ev3.battery.voltage()
    if voltage <= 7500:
        ev3.speaker.beep()
old_voltage = ev3.battery.voltage()
ev3.light.on(Color.RED)

def pitanie():
    
    global solnce
    v, a, w = energy_meter_s1.input()
    print(v, "   ", a,"    ", w, "solnce", solnce )
    if v > 3000:
        solnce = 1
    else:
        solnce = 0


while True:
   # print(us.distance(),us2.distance())
    if us.distance() < 200:
       Light_1.dc(10)
    else:
       Light_1.stop()
    if us2.distance() < 200:
       Light_2.dc(10)
    else:
       Light_2.stop()
    pitanie()
    #print(ev3.battery.voltage())
    new_voltage = ev3.battery.voltage()
    if new_voltage  > old_voltage + 50:
        peremennaya = 1
    old_voltage = new_voltage
    wait(100)
    #print(peremennaya)
    if peremennaya == 1 and solnce == 1:
        ev3.light.on(Color.GREEN) 
        wait(50)
        ev3.light.on(Color.BLACK) 
        wait(50)
    elif peremennaya == 0 and solnce == 1:
        ev3.light.on(Color.ORANGE) 
    elif peremennaya == 1 and solnce == 0:
        ev3.light.on(Color.GREEN) 
        wait(300)
        ev3.light.on(Color.BLACK) 
        wait(300)
    else: 
        ev3.light.on(Color.RED) 
