Im trying to write a basic robot function that moves servo when buttons are pressed. I have the thing debugging to the log that it turns on and off using button presses and a bool. Now i cant get the servo variable to get into scope. Towards the botttom of the script where the servo is writing to the device is where im having trouble. It wont do device.write. Device isnt in scope. Ive tried adding static to the variable.
using System.Threading;
using System.Diagnostics;
using GHIElectronics.TinyCLR.Pins;
using GHIElectronics.TinyCLR.Drivers.Infrared;
using GHIElectronics.TinyCLR.Devices.Gpio;
using System;
using GHIElectronics.TinyCLR.UI.Controls;
using GHIElectronics.TinyCLR.Devices.I2c;
namespace RobotApp
{
class Program
{
static int buttonPressed = 100;
static bool isPoweredOn;
static void Main(string[] args)
{
//IR Remote Setup
var recievePin = GpioController.GetDefault().OpenPin(FEZBit.GpioPin.P8);
var ir = new NecIRDecoder(recievePin);
ir.OnDataReceivedEvent += Ir_OnDataRecievedEvent;
ir.OnRepeatEvent += Ir_OnRepeatEvent;
Debug.WriteLine("System is ready!");
//Setup for servos
var settings = new I2cConnectionSettings(0x01, 100_000);
var controller = I2cController.FromName(FEZBit.I2cBus.Edge);
var device = controller.GetDevice(settings);
while (true)
{
if (buttonPressed == 0)
{
isPoweredOn = !isPoweredOn;
Debug.WriteLine("Power Button pressed!");
TurnOnRobot(isPoweredOn);
buttonPressed = 100;
}
}
}
//Events for button presses
static void Ir_OnDataRecievedEvent(byte address, byte command)
{
Debug.WriteLine("A: " + address + " C: " + command);
buttonPressed = command;
}
static void Ir_OnRepeatEvent()
{
Debug.WriteLine("Repeat!");
}
//Turning the robot on with remote by bool
static void TurnOnRobot(bool value)
{
//Turn on or off
if(value)
Debug.WriteLine("Turned robot on!");
else
{
Debug.WriteLine("Turned robot off!");
}
}
//Sets servo motor speed
static void SetMotorSpeed(double left, double right)
{
var b5 = new byte[5];
left = left / 100; right = right / 100;
b5[0] = 0x02;
if (left > 0)
{
b5[1] = (byte)(left * 255);
b5[2] = 0x00;
}
else
{
left *= -1;
b5[1] = 0x00;
b5[2] = (byte)(left * 255);
}
if (right > 0)
{
b5[3] = (byte)(right * 255);
b5[4] = 0x00;
}
else
{
right *= -1;
b5[3] = 0x00;
b5[4] = (byte)(right * 255);
}
device.Write(b5);
}
}
}
Tried to change the variables scope and move the initial code block but unsuccessful. I normally code in c#, but within Unity, so this is fairly new to me. Some of the code syntax is new to me.
WestMansionHero is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.